Skip to content

FIX: use requested style for empty bar charts. - #31973

Open
vossvayne wants to merge 1 commit into
matplotlib:mainfrom
vossvayne:issue-21506
Open

vossvayne wants to merge 1 commit into
matplotlib:mainfrom
vossvayne:issue-21506

Conversation

@vossvayne

Copy link
Copy Markdown

PR summary

Issue: Bar has no child patches when input data is empty, this leads to the legend handle being left at default as there are no children.

Fix: Store the style information as a fallback and use it when there are no children. This closes #21506.

Example:

import matplotlib.pyplot as plt

ax = plt.figure().add_subplot()
ax.bar([], [], color="red", alpha=0.3, label="empty")
handler = ax.legend().legend_handles[0]
print(handler.get_facecolor()) # (1.0, 0.0, 0.0, 0.3)
print(handler.get_alpha()) # 0.3

AI Disclosure

Fixed manually.

PR checklist

@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.

ax.bar([], [], color='red', alpha=0.3, label='empty')
assert mpl.colors.same_color(
ax.legend().legend_handles[0].get_facecolor(),
mpl.colors.to_rgba('red', 0.3),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You can pass this directly instead of eager resolving it.

Suggested change
mpl.colors.to_rgba('red', 0.3),
('red', 0.3),

Comment on lines +666 to +667
def test_empty_bar_chart_legend_style():
# issue #21506

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
def test_empty_bar_chart_legend_style():
# issue #21506
def test_empty_bar_chart_legend_style():

facecolor='red', edgecolor='black', linewidth=3,
label='styled', hatch='//')
handle = ax.legend().legend_handles[0]
assert mpl.colors.same_color(handle.get_edgecolor(), 'black')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should you not be checking facecolor as well?

This branch has not been deployed

No deployments
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