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

Skip to content

Commit 9d3a736

Browse files
authored
Merge pull request #13894 from timhoffm/subplot-check-nrows-ncols
Check for positive number of rows and cols
2 parents bc4c4cc + 671750c commit 9d3a736

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/matplotlib/axes/_subplots.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def __init__(self, fig, *args, **kwargs):
4848
rows, cols, num = args
4949
rows = int(rows)
5050
cols = int(cols)
51+
if rows <= 0:
52+
raise ValueError(f'Number of rows must be > 0, not {rows}')
53+
if cols <= 0:
54+
raise ValueError(f'Number of columns must be > 0, not {cols}')
5155
if isinstance(num, tuple) and len(num) == 2:
5256
num = [int(n) for n in num]
5357
self._subplotspec = GridSpec(

lib/matplotlib/tests/test_figure.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ def test_gca():
164164
assert fig.gca() is ax1
165165

166166

167+
def test_add_subplot_invalid():
168+
fig = plt.figure()
169+
with pytest.raises(ValueError):
170+
fig.add_subplot(2, 0, 1)
171+
with pytest.raises(ValueError):
172+
fig.add_subplot(0, 2, 1)
173+
with pytest.raises(ValueError):
174+
fig.add_subplot(2, 2, 0)
175+
with pytest.raises(ValueError):
176+
fig.add_subplot(2, 2, 5)
177+
178+
167179
@image_comparison(baseline_images=['figure_suptitle'])
168180
def test_suptitle():
169181
fig, _ = plt.subplots()

0 commit comments

Comments
 (0)