Skip to content

Backport PR #17715 on branch v3.3.x (Clarify gridspec error message for non-integer inputs.) #17718

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
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
6 changes: 4 additions & 2 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ def __init__(self, nrows, ncols, height_ratios=None, width_ratios=None):
If not given, all rows will have the same height.
"""
if not isinstance(nrows, Integral) or nrows <= 0:
raise ValueError(f"Number of rows must be > 0, not {nrows}")
raise ValueError(
f"Number of rows must be a positive integer, not {nrows}")
if not isinstance(ncols, Integral) or ncols <= 0:
raise ValueError(f"Number of columns must be > 0, not {ncols}")
raise ValueError(
f"Number of columns must be a positive integer, not {ncols}")
self._nrows, self._ncols = nrows, ncols
self.set_height_ratios(height_ratios)
self.set_width_ratios(width_ratios)
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ def test_gca():

def test_add_subplot_invalid():
fig = plt.figure()
with pytest.raises(ValueError, match='Number of columns must be > 0'):
with pytest.raises(ValueError,
match='Number of columns must be a positive integer'):
fig.add_subplot(2, 0, 1)
with pytest.raises(ValueError, match='Number of rows must be > 0'):
with pytest.raises(ValueError,
match='Number of rows must be a positive integer'):
fig.add_subplot(0, 2, 1)
with pytest.raises(ValueError, match='num must be 1 <= num <= 4'):
fig.add_subplot(2, 2, 0)
Expand Down