Skip to content

Commit 3a195da

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

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,6 +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 any(e != e for e in err.ravel()):
3508+
raise ValueError(
3509+
f"'{dep_axis}err' must not contain NaN values")
35073510
if np.any(err < -err): # like err<0, but also works for timedelta.
35083511
raise ValueError(
35093512
f"'{dep_axis}err' must not contain negative values")

lib/matplotlib/tests/test_axes.py

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

75517551

7552+
def test_nan_barlabels():
7553+
ax = plt.gca()
7554+
bars = ax.bar([2, 3], [np.nan, 1], yerr=[0.3, 0.5])
7555+
labels = ax.bar_label(bars)
7556+
assert [l.get_text() for l in labels] == ['', '1']
7557+
7558+
with pytest.raises(ValueError,
7559+
match="'yerr' must not contain NaN values"):
7560+
ax = plt.gca()
7561+
bars = ax.bar([2, 3], [np.nan, 1], yerr=[np.nan, 0.5])
7562+
labels = ax.bar_label(bars)
7563+
7564+
75527565
def test_patch_bounds(): # PR 19078
75537566
fig, ax = plt.subplots()
75547567
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy