Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e16ab73

Browse files
committed
FIX
1 parent faad499 commit e16ab73

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
@@ -197,6 +197,29 @@ def get_grid_positions(self, fig, raw=False):
197197
fig_lefts, fig_rights = (left + cell_ws).reshape((-1, 2)).T
198198
return fig_bottoms, fig_tops, fig_lefts, fig_rights
199199

200+
@staticmethod
201+
def _check_gridspec_exists(figure, nrows, ncols):
202+
"""
203+
Check if the figure already has a gridspec with these dimensions...
204+
"""
205+
gs = None
206+
for ax in figure.get_axes():
207+
if hasattr(ax, 'get_subplotspec'):
208+
ggs = ax.get_subplotspec().get_gridspec()
209+
if hasattr(ggs, 'get_topmost_subplotspec'):
210+
# This is needed for colorbar gridspec layouts.
211+
# This is probably OK becase this whole logic tree
212+
# is for when the user is doing simple things with the
213+
# add_subplot command. Complicated stuff, the proper
214+
# gridspec is passed in...
215+
ggs = ggs.get_topmost_subplotspec().get_gridspec()
216+
217+
(nrow, ncol) = ggs.get_geometry()
218+
if nrow == nrows and ncol == ncols:
219+
gs = ggs
220+
return gs
221+
222+
200223
def __getitem__(self, key):
201224
"""Create and return a `.SubplotSpec` instance."""
202225
nrows, ncols = self.get_geometry()
@@ -558,21 +581,11 @@ def _from_subplot_args(figure, args):
558581
else:
559582
raise TypeError(f"subplot() takes 1 or 3 positional arguments but "
560583
f"{len(args)} were given")
561-
for ax in figure.get_axes():
562-
if hasattr(ax, 'get_subplotspec'):
563-
gs = ax.get_subplotspec().get_gridspec()
564-
if hasattr(gs, 'get_topmost_subplotspec'):
565-
# This is needed for colorbar gridspec layouts.
566-
# This is probably OK becase this whole logic tree
567-
# is for when the user is doing simple things with the
568-
# add_subplot command. Complicated stuff, the proper
569-
# gridspec is passed in...
570-
gs = gs.get_topmost_subplotspec().get_gridspec()
571584

572-
(nrow, ncol) = gs.get_geometry()
573-
if nrow == rows and ncol == cols:
574-
return gs[ii-1:jj]
575-
return GridSpec(rows, cols, figure=figure)[ii-1:jj]
585+
gs = GridSpec._check_gridspec_exists(figure, rows, cols)
586+
if gs is None:
587+
gs = GridSpec(rows, cols, figure=figure)
588+
return gs[ii-1:jj]
576589

577590

578591
# 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
@@ -1237,24 +1237,10 @@ def subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs):
12371237
if fig is None:
12381238
fig = gcf()
12391239

1240-
s1, s2 = shape
1241-
gs = None
1242-
for ax in fig.get_axes():
1243-
if hasattr(ax, 'get_subplotspec'):
1244-
ggs = ax.get_subplotspec().get_gridspec()
1245-
if hasattr(ggs, 'get_topmost_subplotspec'):
1246-
# This is needed for colorbar gridspec layouts.
1247-
# This is probably OK becase this whole logic tree
1248-
# is for when the user is doing simple things with the
1249-
# add_subplot command. Complicated stuff, the proper
1250-
# gridspec is passed in...
1251-
ggs = ggs.get_topmost_subplotspec().get_gridspec()
1252-
1253-
(nrow, ncol) = ggs.get_geometry()
1254-
if nrow == s1 and ncol == s2:
1255-
gs = ggs
1240+
rows, cols = shape
1241+
gs = GridSpec._check_gridspec_exists(fig, rows, cols)
12561242
if gs is None:
1257-
gs = GridSpec(s1, s2, figure=fig)
1243+
gs = GridSpec(rows, cols, figure=fig)
12581244

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

0 commit comments

Comments
 (0)