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

Skip to content

Commit 658bbd5

Browse files
committed
Make SubplotSpec.num2 never None.
SubplotSpec.num2 == None is handled as if num2 was set to num1 (a subplot spanning a single entry of a GridSpec). Instead of having a bunch of checks everywhere for that, just hide None behind a property.
1 parent d12b8f4 commit 658bbd5

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ def _make_ghost_gridspec_slots(fig, gs):
245245
axs += [ax]
246246
for ax in axs:
247247
ss0 = ax.get_subplotspec()
248-
if ss0.num2 is None:
249-
ss0.num2 = ss0.num1
250248
hassubplotspec[ss0.num1:(ss0.num2 + 1)] = True
251249
for nn, hss in enumerate(hassubplotspec):
252250
if not hss:
@@ -342,8 +340,6 @@ def _align_spines(fig, gs):
342340

343341
for n, ax in enumerate(axs):
344342
ss0 = ax.get_subplotspec()
345-
if ss0.num2 is None:
346-
ss0.num2 = ss0.num1
347343
rownummin[n], colnummin[n] = divmod(ss0.num1, ncols)
348344
rownummax[n], colnummax[n] = divmod(ss0.num2, ncols)
349345
width[n] = np.sum(
@@ -483,16 +479,12 @@ def _arrange_subplotspecs(gs, hspace=0, wspace=0):
483479
for child0 in sschildren:
484480
ss0 = child0.artist
485481
nrows, ncols = ss0.get_gridspec().get_geometry()
486-
if ss0.num2 is None:
487-
ss0.num2 = ss0.num1
488482
rowNum0min, colNum0min = divmod(ss0.num1, ncols)
489483
rowNum0max, colNum0max = divmod(ss0.num2, ncols)
490484
sschildren = sschildren[1:]
491485
for childc in sschildren:
492486
ssc = childc.artist
493487
rowNumCmin, colNumCmin = divmod(ssc.num1, ncols)
494-
if ssc.num2 is None:
495-
ssc.num2 = ssc.num1
496488
rowNumCmax, colNumCmax = divmod(ssc.num2, ncols)
497489
# OK, this tells us the relative layout of ax
498490
# with axc

lib/matplotlib/_layoutbox.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -438,24 +438,13 @@ def layout_from_subplotspec(self, subspec,
438438
figLefts = [left + cellWs[2 * colNum] for colNum in range(ncols)]
439439
figRights = [left + cellWs[2 * colNum + 1] for colNum in range(ncols)]
440440

441-
rowNum, colNum = divmod(subspec.num1, ncols)
442-
figBottom = figBottoms[rowNum]
443-
figTop = figTops[rowNum]
444-
figLeft = figLefts[colNum]
445-
figRight = figRights[colNum]
446-
447-
if subspec.num2 is not None:
448-
449-
rowNum2, colNum2 = divmod(subspec.num2, ncols)
450-
figBottom2 = figBottoms[rowNum2]
451-
figTop2 = figTops[rowNum2]
452-
figLeft2 = figLefts[colNum2]
453-
figRight2 = figRights[colNum2]
454-
455-
figBottom = min(figBottom, figBottom2)
456-
figLeft = min(figLeft, figLeft2)
457-
figTop = max(figTop, figTop2)
458-
figRight = max(figRight, figRight2)
441+
rowNum1, colNum1 = divmod(subspec.num1, ncols)
442+
rowNum2, colNum2 = divmod(subspec.num2, ncols)
443+
figBottom = min(figBottoms[rowNum1], figBottoms[rowNum2])
444+
figTop = max(figTops[rowNum1], figTops[rowNum2])
445+
figLeft = min(figLefts[colNum1], figLefts[colNum2])
446+
figRight = max(figRights[colNum1], figRights[colNum2])
447+
459448
# These are numbers relative to 0,0,1,1. Need to constrain
460449
# relative to parent.
461450

lib/matplotlib/gridspec.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def __init__(self, gridspec, num1, num2=None):
410410
"""
411411
The subplot will occupy the num1-th cell of the given
412412
gridspec. If num2 is provided, the subplot will span between
413-
num1-th cell and num2-th cell.
413+
num1-th cell and num2-th cell *inclusive*.
414414
415415
The index starts from 0.
416416
"""
@@ -430,6 +430,17 @@ def __init__(self, gridspec, num1, num2=None):
430430
else:
431431
self._layoutbox = None
432432

433+
# num2 is a property only to handle the case where it is None and someone
434+
# mutates num1.
435+
436+
@property
437+
def num2(self):
438+
return self.num1 if self._num2 is None else self._num2
439+
440+
@num2.setter
441+
def num2(self, value):
442+
self._num2 = value
443+
433444
def __getstate__(self):
434445
state = self.__dict__
435446
try:
@@ -464,21 +475,15 @@ def get_rows_columns(self):
464475
gridspec = self.get_gridspec()
465476
nrows, ncols = gridspec.get_geometry()
466477
row_start, col_start = divmod(self.num1, ncols)
467-
if self.num2 is not None:
468-
row_stop, col_stop = divmod(self.num2, ncols)
469-
else:
470-
row_stop = row_start
471-
col_stop = col_start
478+
row_stop, col_stop = divmod(self.num2, ncols)
472479
return nrows, ncols, row_start, row_stop, col_start, col_stop
473480

474481
def get_position(self, figure, return_all=False):
475482
"""Update the subplot position from ``figure.subplotpars``.
476483
"""
477484
gridspec = self.get_gridspec()
478485
nrows, ncols = gridspec.get_geometry()
479-
rows, cols = np.unravel_index(
480-
[self.num1] if self.num2 is None else [self.num1, self.num2],
481-
(nrows, ncols))
486+
rows, cols = np.unravel_index([self.num1, self.num2], (nrows, ncols))
482487
fig_bottoms, fig_tops, fig_lefts, fig_rights = \
483488
gridspec.get_grid_positions(figure)
484489

0 commit comments

Comments
 (0)