Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Closed
Labels
Milestone
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 anAttributeError: 'int' object has no attribute 'startswith'
.
Code for reproduction
importnumpyasnpimportmatplotlib.pyplotaspltplt.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:322inlegendhandles,labels,kwargs=mlegend._parse_legend_args([self],*args,**kwargs)File/software/anaconda3/lib/python3.11/site-packages/matplotlib/legend.py:1361in_parse_legend_argshandles,labels=_get_legend_handles_labels(axs,handlers)File/software/anaconda3/lib/python3.11/site-packages/matplotlib/legend.py:1291in_get_legend_handles_labelsiflabelandnotlabel.startswith('_'):AttributeError:'int'objecthasnoattribute'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 withthis 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