From 671750cca64835e384572343c919ac5f17d00075 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sat, 6 Apr 2019 11:23:52 +0200 Subject: [PATCH] Check for positive number or rows and cols --- lib/matplotlib/axes/_subplots.py | 4 ++++ lib/matplotlib/tests/test_figure.py | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/matplotlib/axes/_subplots.py b/lib/matplotlib/axes/_subplots.py index 001f5f1624cb..f5e57cd73326 100644 --- a/lib/matplotlib/axes/_subplots.py +++ b/lib/matplotlib/axes/_subplots.py @@ -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( diff --git a/lib/matplotlib/tests/test_figure.py b/lib/matplotlib/tests/test_figure.py index 6ae4d430df01..28a0de07fe6e 100644 --- a/lib/matplotlib/tests/test_figure.py +++ b/lib/matplotlib/tests/test_figure.py @@ -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()