Skip to content

DOC: simplify hat graph example #30459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2025
Merged

DOC: simplify hat graph example #30459

merged 1 commit into from
Aug 24, 2025

Conversation

rcomer
Copy link
Member

@rcomer rcomer commented Aug 23, 2025

PR summary

We can use grouped_bar and bar_label to replace the bespoke logic in this example.

PR checklist

@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Aug 23, 2025
@rcomer rcomer force-pushed the hat-graph branch 2 times, most recently from 0473092 to 909a928 Compare August 23, 2025 21:45
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.

Nice simplification!

xytext=(0, 4), # 4 points vertical offset.
textcoords='offset points',
ha='center', va='bottom')

values = np.asarray(values)
x = np.arange(values.shape[1])
ax.set_xticks(x, labels=xlabels)
Copy link
Member

Choose a reason for hiding this comment

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

Can we remove this? I believe grouped_bar(..., tick_labels=...) already cares for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ooops, yes.


# Draw the hats
bars = ax.grouped_bar(
(values - heights0).T, bottom=heights0, tick_labels=xlabels,
Copy link
Member

Choose a reason for hiding this comment

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

Slight preference to inline heights0 = values[0]. I think that extra variable made sense when we had heights in the for loop, but now only introduces additional mental load without any benefit.

Suggested change
(values - heights0).T, bottom=heights0, tick_labels=xlabels,
(values - values[0]).T, bottom=values[0], tick_labels=xlabels,

Comment on lines 46 to 44
# Remove the color that would otherwise show in the first group's legend entry
bars.bar_containers[0].patches[0].set_facecolor('none')
Copy link
Member

Choose a reason for hiding this comment

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

Better do as grouped_bar(..., color=['none', 'C1'])?

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually did that first, but then noticed the docstring assumes an arbitrary number of groups (M). The linked paper only uses two groups. I didn’t find many examples with an image search, but this does show more groups.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have left it for now, but happy to restrict the function to two groups. I was also not a particular fan of this line when I wrote it!

Copy link
Member

@timhoffm timhoffm Aug 24, 2025

Choose a reason for hiding this comment

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

Fair point. Since the colors don’t need to be the exact length, you could do color=[„none“] + all_colors_in_the_color_cycle. That would also make the first color C0, which may be more appropriate.

Or slice all_colors_in_the_color_cycle to the appropriate size if you want to be more explicit.

@timhoffm timhoffm modified the milestones: v3.10-doc, v3.11.0 Aug 24, 2025
@timhoffm timhoffm merged commit a617ece into matplotlib:main Aug 24, 2025
22 checks passed
@rcomer rcomer deleted the hat-graph branch August 24, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy