Skip to content

Fix gridspec.Gridspec: check ratios for consistency with rows and columns #5892

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 3 commits into from
Jan 24, 2016
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
8 changes: 6 additions & 2 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,24 @@ def new_subplotspec(self, loc, rowspan=1, colspan=1):
subplotspec = self[loc1:loc1+rowspan, loc2:loc2+colspan]
return subplotspec


def set_width_ratios(self, width_ratios):
if width_ratios is not None and 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

def get_width_ratios(self):
return self._col_width_ratios

def set_height_ratios(self, height_ratios):
if height_ratios is not None and 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

def get_height_ratios(self):
return self._row_height_ratios


def get_grid_positions(self, fig):
"""
return lists of bottom and top position of rows, left and
Expand Down
20 changes: 19 additions & 1 deletion lib/matplotlib/tests/test_gridspec.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import matplotlib.gridspec as gridspec
from nose.tools import assert_equal
from nose.tools import assert_raises, assert_equal


def test_equal():
gs = gridspec.GridSpec(2, 1)
assert_equal(gs[0, 0], gs[0, 0])
assert_equal(gs[:, 0], gs[:, 0])


def test_width_ratios():
"""
Addresses issue #5835.
See at https://github.com/matplotlib/matplotlib/issues/5835.
"""
assert_raises(ValueError, gridspec.GridSpec,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a second one of these which fails via the other path (if I am reading this right this only really tests the width based failure).

1, 1, width_ratios=[2, 1, 3])


def test_height_ratios():
"""
Addresses issue #5835.
See at https://github.com/matplotlib/matplotlib/issues/5835.
"""
assert_raises(ValueError, gridspec.GridSpec,
1, 1, height_ratios=[2, 1, 3])