Skip to content

Commit b058332

Browse files
committed
Handle NaN in bar labels and error bars
1 parent 1ab4a53 commit b058332

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,10 @@ def hlines(self, y, xmin, xmax, colors=None, linestyles='solid',
10831083
lines._internal_update(kwargs)
10841084

10851085
if len(y) > 0:
1086-
minx = min(xmin.min(), xmax.min())
1087-
maxx = max(xmin.max(), xmax.max())
1088-
miny = y.min()
1089-
maxy = y.max()
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)
10901090

10911091
corners = (minx, miny), (maxx, maxy)
10921092

@@ -1162,10 +1162,10 @@ def vlines(self, x, ymin, ymax, colors=None, linestyles='solid',
11621162
lines._internal_update(kwargs)
11631163

11641164
if len(x) > 0:
1165-
minx = x.min()
1166-
maxx = x.max()
1167-
miny = min(ymin.min(), ymax.min())
1168-
maxy = max(ymin.max(), ymax.max())
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))
11691169

11701170
corners = (minx, miny), (maxx, maxy)
11711171
self.update_datalim(corners)
@@ -2674,7 +2674,7 @@ def sign(x):
26742674
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
26752675
length = abs(x0 - x1)
26762676

2677-
if err is None:
2677+
if err is None or np.size(err) == 0:
26782678
endpt = extrema
26792679
elif orientation == "vertical":
26802680
endpt = err[:, 1].max() if dat >= 0 else err[:, 1].min()
@@ -3504,7 +3504,9 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
35043504
f"'{dep_axis}err' (shape: {np.shape(err)}) must be a "
35053505
f"scalar or a 1D or (2, n) array-like whose shape matches "
35063506
f"'{dep_axis}' (shape: {np.shape(dep)})") from None
3507-
if np.any(err < -err): # like err<0, but also works for timedelta.
3507+
res = np.zeros_like(err, dtype=bool) # Default in case of nan
3508+
if np.any(np.less(err, -err, out=res, where=(err == err))):
3509+
# like err<0, but also works for timedelta and nan.
35083510
raise ValueError(
35093511
f"'{dep_axis}err' must not contain negative values")
35103512
# This is like

lib/matplotlib/tests/test_axes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7549,6 +7549,26 @@ def test_bar_label_nan_ydata_inverted():
75497549
assert labels[0].get_va() == 'bottom'
75507550

75517551

7552+
def test_nan_barlabels():
7553+
fig, ax = plt.subplots()
7554+
bars = ax.bar([1, 2, 3], [np.nan, 1, 2], yerr=[0.2, 0.4, 0.6])
7555+
labels = ax.bar_label(bars)
7556+
assert [l.get_text() for l in labels] == ['', '1', '2']
7557+
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
7558+
7559+
fig, ax = plt.subplots()
7560+
bars = ax.bar([1, 2, 3], [0, 1, 2], yerr=[0.2, np.nan, 0.6])
7561+
labels = ax.bar_label(bars)
7562+
assert [l.get_text() for l in labels] == ['0', '1', '2']
7563+
assert np.allclose(ax.get_ylim(), (-0.5, 3.0))
7564+
7565+
fig, ax = plt.subplots()
7566+
bars = ax.bar([1, 2, 3], [np.nan, 1, 2], yerr=[np.nan, np.nan, 0.6])
7567+
labels = ax.bar_label(bars)
7568+
assert [l.get_text() for l in labels] == ['', '1', '2']
7569+
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
7570+
7571+
75527572
def test_patch_bounds(): # PR 19078
75537573
fig, ax = plt.subplots()
75547574
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1))

0 commit comments

Comments
 (0)