Skip to content

Conversation

@ilakkmanoharan
Copy link
Contributor

PR summary

This pull request fixes issues with the grouped_bar method in Axes related to legend labels, bar colors, and hatch patterns.

Why is this change necessary?

Previously, single labels passed to grouped_bar were not applied to all bars, resulting in empty legends. Additionally, color and hatch patterns were not consistently applied across all grouped bars, and passing label and color twice could cause a TypeError.

What problem does it solve?

Ensures that a single label string propagates to all datasets.
Correctly applies colors and hatch patterns for each bar.
Prevents TypeError when labels or colors are passed multiple times.
Updates tests to verify proper handling of single and multiple labels.

What is the reasoning for this implementation?

By expanding single labels to a list internally and correctly mapping colors and hatches to each dataset, we preserve intuitive usage of grouped_bar and maintain correct legend, color, and hatch behavior without changing the API.

Example usage:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x = np.arange(2)
heights = [np.array([1, 2]), np.array([2, 3])]
ax.grouped_bar(
    heights,
    positions=x,
    labels=["dataset1", "dataset2"],
    color=["C0", "C1"],
    hatch=["//", "xx"]
)
ax.legend()
plt.show()

PR checklist

closes [Bug]: grouped_bar can pass label and color twice, causing TypeError #30739

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

This PR is not properly scoped. It contains the commits from your separate PR on hatch handling, which has nothing to do with #30739.

Please take more care when creating PRs.

@ilakkmanoharan
Copy link
Contributor Author

This PR is not properly scoped. It contains the commits from your separate PR on hatch handling, which has nothing to do with #30739.

Please take more care when creating PRs.

Thank you for the feedback, Tim

This PR includes the commits from the hatch-handling PR because the fix also needs to incorporate hatch behavior, just like it does for label and color. Since those hatch-related updates are not yet in main, the work for this issue had to be built on top of that PR. Once the hatch-handling PR is merged, this PR will show only the intended diff.

@timhoffm
Copy link
Member

Thanks for the clarification. For the future, please note such an aspect in the PR message. It's an important piece of information for reviewers.

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.

3 participants