Closed
Description
Bug summary
It seems a bug was introduced in Matplotlib 3.8, in which numeric labels for bar charts are no longer getting converted to strings. When trying to add a legend, this causes an AttributeError: 'int' object has no attribute 'startswith'
.
Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
plt.bar(np.arange(10), np.random.rand(10), label=1)
plt.legend() # fails
Actual outcome
File /software/anaconda3/lib/python3.11/site-packages/matplotlib/axes/_axes.py:322 in legend
handles, labels, kwargs = mlegend._parse_legend_args([self], *args, **kwargs)
File /software/anaconda3/lib/python3.11/site-packages/matplotlib/legend.py:1361 in _parse_legend_args
handles, labels = _get_legend_handles_labels(axs, handlers)
File /software/anaconda3/lib/python3.11/site-packages/matplotlib/legend.py:1291 in _get_legend_handles_labels
if label and not label.startswith('_'):
AttributeError: 'int' object has no attribute 'startswith'
Expected outcome
Legend should work and show "1" as the label (as it did in Matplotlib 3.7).
Additional information
- It seems this bug was introduced in 3.8.0 (it worked in 3.7.1).
- It's only
plt.bar()
that's affected;plt.plot()
for example works fine. - Converting manually to a string (
label=str(1)
) prevents the error. - Possibly unrelated, but I'm wondering if it has anything to do with this change regarding the
startswith
conditional.
Operating system
Ubuntu
Matplotlib Version
3.8.0
Matplotlib Backend
Qt5Agg
Python version
3.11.3
Jupyter version
No response
Installation
conda