Skip to content

Commit ca4958b

Browse files
authored
Merge pull request matplotlib#22929 from oscargus/nanbarlabel
Handle NaN in bar labels and error bars
2 parents 803df21 + b058332 commit ca4958b

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()
@@ -3505,7 +3505,9 @@ def apply_mask(arrays, mask): return [array[mask] for array in arrays]
35053505
f"'{dep_axis}err' (shape: {np.shape(err)}) must be a "
35063506
f"scalar or a 1D or (2, n) array-like whose shape matches "
35073507
f"'{dep_axis}' (shape: {np.shape(dep)})") from None
3508-
if np.any(err < -err): # like err<0, but also works for timedelta.
3508+
res = np.zeros_like(err, dtype=bool) # Default in case of nan
3509+
if np.any(np.less(err, -err, out=res, where=(err == err))):
3510+
# like err<0, but also works for timedelta and nan.
35093511
raise ValueError(
35103512
f"'{dep_axis}err' must not contain negative values")
35113513
# This is like

lib/matplotlib/tests/test_axes.py

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

75667566

7567+
def test_nan_barlabels():
7568+
fig, ax = plt.subplots()
7569+
bars = ax.bar([1, 2, 3], [np.nan, 1, 2], yerr=[0.2, 0.4, 0.6])
7570+
labels = ax.bar_label(bars)
7571+
assert [l.get_text() for l in labels] == ['', '1', '2']
7572+
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
7573+
7574+
fig, ax = plt.subplots()
7575+
bars = ax.bar([1, 2, 3], [0, 1, 2], yerr=[0.2, np.nan, 0.6])
7576+
labels = ax.bar_label(bars)
7577+
assert [l.get_text() for l in labels] == ['0', '1', '2']
7578+
assert np.allclose(ax.get_ylim(), (-0.5, 3.0))
7579+
7580+
fig, ax = plt.subplots()
7581+
bars = ax.bar([1, 2, 3], [np.nan, 1, 2], yerr=[np.nan, np.nan, 0.6])
7582+
labels = ax.bar_label(bars)
7583+
assert [l.get_text() for l in labels] == ['', '1', '2']
7584+
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
7585+
7586+
75677587
def test_patch_bounds(): # PR 19078
75687588
fig, ax = plt.subplots()
75697589
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1))

0 commit comments

Comments
 (0)