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

Skip to content

Commit 8d7da5e

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 6f25240 commit 8d7da5e

File tree

5 files changed

+18
-39
lines changed

5 files changed

+18
-39
lines changed

lib/matplotlib/_constrained_layout.py

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

322318
# get axes in this gridspec....
323319
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
@@ -56,9 +56,9 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):
5656

5757
def __repr__(self):
5858
height_arg = (', height_ratios=%r' % (self._row_height_ratios,)
59-
if self._row_height_ratios is not None else '')
59+
if len(set(self._row_height_ratios)) != 1 else '')
6060
width_arg = (', width_ratios=%r' % (self._col_width_ratios,)
61-
if self._col_width_ratios is not None else '')
61+
if len(set(self._col_width_ratios)) != 1 else '')
6262
return '{clsname}({nrows}, {ncols}{optionals})'.format(
6363
clsname=self.__class__.__name__,
6464
nrows=self._nrows,
@@ -104,7 +104,9 @@ def set_width_ratios(self, width_ratios):
104104
*width_ratios* must be of length *ncols*. Each column gets a relative
105105
width of ``width_ratios[i] / sum(width_ratios)``.
106106
"""
107-
if width_ratios is not None and len(width_ratios) != self._ncols:
107+
if width_ratios is None:
108+
width_ratios = [1] * self._ncols
109+
elif len(width_ratios) != self._ncols:
108110
raise ValueError('Expected the given number of width ratios to '
109111
'match the number of columns of the grid')
110112
self._col_width_ratios = width_ratios
@@ -124,7 +126,9 @@ def set_height_ratios(self, height_ratios):
124126
*height_ratios* must be of length *nrows*. Each row gets a relative
125127
height of ``height_ratios[i] / sum(height_ratios)``.
126128
"""
127-
if height_ratios is not None and len(height_ratios) != self._nrows:
129+
if height_ratios is None:
130+
height_ratios = [1] * self._nrows
131+
elif len(height_ratios) != self._nrows:
128132
raise ValueError('Expected the given number of height ratios to '
129133
'match the number of rows of the grid')
130134
self._row_height_ratios = height_ratios
@@ -181,22 +185,16 @@ def get_grid_positions(self, fig, raw=False):
181185
# calculate accumulated heights of columns
182186
cell_h = tot_height / (nrows + hspace*(nrows-1))
183187
sep_h = hspace * cell_h
184-
if self._row_height_ratios is not None:
185-
norm = cell_h * nrows / sum(self._row_height_ratios)
186-
cell_heights = [r * norm for r in self._row_height_ratios]
187-
else:
188-
cell_heights = [cell_h] * nrows
188+
norm = cell_h * nrows / sum(self._row_height_ratios)
189+
cell_heights = [r * norm for r in self._row_height_ratios]
189190
sep_heights = [0] + ([sep_h] * (nrows-1))
190191
cell_hs = np.cumsum(np.column_stack([sep_heights, cell_heights]).flat)
191192

192193
# calculate accumulated widths of rows
193194
cell_w = tot_width / (ncols + wspace*(ncols-1))
194195
sep_w = wspace * cell_w
195-
if self._col_width_ratios is not None:
196-
norm = cell_w * ncols / sum(self._col_width_ratios)
197-
cell_widths = [r * norm for r in self._col_width_ratios]
198-
else:
199-
cell_widths = [cell_w] * ncols
196+
norm = cell_w * ncols / sum(self._col_width_ratios)
197+
cell_widths = [r * norm for r in self._col_width_ratios]
200198
sep_widths = [0] + ([sep_w] * (ncols-1))
201199
cell_ws = np.cumsum(np.column_stack([sep_widths, cell_widths]).flat)
202200

lib/matplotlib/tests/test_pickle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ def test_simple():
4040
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)
4141

4242

43-
@image_comparison(['multi_pickle.png'], remove_text=True, style='mpl20',
44-
tol=0 if platform.machine() == 'x86_64' else 0.082)
43+
@image_comparison(
44+
['multi_pickle.png'], remove_text=True, style='mpl20', tol=0.082)
4545
def test_complete():
4646
# Remove this line when this test image is regenerated.
4747
plt.rcParams['pcolormesh.snap'] = False

lib/matplotlib/tests/test_tightlayout.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def test_tight_layout3():
5050
plt.tight_layout()
5151

5252

53-
@image_comparison(['tight_layout4'], freetype_version=('2.5.5', '2.6.1'))
53+
@image_comparison(['tight_layout4'], freetype_version=('2.5.5', '2.6.1'),
54+
tol=0.015)
5455
def test_tight_layout4():
5556
"""Test tight_layout for subplot2grid."""
5657
ax1 = plt.subplot2grid((3, 3), (0, 0))

0 commit comments

Comments
 (0)