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

Skip to content

Normalize gridspec ratios to lists in the setter. #17291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,6 @@ def _align_spines(fig, gs):
nrows, ncols = gs.get_geometry()
width_ratios = gs.get_width_ratios()
height_ratios = gs.get_height_ratios()
if width_ratios is None:
width_ratios = np.ones(ncols)
if height_ratios is None:
height_ratios = np.ones(nrows)

# get axes in this gridspec....
axs = [ax for ax in fig.axes
Expand Down
20 changes: 2 additions & 18 deletions lib/matplotlib/_layoutbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,38 +499,22 @@ def vpack(boxes, padding=0, strength='strong'):
boxes[i].solver.addConstraint(c | strength)


def match_heights(boxes, height_ratios=None, strength='medium'):
def match_heights(boxes, height_ratios, strength='medium'):
"""Stack LayoutBox instances from top to bottom."""

if height_ratios is None:
height_ratios = np.ones(len(boxes))
for i in range(1, len(boxes)):
c = (boxes[i-1].height ==
boxes[i].height*height_ratios[i-1]/height_ratios[i])
boxes[i].solver.addConstraint(c | strength)


def match_widths(boxes, width_ratios=None, strength='medium'):
def match_widths(boxes, width_ratios, strength='medium'):
"""Stack LayoutBox instances from top to bottom."""

if width_ratios is None:
width_ratios = np.ones(len(boxes))
for i in range(1, len(boxes)):
c = (boxes[i-1].width ==
boxes[i].width*width_ratios[i-1]/width_ratios[i])
boxes[i].solver.addConstraint(c | strength)


def vstackeq(boxes, padding=0, height_ratios=None):
vstack(boxes, padding=padding)
match_heights(boxes, height_ratios=height_ratios)


def hstackeq(boxes, padding=0, width_ratios=None):
hstack(boxes, padding=padding)
match_widths(boxes, width_ratios=width_ratios)


def align(boxes, attr, strength='strong'):
cons = []
for box in boxes[1:]:
Expand Down
26 changes: 12 additions & 14 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):

def __repr__(self):
height_arg = (', height_ratios=%r' % (self._row_height_ratios,)
if self._row_height_ratios is not None else '')
if len(set(self._row_height_ratios)) != 1 else '')
width_arg = (', width_ratios=%r' % (self._col_width_ratios,)
if self._col_width_ratios is not None else '')
if len(set(self._col_width_ratios)) != 1 else '')
return '{clsname}({nrows}, {ncols}{optionals})'.format(
clsname=self.__class__.__name__,
nrows=self._nrows,
Expand Down Expand Up @@ -104,7 +104,9 @@ def set_width_ratios(self, width_ratios):
*width_ratios* must be of length *ncols*. Each column gets a relative
width of ``width_ratios[i] / sum(width_ratios)``.
"""
if width_ratios is not None and len(width_ratios) != self._ncols:
if width_ratios is None:
width_ratios = [1] * self._ncols
elif len(width_ratios) != self._ncols:
raise ValueError('Expected the given number of width ratios to '
'match the number of columns of the grid')
self._col_width_ratios = width_ratios
Expand All @@ -124,7 +126,9 @@ def set_height_ratios(self, height_ratios):
*height_ratios* must be of length *nrows*. Each row gets a relative
height of ``height_ratios[i] / sum(height_ratios)``.
"""
if height_ratios is not None and len(height_ratios) != self._nrows:
if height_ratios is None:
height_ratios = [1] * self._nrows
elif len(height_ratios) != self._nrows:
raise ValueError('Expected the given number of height ratios to '
'match the number of rows of the grid')
self._row_height_ratios = height_ratios
Expand Down Expand Up @@ -181,22 +185,16 @@ def get_grid_positions(self, fig, raw=False):
# calculate accumulated heights of columns
cell_h = tot_height / (nrows + hspace*(nrows-1))
sep_h = hspace * cell_h
if self._row_height_ratios is not None:
norm = cell_h * nrows / sum(self._row_height_ratios)
cell_heights = [r * norm for r in self._row_height_ratios]
else:
cell_heights = [cell_h] * nrows
norm = cell_h * nrows / sum(self._row_height_ratios)
cell_heights = [r * norm for r in self._row_height_ratios]
sep_heights = [0] + ([sep_h] * (nrows-1))
cell_hs = np.cumsum(np.column_stack([sep_heights, cell_heights]).flat)

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

Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def test_simple():
pickle.dump(fig, BytesIO(), pickle.HIGHEST_PROTOCOL)


@image_comparison(['multi_pickle.png'], remove_text=True, style='mpl20',
tol=0 if platform.machine() == 'x86_64' else 0.082)
@image_comparison(
['multi_pickle.png'], remove_text=True, style='mpl20', tol=0.082)
def test_complete():
# Remove this line when this test image is regenerated.
plt.rcParams['pcolormesh.snap'] = False
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def test_tight_layout3():
plt.tight_layout()


@image_comparison(['tight_layout4'], freetype_version=('2.5.5', '2.6.1'))
@image_comparison(['tight_layout4'], freetype_version=('2.5.5', '2.6.1'),
tol=0.015)
def test_tight_layout4():
"""Test tight_layout for subplot2grid."""
ax1 = plt.subplot2grid((3, 3), (0, 0))
Expand Down