diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dd7669b3e981..e907167c659a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -2624,14 +2624,15 @@ def sign(x): if label_type == "center": ha, va = "center", "center" elif label_type == "edge": - if orientation == "vertical" and dat >= 0: - ha, va = "center", "bottom" - elif orientation == "vertical" and dat < 0: - ha, va = "center", "top" - elif orientation == "horizontal" and dat >= 0: - ha, va = "left", "center" - elif orientation == "horizontal" and dat < 0: - ha, va = "right", "center" + if orientation == "vertical": + ha = 'center' + va = 'top' if dat < 0 else 'bottom' # also handles NaN + elif orientation == "horizontal": + ha = 'right' if dat < 0 else 'left' # also handles NaN + va = 'center' + + if np.isnan(dat): + lbl = '' annotation = self.annotate(fmt % value if lbl is None else lbl, xy, xytext, textcoords="offset points", diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 4bcacef59eb3..620d7b10bd4d 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -7004,6 +7004,15 @@ def test_bar_label_labels(): assert labels[1].get_text() == 'B' +def test_bar_label_nan_ydata(): + ax = plt.gca() + bars = ax.bar([2, 3], [np.nan, 1]) + labels = ax.bar_label(bars) + assert [l.get_text() for l in labels] == ['', '1'] + assert labels[0].xy == (2, 0) + assert labels[0].get_va() == 'bottom' + + def test_patch_bounds(): # PR 19078 fig, ax = plt.subplots() ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, 0.1))
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: