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

Skip to content

Check for positive number of rows and cols #13894

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
Apr 10, 2019
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: 4 additions & 0 deletions lib/matplotlib/axes/_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def __init__(self, fig, *args, **kwargs):
rows, cols, num = args
rows = int(rows)
cols = int(cols)
if rows <= 0:
raise ValueError(f'Number of rows must be > 0, not {rows}')
if cols <= 0:
raise ValueError(f'Number of columns must be > 0, not {cols}')
if isinstance(num, tuple) and len(num) == 2:
num = [int(n) for n in num]
self._subplotspec = GridSpec(
Expand Down
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ def test_gca():
assert fig.gca() is ax1


def test_add_subplot_invalid():
fig = plt.figure()
with pytest.raises(ValueError):
fig.add_subplot(2, 0, 1)
with pytest.raises(ValueError):
fig.add_subplot(0, 2, 1)
with pytest.raises(ValueError):
fig.add_subplot(2, 2, 0)
with pytest.raises(ValueError):
fig.add_subplot(2, 2, 5)


@image_comparison(baseline_images=['figure_suptitle'])
def test_suptitle():
fig, _ = plt.subplots()
Expand Down