@@ -921,7 +921,7 @@ def subplot(*args, **kwargs):
921
921
922
922
923
923
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 ):
925
925
"""
926
926
Create a figure with a set of subplots already made.
927
927
@@ -981,6 +981,11 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
981
981
:meth:`~matplotlib.figure.Figure.add_subplot` call used to
982
982
create each subplots.
983
983
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
+
984
989
*fig_kw* : dict
985
990
Dict with keywords passed to the :func:`figure` call. Note that all
986
991
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,
1055
1060
(sharey , share_values ))
1056
1061
if subplot_kw is None :
1057
1062
subplot_kw = {}
1063
+ if gridspec_kw is None :
1064
+ gridspec_kw = {}
1058
1065
1059
1066
fig = figure (** fig_kw )
1067
+ gs = GridSpec (nrows , ncols , ** gridspec_kw )
1060
1068
1061
1069
# Create empty object array to hold all axes. It's easiest to make it 1-d
1062
1070
# so we can just append subplots upon creation, and then
1063
1071
nplots = nrows * ncols
1064
1072
axarr = np .empty (nplots , dtype = object )
1065
1073
1066
1074
# 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 )
1068
1076
#if sharex:
1069
1077
# subplot_kw['sharex'] = ax0
1070
1078
#if sharey:
@@ -1094,7 +1102,7 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
1094
1102
subplot_kw ['sharey' ] = None
1095
1103
else :
1096
1104
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 )
1098
1106
1099
1107
# returned axis array will be always 2-d, even if nrows=ncols=1
1100
1108
axarr = axarr .reshape (nrows , ncols )
0 commit comments