8000 Properly deprecate num=0 in subplot related functions · matplotlib/matplotlib@9cc4a3e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cc4a3e

Browse files
committed
Properly deprecate num=0 in subplot related functions
1 parent 10bc45e commit 9cc4a3e

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

doc/api/api_changes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ original location:
189189
If `fontpropreties` is not passed in, but `prop` is, then `prop` is used inplace
190190
of `fontpropreties`. If both are passed in, `prop` is silently ignored.
191191

192+
* The use of the index 0 has in `plt.subplot` and related commands is
193+
deprecated. Due to a lack of validation calling `plt.subplots(2, 2,
194+
0)` does not raise an exception, but puts an axes in the _last_
195+
position. This is due to the indexing in subplot being 1-based (to
196+
support the three-digit number scheme) so before indexing into the
197+
`GridSpec` object used to determine where the axes should go one is
198+
subtracted off, resulting in getting the last position back. Even
199+
though this behavior is clearly wrong and not intended, we are going
200+
through a deprecation cycle in an abundance of caution that any users
201+
are exploiting this 'feature'. The use of 0 as an index will raise
202+
a warning in 1.4 and an exception in 1.5.
203+
204+
192205
Code removal
193206
------------
194207

lib/matplotlib/axes/_subplots.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import matplotlib.artist as martist
1010
from matplotlib.axes._axes import Axes
1111

12+
import warnings
13+
from matplotlib.cbook import mplDeprecation
14+
1215

1316
class SubplotBase(object):
1417
"""
@@ -55,10 +58,15 @@ def __init__(self, fig, *args, **kwargs):
5558
num = [int(n) for n in num]
5659
self._subplotspec = GridSpec(rows, cols)[num[0] - 1:num[1]]
5760
else:
58-
if num == 0 or num > rows*cols:
61+
if num < 0 or num > rows*cols:
5962
raise ValueError(
60-
"num must be 0 < num <= {maxn}, not {num}".format(
63+
"num must be 0 <= num <= {maxn}, not {num}".format(
6164
maxn=rows*cols, num=num))
65+
if num == 0:
66+
warnings.warn("The use of 0 (which ends up being the "
67+
"_last_ sub-plot) is deprecated in 1.4 "
68+
"and will raise an error in 1.5",
69+
mplDeprecation)
6270
self._subplotspec = GridSpec(rows, cols)[int(num) - 1]
6371
# num - 1 for converting from MATLAB to python indexing
6472
else:

lib/matplotlib/tests/test_subplots.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ 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)
108+
assert_raises(ValueError, plt.subplots, 2, 2, -1)
109+
# uncomment this for 1.5
110+
# assert_raises(ValueError, plt.subplots, 2, 2, 0)
109111
assert_raises(ValueError, plt.subplots, 2, 2, 5)
110112

111113

0 commit comments

Comments
 (0)
0