Skip to content

Commit 4e67326

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR matplotlib#31819: FIX: use data values in bar_label
1 parent 6782669 commit 4e67326

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

29082908
if orientation == "vertical":
29092909
extrema = max(y0, y1) if dat >= 0 else min(y0, y1)
2910-
length = abs(y0 - y1)
29112910
else: # horizontal
29122911
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
2913-
length = abs(x0 - x1)
29142912

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

2922-
if label_type == "center":
2923-
value = sign(dat) * length
2924-
else: # edge
2925-
value = extrema
2926-
29272920
if label_type == "center":
29282921
xy = (0.5, 0.5)
29292922
kwargs["xycoords"] = (
@@ -2966,9 +2959,9 @@ def sign(x):
29662959

29672960
if lbl is None:
29682961
if isinstance(fmt, str):
2969-
lbl = cbook._auto_format_str(fmt, value)
2962+
lbl = cbook._auto_format_str(fmt, dat)
29702963
elif callable(fmt):
2971-
lbl = fmt(value)
2964+
lbl = fmt(dat)
29722965
else:
29732966
raise TypeError("fmt must be a str or callable")
29742967
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
@@ -9459,6 +9459,13 @@ def test_nan_barlabels():
94599459
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
94609460

94619461

9462+
def test_int_fmt_bar_label():
9463+
fig, ax = plt.subplots()
9464+
bars = ax.bar(['foo', 'bar'], [5, 7])
9465+
labels = ax.bar_label(bars, fmt='{:d}')
9466+
assert [l.get_text() for l in labels] == ['5', '7']
9467+
9468+
94629469
def test_patch_bounds(): # PR 19078
94639470
fig, ax = plt.subplots()
94649471
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, width=0.1))

0 commit comments

Comments
 (0)