8000 FIX · matplotlib/matplotlib@4fb120c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fb120c

Browse files
committed
FIX
1 parent 50503cc commit 4fb120c

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

lib/matplotlib/gridspec.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,29 @@ def get_grid_positions(self, fig, raw=False):
202202
fig_lefts, fig_rights = (left + cell_ws).reshape((-1, 2)).T
203203
return fig_bottoms, fig_tops, fig_lefts, fig_rights
204204

205+
@staticmethod
206+
def _check_gridspec_exists(figure, nrows, ncols):
207+
"""
208+
Check if the figure already has a gridspec with these dimensions...
209+
"""
210+
gs = None
211+
for ax in figure.get_axes():
212+
if hasattr(ax, 'get_subplotspec'):
213+
ggs = ax.get_subplotspec().get_gridspec()
214+
if hasattr(ggs, 'get_topmost_subplotspec'):
215+
# This is needed for colorbar gridspec layouts.
216+
# This is probably OK becase this whole logic tree
217+
# is for when the user is doing simple things with the
218+
# add_subplot command. Complicated stuff, the proper
219+
# gridspec is passed in...
220+
ggs = ggs.get_topmost_subplotspec().get_gridspec()
221+
222+
(nrow, ncol) = ggs.get_geometry()
223+
if nrow == nrows and ncol == ncols:
224+
gs = ggs
225+
return gs
226+
227+
205228
def __getitem__(self, key):
206229
"""Create and return a `.SubplotSpec` instance."""
207230
nrows, ncols = self.get_geometry()
@@ -690,21 +713,11 @@ def _from_subplot_args(figure, args):
690713
else:
691714
raise TypeError(f"subplot() takes 1 or 3 positional arguments but "
692715
f"{len(args)} were given")
693-
for ax in figure.get_axes():
694-
if hasattr(ax, 'get_subplotspec'):
695-
gs = ax.get_subplotspec().get_gridspec()
696-
if hasattr(gs, 'get_topmost_subplotspec'):
697-
# This is needed for colorbar gridspec layouts.
698-
# This is probably OK becase this whole logic tree
699-
# is for when the user is doing simple things with the
700-
# add_subplot command. Complicated stuff, the proper
701-
# gridspec is passed in...
702-
gs = gs.get_topmost_subplotspec().get_gridspec()
703716

704-
(nrow, ncol) = gs.get_geometry()
705-
if nrow == rows and ncol == cols:
706-
return gs[i-1:j]
707-
return GridSpec(rows, cols, figure=figure)[i-1:j]
717+
gs = GridSpec._check_gridspec_exists(figure, rows, cols)
718+
if gs is None:
719+
gs = GridSpec(rows, cols, figure=figure)
720+
return gs[i-1:j]
708721

709722

710723
# num2 is a property only to handle the case where it is None and someone

lib/matplotlib/pyplot.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,24 +1311,10 @@ def subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs):
13111311
if fig is None:
13121312
fig = gcf()
13131313

1314-
s1, s2 = shape
1315-
gs = None
1316-
for ax in fig.get_axes():
1317-
if hasattr(ax, 'get_subplotspec'):
1318-
ggs = ax.get_subplotspec().get_gridspec()
1319-
if hasattr(ggs, 'get_topmost_subplotspec'):
1320-
# This is needed for colorbar gridspec layouts.
1321-
# This is probably OK becase this whole logic tree
1322-
# is for when the user is doing simple things with the
1323-
# add_subplot command. Complicated stuff, the proper
1324-
# gridspec is passed in...
1325-
ggs = ggs.get_topmost_subplotspec().get_gridspec()
1326-
1327-
(nrow, ncol) = ggs.get_geometry()
1328-
if nrow == s1 and ncol == s2:
1329-
gs = ggs
1314+
rows, cols = shape
1315+
gs = GridSpec._check_gridspec_exists(fig, rows, cols)
13301316
if gs is None:
1331-
gs = GridSpec(s1, s2, figure=fig)
1317+
gs = GridSpec(rows, cols, figure=fig)
13321318

13331319
subplotspec = gs.new_subplotspec(loc, rowspan=rowspan, colspan=colspan)
13341320
ax = fig.add_subplot(subplotspec, **kwargs)

0 commit comments

Comments
 (0)
0