8000 DOC improved subplots' docstring · matplotlib/matplotlib@da9829f · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit da9829f

Browse files
committed
DOC improved subplots' docstring
- the dostring is now in numpydoc format. - added an extra line in the examples to showcase accessing the axes returned array closes #7230
1 parent ed8138e commit da9829f

File tree

1 file changed

+95
-85
lines changed

1 file changed

+95
-85
lines changed

lib/matplotlib/pyplot.py

Lines changed: 95 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,115 +1038,125 @@ def subplot(*args, **kwargs):
10381038

10391039

10401040
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1041-
subplot_kw=None, gridspec_kw=None, **fig_kw):
1041+
subplot_kw=None, gridspec_kw=None, **fig_kw):
10421042
"""
10431043
Create a figure with a set of subplots already made.
10441044
10451045
This utility wrapper makes it convenient to create common layouts of
10461046
subplots, including the enclosing figure object, in a single call.
10471047
1048-
Keyword arguments:
1049-
1050-
*nrows* : int
1051-
Number of rows of the subplot grid. Defaults to 1.
1052-
1053-
*ncols* : int
1054-
Number of columns of the subplot grid. Defaults to 1.
1055-
1056-
*sharex* : string or bool
1057-
If *True*, the X axis will be shared amongst all subplots. If
1058-
*True* and you have multiple rows, the x tick labels on all but
1059-
the last row of plots will have visible set to *False*
1060-
If a string must be one of "row", "col", "all", or "none".
1061-
"all" has the same effect as *True*, "none" has the same effect
1062-
as *False*.
1063-
If "row", each subplot row will share a X axis.
1064-
If "col", each subplot column will share a X axis and the x tick
1065-
labels on all but the last row will have visible set to *False*.
1066-
1067-
*sharey* : string or bool
1068-
If *True*, the Y axis will be shared amongst all subplots. If
1069-
*True* and you have multiple columns, the y tick labels on all but
1070-
the first column of plots will have visible set to *False*
1071-
If a string must be one of "row", "col", "all", or "none".
1072-
"all" has the same effect as *True*, "none" has the same effect
1073-
as *False*.
1074-
If "row", each subplot row will share a Y axis and the y tick
1075-
labels on all but the first column will have visible set to *False*.
1076-
If "col", each subplot column will share a Y axis.
1077-
1078-
*squeeze* : bool
1079-
If *True*, extra dimensions are squeezed out from the
1080-
returned axis object:
1081-
1082-
- if only one subplot is constructed (nrows=ncols=1), the
1083-
resulting single Axis object is returned as a scalar.
1084-
1085-
- for Nx1 or 1xN subplots, the returned object is a 1-d numpy
1086-
object array of Axis objects are returned as numpy 1-d
1087-
arrays.
1088-
1089-
- for NxM subplots with N>1 and M>1 are returned as a 2d
1090-
array.
1091-
1092-
If *False*, no squeezing at all is done: the returned axis
1093-
object is always a 2-d array containing Axis instances, even if it
1094-
ends up being 1x1.
1095-
1096-
*subplot_kw* : dict
1048+
Parameters
1049+
----------
1050+
nrows : int, optional, default: 1
1051+
Number of rows of the subplot grid.
1052+
1053+
ncols : int, optional, default: 1
1054+
Number of columns of the subplot grid.
1055+
1056+
sharex : bool or string, optional, default: False
1057+
- If True, the X axis will be shared amongst all subplots. Note that
1058+
if nrows > 1 then the x tick labels won't be displayed on any of
1059+
plots but the ones on the bottom row.
1060+
- If False, no axis will be shared amongst subplots.
1061+
- If a string must be one of "row", "col", "all", or "none".
1062+
- "all" has the same effect as True.
1063+
- "none" has the same effect as False.
1064+
- If "row", each subplot row will share a X axis.
1065+
- If "col", each subplot column will share a X axis. Note that if
1066+
nrows > 1 then the x tick labels won't be displayed on any of
1067+
the plots but the ones on the bottom row.
1068+
1069+
sharey : bool or string, optional, default: False
1070+
- If True, the Y axis will be shared amongst all subplots. Note that
1071+
if ncols > 1 then the y tick labels won't be displayed on any of
1072+
plots but the ones on first column.
1073+
- If False, no y-axis will be shared amongst subplots.
1074+
- If a string must be one of "row", "col", "all", or "none".
1075+
- "all" has the same effect as True.
1076+
- "none" has the same effect as False.
1077+
- If "row", each subplot row will share a y-axis.
1078+
- If "col", each subplot column will share a y-axis. Note that if
1079+
ncols > 1 then the y tick labels won't be displayed on any of
1080+
the plots but the ones on the first column.
1081+
1082+
squeeze : bool, optional, default: True
1083+
- If True, extra dimensions are squeezed out from the returned axis
1084+
object:
1085+
1086+
- if only one subplot is constructed (nrows=ncols=1), the
1087+
resulting single Axis object is returned as a scalar.
1088+
- for Nx1 or 1xN subplots, the returned object is a 1-d numpy
1089+
object array of Axis objects are returned as numpy 1-d arrays.
1090+
- for NxM subplots with N>1 and M>1 are returned as a 2d arrays.
1091+
1092+
- If False, no squeezing at all is done: the returned axis object is
1093+
always a 2-d array containing Axis instances, even if it ends up
1094+
being 1x1.
1095+
1096+
subplot_kw : dict, optional
10971097
Dict with keywords passed to the
1098-
:meth:`~matplotlib.figure.Figure.add_subplot` call used to
1099-
create each subplots.
1098+
:meth:`~matplotlib.figure.Figure.add_subplot` call used to create each
1099+
subplot.
11001100
1101-
*gridspec_kw* : dict
1101+
gridspec_kw : dict, optional
11021102
Dict with keywords passed to the
1103-
:class:`~matplotlib.gridspec.GridSpec` constructor used to create
1104-
the grid the subplots are placed on.
1103+
:class:`~matplotlib.gridspec.GridSpec` constructor used to create the
1104+
grid the subplots are placed on.
11051105
1106-
*fig_kw* : dict
1106+
fig_kw : dict, optional
11071107
Dict with keywords passed to the :func:`figure` call. Note that all
11081108
keywords not recognized above will be automatically included here.
11091109
1110-
Returns:
1110+
Returns
1111+
-------
1112+
fig : :class:`matplotlib.figure.Figure` object
11111113
1112-
fig, ax : tuple
1114+
ax : axis or array of axis objects.
1115+
ax can be either a single axis object or an array of axis objects if
1116+
more than one subplot was created. The dimensions of the resulting
1117+
array can be controlled with the squeeze keyword, see above.
11131118
1114-
- *fig* is the :class:`matplotlib.figure.Figure` object
1119+
Examples
1120+
--------
1121+
First create some toy data:
11151122
1116-
- *ax* can be either a single axis object or an array of axis
1117-
objects if more than one subplot was created. The dimensions
1118-
of the resulting array can be controlled with the squeeze
1119-
keyword, see above.
1123+
>>> x = np.linspace(0, 2*np.pi, 400)
1124+
>>> y = np.sin(x**2)
11201125
1121-
Examples::
1126+
Creates just a figure and only one subplot.
1127+
1128+
>>> fig, ax = plt.subplots()
1129+
>>> ax.plot(x, y)
1130+
>>> ax.set_title('Simple plot')
1131+
1132+
Creates two subplots and unpacks the output array immediately
1133+
1134+
>>> f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
1135+
>>> ax1.plot(x, y)
1136+
>>> ax1.set_title('Sharing Y axis')
1137+
>>> ax2.scatter(x, y)
1138+
1139+
Creates four polar axes, and accesses them through the returned array
1140+
1141+
>>> fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
1142+
>>> axes[0, 0].plot(x, y)
1143+
>>> axes[1, 1].scatter(x, y)
1144+
1145+
Share a X axis with each column of subplots
11221146
1123-
x = np.linspace(0, 2*np.pi, 400)
1124-
y = np.sin(x**2)
1147+
>>> plt.subplots(2, 2, sharex='col')
11251148
1126-
# Just a figure and one subplot
1127-
f, ax = plt.subplots()
1128-
ax.plot(x, y)
1129-
ax.set_title('Simple plot')
1149+
Share a Y axis with each row of subplots
11301150
1131-
# Two subplots, unpack the output array immediately
1132-
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
1133-
ax1.plot(x, y)
1134-
ax1.set_title('Sharing Y axis')
1135-
ax2.scatter(x, y)
1151+
>>> plt.subplots(2, 2, sharey='row')
11361152
1137-
# Four polar axes
1138-
plt.subplots(2, 2, subplot_kw=dict(polar=True))
1153+
Share a X and Y axis with all subplots
11391154
1140-
# Share a X axis with each column of subplots
1141-
plt.subplots(2, 2, sharex='col')
1155+
>>> plt.subplots(2, 2, sharex='all', sharey='all')
11421156
1143-
# Share a Y axis with each row of subplots
1144-
plt.subplots(2, 2, sharey='row')
1157+
Note that this is the same as
11451158
1146-
# Share a X and Y axis with all subplots
1147-
plt.subplots(2, 2, sharex='all', sharey='all')
1148-
# same as
1149-
plt.subplots(2, 2, sharex=True, sharey=True)
1159+
>>> plt.subplots(2, 2, sharex=True, sharey=True)
11501160
"""
11511161
fig = figure(**fig_kw)
11521162
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,

0 commit comments

Comments
 (0)
0