python – In Matplotlib, what does the argument mean in fig.add_subplot(111)?

python – In Matplotlib, what does the argument mean in fig.add_subplot(111)?

I think this would be best explained by the following picture:

enter

To initialize the above, one would type:

import matplotlib.pyplot as plt
fig = plt.figure()
fig.add_subplot(221)   #top left
fig.add_subplot(222)   #top right
fig.add_subplot(223)   #bottom left
fig.add_subplot(224)   #bottom right 
plt.show()

These are subplot grid parameters encoded as a single integer. For example, 111 means 1×1 grid, first subplot and 234 means 2×3 grid, 4th subplot.

Alternative form for add_subplot(111) is add_subplot(1, 1, 1).

python – In Matplotlib, what does the argument mean in fig.add_subplot(111)?

The answer from Constantin is spot on but for more background this behavior is inherited from Matlab.

The Matlab behavior is explained in the Figure Setup – Displaying Multiple Plots per Figure section of the Matlab documentation.

subplot(m,n,i) breaks the figure window into an m-by-n matrix of small
subplots and selects the ithe subplot for the current plot. The plots
are numbered along the top row of the figure window, then the second
row, and so forth.

Leave a Reply

Your email address will not be published. Required fields are marked *