Skip to content

Commit a5e51ca

Browse files
committed
Merge pull request #4352 from tacaswell/api_dontplot_None
API/FIX : don't accept None for x or y in plot
2 parents 1bca1db + 7674cb2 commit a5e51ca

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Disallow ``None`` as x or y value in ax.plot
2+
````````````````````````````````````````````
3+
4+
Do not allow ``None`` as a valid input for the ``x`` or ``y`` args in
5+
`ax.plot`. This may break some user code, but this was never officially
6+
supported (ex documented) and allowing ``None`` objects through can lead
7+
to confusing exceptions downstream.
8+
9+
To create an empty line use ::
10+
11+
ln1, = ax.plot([], [], ...)
12+
ln2, = ax.plot([], ...)
13+
14+
In either case to update the data in the `Line2D` object you must update
15+
both the ``x`` and ``y`` data.

lib/matplotlib/axes/_base.py

+7
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,13 @@ def _plot_args(self, tup, kwargs):
263263
raise ValueError('third arg must be a format string')
264264
else:
265265
linestyle, marker, color = None, None, None
266+
267+
# Don't allow any None value; These will be up-converted
268+
# to one element array of None which causes problems
269+
# downstream.
270+
if any(v is None for v in tup):
271+
raise ValueError("x and y must not be None")
272+
266273
kw = {}
267274
for k, v in zip(('linestyle', 'marker', 'color'),
268275
(linestyle, marker, color)):

lib/matplotlib/tests/test_axes.py

+7
Original file line numberDiff line numberDiff line change
@@ -3668,6 +3668,13 @@ def test_bar_negative_width():
36683668
assert_equal(b._height, indx + 1)
36693669

36703670

3671+
@cleanup
3672+
def test_no_None():
3673+
fig, ax = plt.subplots()
3674+
assert_raises(ValueError, plt.plot, None)
3675+
assert_raises(ValueError, plt.plot, None, None)
3676+
3677+
36713678
if __name__ == '__main__':
36723679
import nose
36733680
import sys

0 commit comments

Comments
 (0)