Add group_positions attribute to BarContainer by robertoffmoura · Pull Request #32282 · matplotlib/matplotlib · GitHub
Skip to content

Add group_positions attribute to BarContainer - #32282

Open
robertoffmoura wants to merge 3 commits into
matplotlib:mainfrom
robertoffmoura:add-grouped-bar-metadata
Open

Add group_positions attribute to BarContainer#32282
robertoffmoura wants to merge 3 commits into
matplotlib:mainfrom
robertoffmoura:add-grouped-bar-metadata

Conversation

@robertoffmoura

@robertoffmoura robertoffmoura commented Sep 3, 2026

Copy link
Copy Markdown

Add group_positions attribute to BarContainer.

Motivation and Context

In Axes.grouped_bar(), BarContainer instances are placed at calculated fractional coordinates (lefts). However, the resulting BarContainer instances in ax.containers do not record the shared categorical group_centers from which those fractional offsets were derived.

This makes it difficult for downstream converters (such as interactive exporters like Plotly/matplotlylib) and inspection tools to access the intended group coordinates without resorting to reverse-engineering layout geometry.

Following the existing pattern of BarContainer.datavalues (added for bar_label) and BarContainer.orientation:

  • Independent ax.bar() containers retain group_positions = None.
  • Containers created by Axes.grouped_bar() have bc.group_positions = group_centers.

Proposed Changes

  1. Add optional group_positions=None parameter and attribute to BarContainer in lib/matplotlib/container.py and its type stub container.pyi.
  2. In Axes.grouped_bar(), set bc.group_positions = group_centers.
  3. Assert bc.group_positions in test_grouped_bar_return_value.

Backwards Compatibility

100% backwards-compatible. Standard ax.bar() calls default to group_positions = None.

AI Disclosure

AI was used to gain understanding of the existing code and to proof-read the PR description.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

@story645

story645 commented Sep 3, 2026

Copy link
Copy Markdown
Member

Thanks for the PR! can you please fill out the AI disclosure bit? Would it also be useful to then provide more information, like bar width, given width also seems to be calculated internally?

Return the list of bar widths (perpendicular to height), mirroring
bottoms, tops, and position_centers. Works consistently for all bar
plots across Matplotlib.
@robertoffmoura

Copy link
Copy Markdown
Author

Thanks for the review! I've updated the PR description with the AI disclosure section. Yes, a widths property would be helpful. I added a widths property to BarContainer. Could you please have another look?

@timhoffm timhoffm left a comment

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.

This is a reasonable addition. Thanks"

@story645 story645 added this to the v3.12.0 milestone Sep 4, 2026
@story645

story645 commented Sep 4, 2026

Copy link
Copy Markdown
Member

Thanks for the quick turn around. This just needs a what's new entry before it can be merged https://matplotlib.org/devdocs/devel/api_changes.html#release-notes

@robertoffmoura

Copy link
Copy Markdown
Author

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