Skip to content

Apply requested bar style to legend entry of empty bar plots - #31974

Closed
yohaidajurno wants to merge 2 commits into
matplotlib:mainfrom
yohaidajurno:fix-21506-empty-bar-legend-style
Closed

yohaidajurno wants to merge 2 commits into
matplotlib:mainfrom
yohaidajurno:fix-21506-empty-bar-legend-style

Conversation

@yohaidajurno

Copy link
Copy Markdown

PR summary

Closes #21506.

When bar() is called with empty data, no Rectangle patches are created, so the BarContainer has no child artist for the legend handler to copy style from. The legend entry therefore fell back to the default style and the first color of the cycle, instead of the requested color/alpha — inconsistent with how plot() and scatter() handle empty data.

plt.bar([], [], color='red', alpha=0.3, label='x')
plt.legend()   # before: default blue swatch;  after: red, alpha 0.3

Fix

Stash a non-drawn sample Rectangle carrying the resolved style on the container, and let update_from_first_child fall back to it when the container has no children.

PR checklist

  • New regression test (test_empty_bar_chart_legend_handle_style)
  • test_legend.py + bar tests pass locally; ruff clean
  • Behavior-change note added under doc/api/next_api_changes/behavior/

🤖 Generated with Claude Code

yohaidajurno and others added 2 commits June 29, 2026 17:07
When `bar()` is called with empty data, no Rectangle patches are
created, so the BarContainer has no child artist for the legend handler
to copy style from. The legend entry therefore fell back to the default
style and the first color of the cycle, instead of the color/alpha/etc.
that were requested -- inconsistent with how plot() and scatter() handle
empty data.

Stash a non-drawn sample Rectangle carrying the resolved style on the
container and let update_from_first_child fall back to it when there are
no children, so the legend entry reflects the requested bar style.

Closes matplotlib#21506

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@rcomer

rcomer commented Jun 30, 2026

Copy link
Copy Markdown
Member

We do not accept AI generated PRs. Please see https://matplotlib.org/devdocs/devel/contribute.html#use-of-generative-ai

Also, there is already an open PR #31973 for this issue and we generally give priority to whichever is opened first.

@rcomer rcomer closed this Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: plt.bar legend displays incorrect style for empty data

2 participants