Skip to content

[Bug]: Legend fails for bar plot with numeric label #26824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cliffckerr opened this issue Sep 19, 2023 · 1 comment · Fixed by #26825
Closed

[Bug]: Legend fails for bar plot with numeric label #26824

cliffckerr opened this issue Sep 19, 2023 · 1 comment · Fixed by #26825
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug
Milestone

Comments

@cliffckerr
Copy link

cliffckerr commented Sep 19, 2023

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

@oscargus
Copy link
Member

It is related to #26326

Apparently, we had no test for non-string labels...

Fixed in #26825

Thanks for reporting and sorry for breaking this.

@oscargus oscargus added status: confirmed bug Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. labels Sep 19, 2023
@oscargus oscargus added this to the v3.8.1 milestone Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants