8000 Merge pull request #5717 from tacaswell/fix_errorbar_empty · matplotlib/matplotlib@e4d48ae · GitHub
[go: up one dir, main page]

Skip to content

Commit e4d48ae

Browse files
committed
Merge pull request #5717 from tacaswell/fix_errorbar_empty
FIX: fix passing empty lists to errorbar xerr/yerr
2 parents 7a57e8d + e67b42b commit e4d48ae

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,7 @@ def xywhere(xs, ys, mask):
28802880

28812881
if xerr is not None:
28822882
if (iterable(xerr) and len(xerr) == 2 and
2883-
iterable(xerr[0]) and iterable(xerr[1])):
2883+
iterable(xerr[0]) and iterable(xerr[1])):
28842884
# using list comps rather than arrays to preserve units
28852885
left = [thisx - thiserr for (thisx, thiserr)
28862886
in cbook.safezip(x, xerr[0])]
@@ -2890,9 +2890,9 @@ def xywhere(xs, ys, mask):
28902890
# Check if xerr is scalar or symmetric. Asymmetric is handled
28912891
# above. This prevents Nx2 arrays from accidentally
28922892
# being accepted, when the user meant the 2xN transpose.
2893-
if not (len(xerr) == 1 or
2894-
(len(xerr) == len(x) and not (
2895-
iterable(xerr[0]) and len(xerr[0]) > 1))):
2893+
# special case for empty lists
2894+
if len(xerr) > 1 and not ((len(xerr) == len(x) and not (
2895+
iterable(xerr[0]) and len(xerr[0]) > 1))):
28962896
raise ValueError("xerr must be a scalar, the same "
28972897
"dimensions as x, or 2xN.")
28982898
# using list comps rather than arrays to preserve units
@@ -2954,9 +2954,8 @@ def xywhere(xs, ys, mask):
29542954
in cbook.safezip(y, yerr[1])]
29552955
else:
29562956
# Check for scalar or symmetric, as in xerr.
2957-
if not (len(yerr) == 1 or
2958-
(len(yerr) == len(y) and not (
2959-
iterable(yerr[0]) and len(yerr[0]) > 1))):
2957+
if len(yerr) > 1 and not ((len(yerr) == len(y) and not (
2958+
iterable(yerr[0]) and len(yerr[0]) > 1))):
29602959
raise ValueError("yerr must be a scalar, the same "
29612960
"dimensions as y, or 2xN.")
29622961
# using list comps rather than arrays to preserve units

lib/matplotlib/tests/test_axes.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import six
55
from six.moves import xrange
6-
6+
from itertools import chain
77
import io
88

99
from nose.tools import assert_equal, assert_raises, assert_false, assert_true
@@ -13,6 +13,7 @@
1313
import numpy as np
1414
from numpy import ma
1515
from numpy import arange
16+
from cycler import cycler
1617

1718
import matplotlib
1819
from matplotlib.testing.decorators import image_comparison, cleanup
@@ -4093,6 +4094,44 @@ def test_violin_point_mass():
40934094
plt.violinplot(np.array([0, 0]))
40944095

40954096

4097+
def _eb_succes_helper(ax, x, y, xerr=None, yerr=None):
4098+
eb = ax.errorbar(x, y, xerr=xerr, yerr=yerr)
4099+
eb.remove()
4100+
4101+
4102+
@cleanup
4103+
def test_errorbar_inputs_shotgun():
4104+
base_xy = cycler('x', [np.arange(5)]) + cycler('y', [np.ones((5, ))])
4105+
err_cycler = cycler('err', [1,
4106+
[1, 1, 1, 1, 1],
4107+
[[1, 1, 1, 1, 1],
4108+
[1, 1, 1, 1, 1]],
4109+
[[1]] * 5,
4110+
np.ones(5),
4111+
np.ones((2, 5)),
4112+
np.ones((5, 1)),
4113+
None
4114+
])
4115+
xerr_cy = cycler('xerr', err_cycler)
4116+
yerr_cy = cycler('yerr', err_cycler)
4117+
4118+
empty = ((cycler('x', [[]]) + cycler('y', [[]])) *
4119+
cycler('xerr', [[], None]) * cycler('yerr', [[], None]))
4120+
xerr_only = base_xy * xerr_cy
4121+
yerr_only = base_xy * yerr_cy
4122+
both_err = base_xy * yerr_cy * xerr_cy
4123+
4124+
test_cyclers = xerr_only, yerr_only, both_err, empty
4125+
4126+
ax = plt.gca()
4127+
# should do this as a generative test, but @cleanup seems to break that
4128+
# for p in chain(*test_cyclers):
4129+
# yield (_eb_succes_helper, ax) + tuple(p.get(k, None) for
4130+
# k in ['x', 'y', 'xerr', 'yerr'])
4131+
for p in chain(*test_cyclers):
4132+
_eb_succes_helper(ax, **p)
4133+
4134+
40964135
@cleanup
40974136
def test_remove_shared_axes():
40984137

@@ -4137,6 +4176,9 @@ def _helper_y(ax):
41374176
ax.set_xlim(0, 5)
41384177
assert assert_array_equal(ax_lst[0][1].get_xlim(), orig_xlim)
41394178

4179+
4180+
4181+
41404182
if __name__ == '__main__':
41414183
import nose
41424184
import sys

0 commit comments

Comments
 (0)
0