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

Skip to content

Commit fb8ddd1

Browse files
committed
Normalize gridspec ratios to lists in the setter.
This avoids having to check for `ratios == None` every time someone actually accesses the ratios. I just deleted the never used and private `vstackeq` and `hstackeq` instead of changing their signatures to make height_ratios/width_ratios non-optional, given that they should be simple enough to bring back if needed.
1 parent 100c7cf commit fb8ddd1

File tree

3 files changed

+14
-36
lines changed

3 files changed

+14
-36
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,6 @@ def _align_spines(fig, gs):
315315
nrows, ncols = gs.get_geometry()
316316
width_ratios = gs.get_width_ratios()
317317
height_ratios = gs.get_height_ratios()
318-
if width_ratios is None:
319-
width_ratios = np.ones(ncols)
320-
if height_ratios is None:
321-
height_ratios = np.ones(nrows)
322318

323319
# get axes in this gridspec....
324320
axs = [ax for ax in fig.axes

lib/matplotlib/_layoutbox.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -499,38 +499,22 @@ def vpack(boxes, padding=0, strength='strong'):
499499
boxes[i].solver.addConstraint(c | strength)
500500

501501

502-
def match_heights(boxes, height_ratios=None, strength='medium'):
502+
def match_heights(boxes, height_ratios, strength='medium'):
503503
"""Stack LayoutBox instances from top to bottom."""
504-
505-
if height_ratios is None:
506-
height_ratios = np.ones(len(boxes))
507504
for i in range(1, len(boxes)):
508505
c = (boxes[i-1].height ==
509506
boxes[i].height*height_ratios[i-1]/height_ratios[i])
510507
boxes[i].solver.addConstraint(c | strength)
511508

512509

513-
def match_widths(boxes, width_ratios=None, strength='medium'):
510+
def match_widths(boxes, width_ratios, strength='medium'):
514511
"""Stack LayoutBox instances from top to bottom."""
515-
516-
if width_ratios is None:
517-
width_ratios = np.ones(len(boxes))
518512
for i in range(1, len(boxes)):
519513
c = (boxes[i-1].width ==
520514
boxes[i].width*width_ratios[i-1]/width_ratios[i])
521515
boxes[i].solver.addConstraint(c | strength)
522516

523517

524-
def vstackeq(boxes, padding=0, height_ratios=None):
525-
vstack(boxes, padding=padding)
526-
match_heights(boxes, height_ratios=height_ratios)
527-
528-
529-
def hstackeq(boxes, padding=0, width_ratios=None):
530-
hstack(boxes, padding=padding)
531-
match_widths(boxes, width_ratios=width_ratios)
532-
533-
534518
def align(boxes, attr, strength='strong'):
535519
cons = []
536520
for box in boxes[1:]:

lib/matplotlib/gridspec.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):
4949

5050
def __repr__(self):
5151
height_arg = (', height_ratios=%r' % (self._row_height_ratios,)
52-
if self._row_height_ratios is not None else '')
52+
if len(set(self._row_height_ratios)) != 1 else '')
5353
width_arg = (', width_ratios=%r' % (self._col_width_ratios,)
54-
if self._col_width_ratios is not None else '')
54+
if len(set(self._col_width_ratios)) != 1 else '')
5555
return '{clsname}({nrows}, {ncols}{optionals})'.format(
5656
clsname=self.__class__.__name__,
5757
nrows=self._nrows,
@@ -97,7 +97,9 @@ def set_width_ratios(self, width_ratios):
9797
*width_ratios* must be of length *ncols*. Each column gets a relative
9898
width of ``width_ratios[i] / sum(width_ratios)``.
9999
"""
100-
if width_ratios is not None and len(width_ratios) != self._ncols:
100+
if width_ratios is None:
101+
width_ratios = [1] * self._ncols
102+
elif len(width_ratios) != self._ncols:
101103
raise ValueError('Expected the given number of width ratios to '
102104
'match the number of columns of the grid')
103105
self._col_width_ratios = width_ratios
@@ -117,7 +119,9 @@ def set_height_ratios(self, height_ratios):
117119
*height_ratios* must be of length *nrows*. Each row gets a relative
118120
height of ``height_ratios[i] / sum(height_ratios)``.
119121
"""
120-
if height_ratios is not None and len(height_ratios) != self._nrows:
122+
if height_ratios is None:
123+
height_ratios = [1] * self._nrows
124+
elif len(height_ratios) != self._nrows:
121125
raise ValueError('Expected the given number of height ratios to '
122126
'match the number of rows of the grid')
123127
self._row_height_ratios = height_ratios
@@ -174,22 +178,16 @@ def get_grid_positions(self, fig, raw=False):
174178
# calculate accumulated heights of columns
175179
cell_h = tot_height / (nrows + hspace*(nrows-1))
176180
sep_h = hspace * cell_h
177-
if self._row_height_ratios is not None:
178-
norm = cell_h * nrows / sum(self._row_height_ratios)
179-
cell_heights = [r * norm for r in self._row_height_ratios]
180-
else:
181-
cell_heights = [cell_h] * nrows
181+
norm = cell_h * nrows / sum(self._row_height_ratios)
182+
cell_heights = [r * norm for r in self._row_height_ratios]
182183
sep_heights = [0] + ([sep_h] * (nrows-1))
183184
cell_hs = np.cumsum(np.column_stack([sep_heights, cell_heights]).flat)
184185

185186
# calculate accumulated widths of rows
186187
cell_w = tot_width / (ncols + wspace*(ncols-1))
187188
sep_w = wspace * cell_w
188-
if self._col_width_ratios is not None:
189-
norm = cell_w * ncols / sum(self._col_width_ratios)
190-
cell_widths = [r * norm for r in self._col_width_ratios]
191-
else:
192-
cell_widths = [cell_w] * ncols
189+
norm = cell_w * ncols / sum(self._col_width_ratios)
190+
cell_widths = [r * norm for r in self._col_width_ratios]
193191
sep_widths = [0] + ([sep_w] * (ncols-1))
194192
cell_ws = np.cumsum(np.column_stack([sep_widths, cell_widths]).flat)
195193

0 commit comments

Comments
 (0)