Skip to content

Commit 673dbb8

Browse files
committed
Merge pull request #2647 from rwirth/subplots_gridspec
use GridSpec in plt.subplots
2 parents fc07202 + 9bfb3e0 commit 673dbb8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/matplotlib/pyplot.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def subplot(*args, **kwargs):
921921

922922

923923
def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
924-
subplot_kw=None, **fig_kw):
924+
subplot_kw=None, gridspec_kw=None, **fig_kw):
925925
"""
926926
Create a figure with a set of subplots already made.
927927
@@ -981,6 +981,11 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
981981
:meth:`~matplotlib.figure.Figure.add_subplot` call used to
982982
create each subplots.
983983
984+
*gridspec_kw* : dict
985+
Dict with keywords passed to the
986+
:class:`~matplotlib.gridspec.GridSpec` constructor used to create
987+
the grid the subplots are placed on.
988+
984989
*fig_kw* : dict
985990
Dict with keywords passed to the :func:`figure` call. Note that all
986991
keywords not recognized above will be automatically included here.
@@ -1055,16 +1060,19 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
10551060
(sharey, share_values))
10561061
if subplot_kw is None:
10571062
subplot_kw = {}
1063+
if gridspec_kw is None:
1064+
gridspec_kw = {}
10581065

10591066
fig = figure(**fig_kw)
1067+
gs = GridSpec(nrows, ncols, **gridspec_kw)
10601068

10611069
# Create empty object array to hold all axes. It's easiest to make it 1-d
10621070
# so we can just append subplots upon creation, and then
10631071
nplots = nrows*ncols
10641072
axarr = np.empty(nplots, dtype=object)
10651073

10661074
# Create first subplot separately, so we can share it if requested
1067-
ax0 = fig.add_subplot(nrows, ncols, 1, **subplot_kw)
1075+
ax0 = fig.add_subplot(gs[0, 0], **subplot_kw)
10681076
#if sharex:
10691077
# subplot_kw['sharex'] = ax0
10701078
#if sharey:
@@ -1094,7 +1102,7 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
10941102
subplot_kw['sharey'] = None
10951103
else:
10961104
subplot_kw['sharey'] = axarr[sys[i]]
1097-
axarr[i] = fig.add_subplot(nrows, ncols, i + 1, **subplot_kw)
1105+
axarr[i] = fig.add_subplot(gs[i // ncols, i % ncols], **subplot_kw)
10981106

10991107
# returned axis array will be always 2-d, even if nrows=ncols=1
11001108
axarr = axarr.reshape(nrows, ncols)

0 commit comments

Comments
 (0)