Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ def subplot(*args, **kwargs):


def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
subplot_kw=None, gridspec_kw=None, **fig_kw):
subplot_kw=None, gridspec_kw=None, **kwargs):
"""
Create a figure and a set of subplots.

Expand Down Expand Up @@ -1082,9 +1082,11 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
Dict with keywords passed to the `~matplotlib.gridspec.GridSpec`
constructor used to create the grid the subplots are placed on.

**fig_kw
All additional keyword arguments are passed to the
`.pyplot.figure` call.
**kwargs
Keyword arguements that are "projections", "polar", or "label"
are passed to the `~matplotlib.figure.Figure.add_subplot` call used
to create each subplot. All additional keyword arguments are
passed to the `.pyplot.figure` call.

Returns
-------
Expand Down Expand Up @@ -1132,7 +1134,7 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
ax2.scatter(x, y)

# Create four polar axes and access them through the returned array
fig, axs = plt.subplots(2, 2, subplot_kw=dict(polar=True))
fig, axs = plt.subplots(2, 2, polar=True)
axs[0, 0].plot(x, y)
axs[1, 1].scatter(x, y)

Expand Down Expand Up @@ -1161,7 +1163,17 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
.Figure.add_subplot

"""
fig = figure(**fig_kw)
if subplot_kw is None:
subplot_kw = {}
for arg in ["projection", "polar", "label"]:
if arg in kwargs:
if arg in subplot_kw:
raise TypeError("subplots() got multiple values for "
"keyword argument '{}'".format(arg))
subplot_kw[arg] = kwargs[arg]
kwargs.pop(arg)

fig = figure(**kwargs)
axs = fig.subplots(nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey,
squeeze=squeeze, subplot_kw=subplot_kw,
gridspec_kw=gridspec_kw)
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,8 @@ def test_dont_mutate_kwargs():
gridspec_kw=gridspec_kw)
assert subplot_kw == {'sharex': 'all'}
assert gridspec_kw == {'width_ratios': [1, 2]}


def test_combine_subplot_kw_fig_kw():
fig1, axs1 = plt.subplots(2, 2, polar=True)
assert type(axs1[0][0]).__name__ == "PolarAxesSubplot"