diff --git a/lib/matplotlib/gridspec.py b/lib/matplotlib/gridspec.py index 1045aa596bcb..b31aa70c6787 100644 --- a/lib/matplotlib/gridspec.py +++ b/lib/matplotlib/gridspec.py @@ -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 diff --git a/lib/matplotlib/tests/test_gridspec.py b/lib/matplotlib/tests/test_gridspec.py index 57616397f5e3..55ad16b42558 100644 --- a/lib/matplotlib/tests/test_gridspec.py +++ b/lib/matplotlib/tests/test_gridspec.py @@ -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, + 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])