Skip to content

Commit 2053f81

Browse files
authored
Merge pull request #31823 from meeseeksmachine/auto-backport-of-pr-31819-on-v3.11.x
Backport PR #31819 on branch v3.11.x (FIX: use data values in bar_label)
2 parents a85d942 + 4e67326 commit 2053f81

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

29052905
if orientation == "vertical":
29062906
extrema = max(y0, y1) if dat >= 0 else min(y0, y1)
2907-
length = abs(y0 - y1)
29082907
else: # horizontal
29092908
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
2910-
length = abs(x0 - x1)
29112909

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

2919-
if label_type == "center":
2920-
value = sign(dat) * length
2921-
else: # edge
2922-
value = extrema
2923-
29242917
if label_type == "center":
29252918
xy = (0.5, 0.5)
29262919
kwargs["xycoords"] = (
@@ -2963,9 +2956,9 @@ def sign(x):
29632956

29642957
if lbl is None:
29652958
if isinstance(fmt, str):
2966-
lbl = cbook._auto_format_str(fmt, value)
2959+
lbl = cbook._auto_format_str(fmt, dat)
29672960
elif callable(fmt):
2968-
lbl = fmt(value)
2961+
lbl = fmt(dat)
29692962
else:
29702963
raise TypeError("fmt must be a str or callable")
29712964
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
@@ -9471,6 +9471,13 @@ def test_nan_barlabels():
94719471
assert np.allclose(ax.get_ylim(), (0.0, 3.0))
94729472

94739473

9474+
def test_int_fmt_bar_label():
9475+
fig, ax = plt.subplots()
9476+
bars = ax.bar(['foo', 'bar'], [5, 7])
9477+
labels = ax.bar_label(bars, fmt='{:d}')
9478+
assert [l.get_text() for l in labels] == ['5', '7']
9479+
9480+
94749481
def test_patch_bounds(): # PR 19078
94759482
fig, ax = plt.subplots()
94769483
ax.add_patch(mpatches.Wedge((0, -1), 1.05, 60, 120, width=0.1))

0 commit comments

Comments
 (0)