E.Z.
E.Z.

Reputation: 6661

Matplotlib: What's the meaning of the 3-digit parameter given to Figure.add_subplot()?

When I add a new plot to a figure using matplotlib, I'm always using something like fig.add_subplot(111) (as seen in may online examples).

The documentation on add_subplot() doesn't mention anything about this 3-digit parameter, other than showing it in an example (without much explanation).

Any idea how this should be used and/or where I could find some more information about this?

Upvotes: 2

Views: 2969

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

Its the way to define where your new axes will be placed within the figure. The first digit means the amount of rows, the second the amount of columns. The figure will be equally divided based on the number of rows and columns you specify. The last digit will then 'pick' one of those places and return the axes for it. The number goes from the top-left to the bottom-right.

So fig.add_subplots(2,2,1) will create a grid of 2 by 2 and return an axes for the top-left area. Note that even though you define a 2 by 2 grid, only the axes you specify is actually created.

For example:

fig = plt.figure()

# a 2x2 grid and create the fourth (bottom-right) one
ax1 = fig.add_subplot(2,2,4)
ax1.text(.5,.5,'ax1')

# a 2x1 grid and create the top one
ax2 = fig.add_subplot(2,1,1)
ax2.text(.5,.5,'ax2')

enter image description here

Upvotes: 4

Related Questions