Skip to content

Commit a2ad1f7

Browse files
authored
Merge pull request #23333 from anntzer/en
Fix errorbar handling of nan.
2 parents da24a01 + fbe0371 commit a2ad1f7

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

lib/matplotlib/axes/_axes.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -1083,13 +1083,14 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
10831083
lines._internal_update(kwargs)
10841084

10851085
if len(y) > 0:
1086-
minx = min(np.nanmin(xmin), np.nanmin(xmax))
1087-
maxx = max(np.nanmax(xmin), np.nanmax(xmax))
1088-
miny = np.nanmin(y)
1089-
maxy = np.nanmax(y)
1090-
1086+
# Extreme values of xmin/xmax/y. Using masked_verts here handles
1087+
# the case of y being a masked *object* array (as can be generated
1088+
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1089+
minx = np.nanmin(masked_verts[..., 0])
1090+
maxx = np.nanmax(masked_verts[..., 0])
1091+
miny = np.nanmin(masked_verts[..., 1])
1092+
maxy = np.nanmax(masked_verts[..., 1])
10911093
corners = (minx, miny), (maxx, maxy)
1092-
10931094
self.update_datalim(corners)
10941095
self._request_autoscale_view()
10951096

@@ -1162,11 +1163,13 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11621163
lines._internal_update(kwargs)
11631164

11641165
if len(x) > 0:
1165-
minx = np.nanmin(x)
1166-
maxx = np.nanmax(x)
1167-
miny = min(np.nanmin(ymin), np.nanmin(ymax))
1168-
maxy = max(np.nanmax(ymin), np.nanmax(ymax))
1169-
1166+
# Extreme values of x/ymin/ymax. Using masked_verts here handles
1167+
# the case of x being a masked *object* array (as can be generated
1168+
# e.g. by errorbar()), which would make nanmin/nanmax stumble.
1169+
minx = np.nanmin(masked_verts[..., 0])
1170+
maxx = np.nanmax(masked_verts[..., 0])
1171+
miny = np.nanmin(masked_verts[..., 1])
1172+
maxy = np.nanmax(masked_verts[..., 1])
11701173
corners = (minx, miny), (maxx, maxy)
11711174
self.update_datalim(corners)
11721175
self._request_autoscale_view()

lib/matplotlib/tests/test_axes.py

+14
Original file line numberDiff line numberDiff line change
@@ -3790,6 +3790,20 @@ def test_errorbar_linewidth_type(elinewidth):
37903790
plt.errorbar([1, 2, 3], [1, 2, 3], yerr=[1, 2, 3], elinewidth=elinewidth)
37913791

37923792

3793+
@check_figures_equal(extensions=["png"])
3794+
def test_errorbar_nan(fig_test, fig_ref):
3795+
ax = fig_test.add_subplot()
3796+
xs = range(5)
3797+
ys = np.array([1, 2, np.nan, np.nan, 3])
3798+
es = np.array([4, 5, np.nan, np.nan, 6])
3799+
ax.errorbar(xs, ys, es)
3800+
ax = fig_ref.add_subplot()
3801+
ys = np.array([1, 2, np.nan, np.nan, 3])
3802+
es = np.array([4, 5, np.nan, np.nan, 6])
3803+
ax.errorbar([0, 1], [1, 2], [4, 5])
3804+
ax.errorbar([4], [3], [6], fmt="C0")
3805+
3806+
37933807
@image_comparison(['hist_stacked_stepfilled', 'hist_stacked_stepfilled'])
37943808
def test_hist_stacked_stepfilled():
37953809
# make some data

0 commit comments

Comments
 (0)