4

Just wandering - how can one obtain the size of a subplot (axes?) in Matplotlib?

If I do Ctrl-F "size" in https://matplotlib.org/3.1.1/api/axes_api.html - there is only one match, in context: "... with varying marker size and/or ...", so it does not really tell me how to find the size of the axes.

Say, I have the same code as in Interactively resize figure and toggle plot visibility in Matplotlib?

#!/usr/bin/env python3

import matplotlib
print("matplotlib.__version__ {}".format(matplotlib.__version__))
import matplotlib.pyplot as plt
import numpy as np

default_size_inch = (9, 6)
showThird = False

def onpress(event):
  global fig, ax1, ax2, ax3, showThird
  if event.key == 'x':
    showThird = not showThird
    if showThird:
      fig.set_size_inches(default_size_inch[0]+3, default_size_inch[1], forward=True)
      plt.subplots_adjust(right=0.85) # leave a bit of space on the right
      ax3.set_visible(True)
      ax3.set_axis_on()
    else:
      fig.set_size_inches(default_size_inch[0], default_size_inch[1], forward=True)
      plt.subplots_adjust(right=0.9) # default
      ax3.set_visible(False)
      ax3.set_axis_off()
    fig.canvas.draw()


def main():
  global fig, ax1, ax2, ax3
  xdata = np.arange(0, 101, 1) # 0 to 100, both included
  ydata1 = np.sin(0.01*xdata*np.pi/2)
  ydata2 = 10*np.sin(0.01*xdata*np.pi/4)

  fig = plt.figure(figsize=default_size_inch, dpi=120)
  ax1 = plt.subplot2grid((3,3), (0,0), colspan=2, rowspan=2)
  ax2 = plt.subplot2grid((3,3), (2,0), colspan=2, sharex=ax1)
  ax3 = plt.subplot2grid((3,3), (0,2), rowspan=3)

  ax3.set_visible(False)
  ax3.set_axis_off()

  ax1.plot(xdata, ydata1, color="Red")
  ax2.plot(xdata, ydata2, color="Khaki")

  fig.canvas.mpl_connect('key_press_event', lambda event: onpress(event))
  plt.show()


# ENTRY POINT
if __name__ == '__main__':
  main()

How do I find the size of the subplots represented by ax1 and ax2 axes?

4
  • 1
    Matplotlib figures have many coordinate systems. In which coordinate system would you like to know the size of the axis? Commented Nov 22, 2019 at 12:58
  • Thanks @PaulBrodersen - I guess, I'd want both the "relative" (i.e. "figure") and the "absolute" (that is, "figure-inches") in terms of Figure, as I'd like to have that info for further scaling/resizing of the subplots. Commented Nov 22, 2019 at 13:57
  • 5
    ax.get_position() gives the position in relative figure coordinates. Divide those numbers by the figure size in inches (fig.get_size_inches()) to get the position in inches. Commented Nov 22, 2019 at 14:12
  • stackoverflow.com/questions/19306510/… Does this help? Commented Feb 25, 2021 at 18:59

1 Answer 1

4

For the full explanation of how bbox works refer to here. Each of your axes object fits in a bounding box. All you need to do is to get the height and width of your axis bounding box. ax_h, ax_w = ax.bbox.height, ax.bbox.width

You can transform to figure coordinates by using bbox.transformed method. For example: ax_h = ax.bbox.transformed(fig.gca().transAxes).height

Sign up to request clarification or add additional context in comments.

1 Comment

Please give an explanation of how your answer works and what exactly you have done. This encourages OP to copy and paste, without understanding what the solution was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.