Skip to content

ENH: Add broadcasted hatch to grouped_bar - #30683

Closed
ilakkmanoharan wants to merge 19 commits into
matplotlib:mainfrom
ilakkmanoharan:enh/grouped-bar-hatch-broadcast
Closed

ENH: Add broadcasted hatch to grouped_bar#30683
ilakkmanoharan wants to merge 19 commits into
matplotlib:mainfrom
ilakkmanoharan:enh/grouped-bar-hatch-broadcast

Conversation

@ilakkmanoharan

Copy link
Copy Markdown
Contributor

PR summary

Closes #30673

Why is this change necessary?

Currently, Axes.grouped_bar supports color broadcasting (one color per dataset) but not hatch broadcasting.
Adding support for broadcasted hatch values improves group distinction and makes grouped_bar consistent with other multi-dataset plotting APIs such as hist.

What problem does it solve?

Without this feature, all grouped bars share the same hatch pattern, reducing visual clarity when color is insufficient or unavailable (e.g., grayscale printing).
This PR allows one hatch per dataset, improving accessibility and plot readability.

What is the reasoning for this implementation?

The design mirrors existing color broadcasting:

  • A single string applies the same hatch to all groups.
  • A sequence of strings applies one hatch per dataset.
  • Validation ensures length consistency.
    This follows Matplotlib’s API design for argument broadcasting and maintains parity with other plotting functions.

Summary of changes

  • Added hatch parameter to Axes.grouped_bar.
  • Supports str | Sequence[str] | None input types.
  • Normalizes and validates sequence length.
  • Applies hatches per dataset during bar creation.
  • Updated _axes.pyi type stub for the new argument.
  • Added new test file test_grouped_bar_hatch.py to cover:
    • Single hatch string.
    • Per-dataset sequence.
    • Length mismatch (raises ValueError).
    • Both vertical and horizontal orientations.
  • Updated grouped_bar docstring to include hatch broadcasting.

Example

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x = np.arange(3)
heights = [np.array([1, 2, 3]),
           np.array([2, 3, 4]),
           np.array([3, 4, 5])]
hatches = ['//', 'xx', '..']
ax.grouped_bar(heights, positions=x, hatch=hatches)
plt.show()

This produces grouped bars with distinct hatch patterns per dataset, matching color broadcasting behavior.

###PR checklist

 "closes #30673" is in the body of the PR description
 new and changed code is tested (test_grouped_bar_hatch.py)
 Plotting related feature is demonstrated in an example snippet above
 New Feature is noted with an API-change directive and docstring update
 Documentation complies with general and docstring guidelines

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

[ENH]: Add custom hatch styling to grouped_bar

4 participants