Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.1k
ENH: introduce PieContainer and pie_label method#30733
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
| rotate : bool, default: False | ||
| Rotate each label to the angle of the corresponding slice if true. | ||
| alignment : {'center', 'outer', 'auto'}, default: 'auto' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
'center' is how theautopct labels ofpie are aligned and 'outer' is how thelabels ofpie are aligned. We had some discussion about the horizontal alignment at#29152 (comment) and#29152 (comment), but I did not notice the extra complication for vertical alignment withrotate until I was writing the example.autopct does not have an associated rotate option, but I can report that rotated labels that were centred horizontally but not centred vertically looked bad!
timhoffm commentedNov 8, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I think we need to come up with a more generic concept. For a start, I'd say make your own type (i.e. copy the remove method) and define whatever minimal API is reasonable. If we know what kind of API we want to have based on this example it's easier to design a more general On a side-note we want this to not only be pure collections, but be a semantic entity, like Edit: The case is similar to |
timhoffm commentedNov 8, 2025
If writing from scratch, I would mostly test the functionality on |
867bd4a tobed6636CompareUh oh!
There was an error while loading.Please reload this page.
rcomer commentedNov 9, 2025
OK, |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
rcomer commentedNov 10, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
For complete backwards compatibility, we need "texts" to be returned as an empty list whenlabeldistance is In draft for now while I think about how to address that. Suggestions of course welcome! Edit: this is now addressed by the next push. |
rcomer commentedNov 10, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Apologies - just found some doc changes that I had missed before. None of the examples now unpack or index the return value. |
Uh oh!
There was an error while loading.Please reload this page.
| super().__init__(lines,**kwargs) | ||
| classPieContainer: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I am unsure on the name. I'm tempted to call this justPie on the background, that I envision this will become a compound semantic artist. The "Container" puts more focus on "aggregation of parts".
In that context, I'm pondering whetherpie() could/should become essentially
defpie(self,*args,**kwargs):pie=Pie(*args,**kwargs)self.add_artist(pie)returnpie
OTOH, full semantics may be more than we can easily achieve; e.g. assume a laterpie.set_values(new_values) changes the number of wedges. How does that affect explicitly set lists of labels or wedge properties?
Recommendation: Let's stick withPieContainer for now, but make then name provisional. - It's not needed for the basic use case of
pie = ax.pie(...)ax.pie_label(pie, ....)
timhoffm left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I've left some minor comments.
Overall a systematic and easy-to-review PR. Thanks for the effort.🚀
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
rcomer commentedNov 11, 2025
Thanks for all your reviewing work@timhoffm. Your latest suggestions seem good to me. |
rcomer commentedNov 18, 2025
It just occurred to me that if someone is usingpandas pie, they will not get the |
rcomer commentedNov 18, 2025
To be in the containers list we need at least a |
story645 commentedNov 18, 2025
Does it make sense to have a container interface/ABC to spell out the membership requirements for the containers list? |
timhoffm commentedNov 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
We need to define what exactly a container is. My understanding from the label machinery is that it is an artist that is realized through multiple base artists, but rather as an implementation detail. ErrorbarContainer uses multiple Line2D, but is logically still a single entity that shows up as one legend entry. I haven’t checked, but suspect that tracking containers explicitly in Axes may be done so that we can identify the artists to label (but it could also be that this was done purely to have a place to attach the involved artists to the figure, since historically we tracked different types of artists like lines, patches, etc. in their own lists and we certainly want to mix the lines of BarContainer with regular standalone lines). In contrast, Pie is a rather a concerted logical aggregation of multiple artists. A single label and legend entry does not make sense. I therefore believe that Pie is not a container as far as current container design goes. How to move forward? I wouldn’t jump extra hoops to support advanced plotting through the pandas plot API. It it’s a high-level API to ease plotting from dataframes, but you can always extract the column data and feed it to matplotlib if needed. we should specify the concept behind the type of aggregation we do with Pie more clearly. And we need to define how that is represented in internal structures. Is it one artist that can provide multiple legend entries? Or is it rather a grouping where the individual parts are tracked as separate entities in the Axes? Currently, and historically, wedges are added as individual patches. As long as we do this, we can live without adding Pie to the Axes as a minimal starting point. Agreed it would be nice to be able to retrieve it back, but it’s not that we are losing functionality. It’s just a yet to be implemented feature which requires additional design and refactoring, and I propose to rather do this as a follow up. |
story645 commentedNov 19, 2025
|
rcomer commentedNov 19, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
OK, I am happy to just drop the latest commit here. I was thinking that we need to support adding (at least) two sets of labels - once we deprecate |
rcomer commentedNov 19, 2025
I can confirm that if you put something without a |
rcomer commentedNov 19, 2025
OK, PR is now back to what@timhoffm already approved 😇 |
timhoffm commentedNov 20, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
BarContainer dues not reasonably handle labels either. There's some low-level hack in bar() to make this work. matplotlib/lib/matplotlib/axes/_axes.py Lines 2544 to 2549 in91b8a07
|
story645 commentedNov 20, 2025
Looks like it's doing an either use labels on individual artists or container level label. I think it's maybe a questionable API decision to make them mutually exclusive, but I don't think it's a hack. |
timhoffm commentedNov 21, 2025
Whatever you want to name it. The effect is that responsibility of label handling is distributed between the individual bars and the container. There is no easy way to change between the two approaches afterwards. But anyway, this is of no concern for |
story645 left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
nit you can take or leave but mostly fun 'cause I learned some new things.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| forfrac,label,explinzip(x,labels,explode): | ||
| x,y=center |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
🤨 til that loops have local scoping starting in v3
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
rcomer left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for the review@story645
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
rcomer commentedNov 24, 2025
Have I missed something I still need to do for this one? |
50fea43 intomatplotlib:mainUh oh!
There was an error while loading.Please reload this page.
story645 commentedNov 24, 2025
Thanks@rcomer for the new labeling capabilities! |
Uh oh!
There was an error while loading.Please reload this page.
PR summary
This PR introduces a new
pie_labelmethod for adding labels to the wedges of existing pie charts, following discussion in#29152. Similarly toBarContainer, we introduce aPieContaineras the new return value ofpie. This contains theWedgepatches and the numbers thatpie_labelrequires for automatically labelling the wedges with their values and/or fractions.Closes#19338
A few things I’m not sure about:
So far I have added very few new tests, since most of theAddressedpie_labelfunctionality should be covered by existingpietests that use thelabels and/orautopct parameters. Is is better to duplicate this coverage?ShouldAddressedPieContainerinherit fromContaineras I have currently done? It feels a bit wrong to me as I have also overwritten the__iter__method to provide backwards compatibility on the return value ofpie. So we do not get the standardContainerbehaviour that looping through it gives you the patches one by one. OTOH it is nice to get theremovemethod for free.pie_labelaccept a function for thelabels parameter, similar to theautopct parameter ofpie? Initially I thought no, because I do not know of a use case and the only example I found in the docs was the bakery one, which can now use the format string. However, supporting a function would allow us to move more of theautopct handling intopie_label, and remove the duplicate handling for the "usetex" case.PR checklist