Skip to content

Commit bd14441

Browse files
authored
Merge pull request matplotlib#31819 from rcomer/bar_label-int
FIX: use data values in bar_label
2 parents 4bef190 + 2d85188 commit bd14441

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
@@ -2905,10 +2905,8 @@ def sign(x):
29052905

29062906
if orientation == "vertical":
29072907
extrema = max(y0, y1) if dat >= 0 else min(y0, y1)
2908-
length = abs(y0 - y1)
29092908
else: # horizontal
29102909
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
2911-
length = abs(x0 - x1)
29122910

29132911
if err is None or np.size(err) == 0:
29142912
endpt = extrema
@@ -2917,11 +2915,6 @@ def sign(x):
29172915
else: # horizontal
29182916
endpt = err[:, 0].max() if dat >= 0 else err[:, 0].min()
29192917

2920-
if label_type == "center":
2921-
value = sign(dat) * length
2922-
else: # edge
2923-
value = extrema
2924-
29252918
if label_type == "center":
29262919
xy = (0.5, 0.5)
29272920
kwargs["xycoords"] = (
@@ -2964,9 +2957,9 @@ def sign(x):
29642957

29652958
if lbl is None:
29662959
if isinstance(fmt, str):
2967-
lbl = cbook._auto_format_str(fmt, value)
2960+
lbl = cbook._auto_format_str(fmt, dat)
29682961
elif callable(fmt):
2969-
lbl = fmt(value)
2962+
lbl = fmt(dat)
29702963
else:
29712964
raise TypeError("fmt must be a str or callable")
29722965
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
@@ -9515,6 +9515,13 @@ def test_nan_barlabels():
95159515
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
95169516

95179517

9518+
def test_int_fmt_bar_label():
9519+
fig, ax = plt.subplots()
9520+
bars = ax.bar(['foo', 'bar'], [5, 7])
9521+
labels = ax.bar_label(bars, fmt='{:d}')
9522+
assert [l.get_text() for l in labels] == ['5', '7']
9523+
9524+
95189525
def test_patch_bounds(): # PR 19078
95199526
fig, ax = plt.subplots()
95209527
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, width=0.1))

0 commit comments

Comments
 (0)