Skip to content

Commit 2d85188

Browse files
committed
FIX: use data values in bar_label
1 parent 5f80961 commit 2d85188

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,10 +2908,8 @@ def sign(x):
29082908

29092909
if orientation == "vertical":
29102910
extrema = max(y0, y1) if dat >= 0 else min(y0, y1)
2911-
length = abs(y0 - y1)
29122911
else: # horizontal
29132912
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
2914-
length = abs(x0 - x1)
29152913

29162914
if err is None or np.size(err) == 0:
29172915
endpt = extrema
@@ -2920,11 +2918,6 @@ def sign(x):
29202918
else: # horizontal
29212919
endpt = err[:, 0].max() if dat >= 0 else err[:, 0].min()
29222920

2923-
if label_type == "center":
2924-
value = sign(dat) * length
2925-
else: # edge
2926-
value = extrema
2927-
29282921
if label_type == "center":
29292922
xy = (0.5, 0.5)
29302923
kwargs["xycoords"] = (
@@ -2967,9 +2960,9 @@ def sign(x):
29672960

29682961
if lbl is None:
29692962
if isinstance(fmt, str):
2970-
lbl = cbook._auto_format_str(fmt, value)
2963+
lbl = cbook._auto_format_str(fmt, dat)
29712964
elif callable(fmt):
2972-
lbl = fmt(value)
2965+
lbl = fmt(dat)
29732966
else:
29742967
raise TypeError("fmt must be a str or callable")
29752968
annotation = self.annotate(lbl,

lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from types import SimpleNamespace
1414
import unittest.mock
1515

16-
import dateutil.tz
16+
import dateutil
1717

1818
import numpy as np
1919
from numpy import ma
@@ -9503,6 +9503,13 @@ def test_nan_barlabels():
95039503
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
95049504

95059505

9506+
def test_int_fmt_bar_label():
9507+
fig, ax = plt.subplots()
9508+
bars = ax.bar(['foo', 'bar'], [5, 7])
9509+
labels = ax.bar_label(bars, fmt='{:d}')
9510+
assert [l.get_text() for l in labels] == ['5', '7']
9511+
9512+
95069513
def test_patch_bounds(): # PR 19078
95079514
fig, ax = plt.subplots()
95089515
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, width=0.1))

0 commit comments

Comments
 (0)