Skip to content

FIX: use data values in bar_label#31819

Merged
timhoffm merged 1 commit into
matplotlib:mainfrom
rcomer:bar_label-int
Jun 4, 2026
Merged

FIX: use data values in bar_label#31819
timhoffm merged 1 commit into
matplotlib:mainfrom
rcomer:bar_label-int

Conversation

@rcomer
Copy link
Copy Markdown
Member

@rcomer rcomer commented Jun 4, 2026

PR summary

This is a minor issue, but...

Currently it is not possible to format bar labels as integers, even if the original data are ints:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
bars = ax.bar(['foo', 'bar'], [5, 7])
labels = ax.bar_label(bars, fmt='N={:d}')

plt.show()
Traceback (most recent call last):
  File "/[path-to-git-repo]/matplotlib/lib/matplotlib/cbook.py", line 2546, in _auto_format_str
    return fmt % (value,)
           ~~~~^~~~~~~~~~
TypeError: not all arguments converted during string formatting

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/[path-to-script]/bar_label_example.py", line 5, in <module>
    labels = ax.bar_label(bars, fmt='N={:d}')
  File "/[path-to-git-repo]/matplotlib/lib/matplotlib/axes/_axes.py", line 2970, in bar_label
    lbl = cbook._auto_format_str(fmt, value)
  File "/[path-to-git-repo]/matplotlib/lib/matplotlib/cbook.py", line 2548, in _auto_format_str
    return fmt.format(value)
           ~~~~~~~~~~^^^^^^^
ValueError: Unknown format code 'd' for object of type 'float'

The values used are derived from the size of the rectangle, and are therefore always floats. However, the actual data values are also available in this loop, so I'm not clear why we don't directly use them. I wondered if it had to do with unit handling, but the data values on the container have already been processed as height or width within bar

width = self._convert_dx(width, x0, x, self.convert_xunits)

height = self._convert_dx(height, y0, y, self.convert_yunits)

if orientation == 'vertical':
datavalues = height
else: # horizontal
datavalues = width

AI Disclosure

No AI used.

PR checklist

@story645
Copy link
Copy Markdown
Member

story645 commented Jun 4, 2026

Good catch!

@story645 story645 added this to the v3.11.0 milestone Jun 4, 2026
Copy link
Copy Markdown
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not using the original data is probably a historic implementation oversight. #15602 was created though many iterations and the datavalues were not originally part of the loop. They just came in via 21da4c1 and when that happened probably nobody thought about that this also simplifies and makes the value formatting more straightforward.

@timhoffm timhoffm merged commit bd14441 into matplotlib:main Jun 4, 2026
41 checks passed
@rcomer
Copy link
Copy Markdown
Member Author

rcomer commented Jun 4, 2026

Good catch!

I actually tripped over this one in my day job 😏

@rcomer rcomer deleted the bar_label-int branch June 4, 2026 20:58
@rcomer rcomer added the PR: bugfix Pull requests that fix identified bugs label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: bugfix Pull requests that fix identified bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants