Skip to content

Commit aff4ae9

Browse files
stefmolintimhoffm
andauthored
Apply suggestions from code review
Co-authored-by: Tim Hoffmann <[email protected]>
1 parent 62fdc1f commit aff4ae9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,8 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
26252625
fmt : str or callable, default: '%g'
26262626
An unnamed %-style or {}-style format string for the label or a
26272627
function to call with the value as the first argument.
2628-
When *fmt* is a string, %-style takes precedence over {}-style.
2628+
When *fmt* is a string and can be interpreted in both formats,
2629+
%-style takes precedence over {}-style.
26292630
26302631
label_type : {'edge', 'center'}, default: 'edge'
26312632
The label type. Possible values:
@@ -2748,10 +2749,12 @@ def sign(x):
27482749
lbl = ''
27492750

27502751
if lbl is None:
2751-
try:
2752-
lbl = fmt(value)
2753-
except TypeError:
2752+
if isinstance(fmt, str):
27542753
lbl = cbook._auto_format_str(fmt, value)
2754+
elif callable(fmt):
2755+
lbl = fmt(value)
2756+
else:
2757+
raise TypeError("fmt must be a str or callable")
27552758
annotation = self.annotate(lbl,
27562759
xy, xytext, textcoords="offset points",
27572760
ha=ha, va=va, **kwargs)

0 commit comments

Comments
 (0)