Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add group_positions attribute to BarContainer - #32282

Merged
story645 merged 9 commits into
matplotlib:mainfrom
robertoffmoura:add-grouped-bar-metadata
Sep 8, 2026
Merged

Add group_positions attribute to BarContainer#32282
story645 merged 9 commits into
matplotlib:mainfrom
robertoffmoura:add-grouped-bar-metadata

Conversation

@robertoffmoura

@robertoffmoura robertoffmoura commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

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

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.

@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
Contributor 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
Contributor Author

Thanks. I just added an entry.

Comment thread doc/release/next_whats_new/new_barcontainer_properties.rst Outdated
Comment thread doc/release/next_whats_new/new_barcontainer_properties.rst Outdated
Comment thread doc/release/next_whats_new/new_barcontainer_properties.rst Outdated
@robertoffmoura

robertoffmoura commented Sep 7, 2026 via email

Copy link
Copy Markdown
Contributor Author

@robertoffmoura

Copy link
Copy Markdown
Contributor Author

The failure in circleci is due to embedding_in_wx5_sgskip.rst and is unrelated to this change. @story645 Could you please have a look at the PR?

@story645
story645 merged commit 8a621d3 into matplotlib:main Sep 8, 2026
38 of 40 checks passed
@story645

story645 commented Sep 8, 2026

Copy link
Copy Markdown
Member

Thanks for the contribution and putting up w/ the nits on the release notes!

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.

4 participants