Skip to content

Commit 30db3ac

Browse files
committed
Merge pull request #5892 from julianvmodesto/master
Fix gridspec.Gridspec: check ratios for consistency with rows and columns
2 parents 7a78f70 + 9052785 commit 30db3ac

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/matplotlib/gridspec.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,24 @@ def new_subplotspec(self, loc, rowspan=1, colspan=1):
6262
subplotspec = self[loc1:loc1+rowspan, loc2:loc2+colspan]
6363
return subplotspec
6464

65-
6665
def set_width_ratios(self, width_ratios):
66+
if width_ratios is not None and len(width_ratios) != self._ncols:
67+
raise ValueError('Expected the given number of width ratios to '
68+
'match the number of columns of the grid')
6769
self._col_width_ratios = width_ratios
6870

6971
def get_width_ratios(self):
7072
return self._col_width_ratios
7173

7274
def set_height_ratios(self, height_ratios):
75+
if height_ratios is not None and len(height_ratios) != self._nrows:
76+
raise ValueError('Expected the given number of height ratios to '
77+
'match the number of rows of the grid')
7378
self._row_height_ratios = height_ratios
7479

7580
def get_height_ratios(self):
7681
return self._row_height_ratios
7782

78-
7983
def get_grid_positions(self, fig):
8084
"""
8185
return lists of bottom and top position of rows, left and

lib/matplotlib/tests/test_gridspec.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
import matplotlib.gridspec as gridspec
2-
from nose.tools import assert_equal
2+
from nose.tools import assert_raises, assert_equal
33

44

55
def test_equal():
66
gs = gridspec.GridSpec(2, 1)
77
assert_equal(gs[0, 0], gs[0, 0])
88
assert_equal(gs[:, 0], gs[:, 0])
9+
10+
11+
def test_width_ratios():
12+
"""
13+
Addresses issue #5835.
14+
See at https://github.com/matplotlib/matplotlib/issues/5835.
15+
"""
16+
assert_raises(ValueError, gridspec.GridSpec,
17+
1, 1, width_ratios=[2, 1, 3])
18+
19+
20+
def test_height_ratios():
21+
"""
22+
Addresses issue #5835.
23+
See at https://github.com/matplotlib/matplotlib/issues/5835.
24+
"""
25+
assert_raises(ValueError, gridspec.GridSpec,
26+
1, 1, height_ratios=[2, 1, 3])

0 commit comments

Comments
 (0)