mpl.pyplot.subplots()¶
-
mpl.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)[source]¶ Create a figure with a set of subplots already made.
This utility wrapper makes it convenient to create common layouts of subplots, including the enclosing figure object, in a single call.
Keyword arguments:
- nrows : int
- Number of rows of the subplot grid. Defaults to 1.
- ncols : int
- Number of columns of the subplot grid. Defaults to 1.
- sharex : string or bool
- If True, the X axis will be shared amongst all subplots. If True and you have multiple rows, the x tick labels on all but the last row of plots will have visible set to False If a string must be one of “row”, “col”, “all”, or “none”. “all” has the same effect as True, “none” has the same effect as False. If “row”, each subplot row will share a X axis. If “col”, each subplot column will share a X axis and the x tick labels on all but the last row will have visible set to False.
- sharey : string or bool
- If True, the Y axis will be shared amongst all subplots. If True and you have multiple columns, the y tick labels on all but the first column of plots will have visible set to False If a string must be one of “row”, “col”, “all”, or “none”. “all” has the same effect as True, “none” has the same effect as False. If “row”, each subplot row will share a Y axis and the y tick labels on all but the first column will have visible set to False. If “col”, each subplot column will share a Y axis.
- squeeze : bool
If True, extra dimensions are squeezed out from the returned axis object:
- if only one subplot is constructed (nrows=ncols=1), the resulting single Axis object is returned as a scalar.
- for Nx1 or 1xN subplots, the returned object is a 1-d numpy object array of Axis objects are returned as numpy 1-d arrays.
- for NxM subplots with N>1 and M>1 are returned as a 2d array.
If False, no squeezing at all is done: the returned axis object is always a 2-d array containing Axis instances, even if it ends up being 1x1.
- subplot_kw : dict
- Dict with keywords passed to the
add_subplot()call used to create each subplots. - gridspec_kw : dict
- Dict with keywords passed to the
GridSpecconstructor used to create the grid the subplots are placed on. - fig_kw : dict
- Dict with keywords passed to the
figure()call. Note that all keywords not recognized above will be automatically included here.
Returns:
fig, ax : tuple
- fig is the
matplotlib.figure.Figureobject - ax can be either a single axis object or an array of axis objects if more than one subplot was created. The dimensions of the resulting array can be controlled with the squeeze keyword, see above.
Examples:
x = np.linspace(0, 2*np.pi, 400) y = np.sin(x**2) # Just a figure and one subplot f, ax = plt.subplots() ax.plot(x, y) ax.set_title('Simple plot') # Two subplots, unpack the output array immediately f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.plot(x, y) ax1.set_title('Sharing Y axis') ax2.scatter(x, y) # Four polar axes plt.subplots(2, 2, subplot_kw=dict(polar=True)) # Share a X axis with each column of subplots plt.subplots(2, 2, sharex='col') # Share a Y axis with each row of subplots plt.subplots(2, 2, sharey='row') # Share a X and Y axis with all subplots plt.subplots(2, 2, sharex='all', sharey='all') # same as plt.subplots(2, 2, sharex=True, sharey=True)