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

Skip to content

Commit 10bc45e

Browse files
committed
BUG : raise exception in subplot if num out of range
if subplot(rows, cols, num) is called with num not in 0 < num <= rows * cols raise a ValueError close #3166
1 parent f896381 commit 10bc45e

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

lib/matplotlib/axes/_subplots.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def __init__(self, fig, *args, **kwargs):
5555
num = [int(n) for n in num]
5656
self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]]
5757
else:
58+
if num == 0 or num > rows*cols:
59+
raise ValueError(
60+
"num must be 0 < num <= {maxn}, not {num}".format(
61+
maxn=rows*cols, num=num))
5862
self._subplotspec = GridSpec(rows, cols)[int(num) - 1]
5963
# num - 1 for converting from MATLAB to python indexing
6064
else:

lib/matplotlib/tests/test_subplots.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy
88
import matplotlib.pyplot as plt
9-
from matplotlib.testing.decorators import image_comparison
9+
from matplotlib.testing.decorators import image_comparison, cleanup
1010

1111
from nose.tools import assert_raises
1212

@@ -105,19 +105,22 @@ def test_exceptions():
105105
# TODO should this test more options?
106106
assert_raises(ValueError, plt.subplots, 2, 2, sharex='blah')
107107
assert_raises(ValueError, plt.subplots, 2, 2, sharey='blah')
108+
assert_raises(ValueError, plt.subplots, 2, 2, 0)
109+
assert_raises(ValueError, plt.subplots, 2, 2, 5)
108110

109111

110112
@image_comparison(baseline_images=['subplots_offset_text'], remove_text=False)
111113
def test_subplots_offsettext():
112-
x = numpy.arange(0,1e10,1e9)
113-
y = numpy.arange(0,100,10)+1e4
114-
fig,axes = plt.subplots(2,2, sharex = 'col', sharey = 'all')
115-
axes[0,0].plot(x,x)
116-
axes[1,0].plot(x,x)
117-
axes[0,1].plot(y,x)
118-
axes[1,1].plot(y,x)
114+
x = numpy.arange(0, 1e10, 1e9)
115+
y = numpy.arange(0, 100, 10)+1e4
116+
fig, axes = plt.subplots(2, 2, sharex='col', sharey='all')
117+
axes[0, 0].plot(x, x)
118+
axes[1, 0].plot(x, x)
119+
axes[0, 1].plot(y, x)
120+
axes[1, 1].plot(y, x)
119121

120122

123+
@cleanup
121124
def test_subplots():
122125
# things to test
123126
# - are axes actually shared?

0 commit comments

Comments
 (0)