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
base:main
Are you sure you want to change the base?
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.🚀
| returnslices,texts,autotexts | ||
| returnpc | ||
| defpie_label(self,container,labels,*,distance=0.6, |
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.
| defpie_label(self,container,labels,*,distance=0.6, | |
| defpie_label(self,container,/,labels,*,distance=0.6, |
A bit unconventional, but we could makecontainer positional-only so that we can more easily rename it should the need arise.
| The ``PieContainer`` name is provisional and may change in future to reflect | ||
| development of its functionality. |
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.
This could have a more prominent styling. If you think warning is too much, I'm also ok with note
| The``PieContainer``nameisprovisionalandmaychangeinfuturetoreflect | |
| developmentofitsfunctionality. | |
| ..warning:: | |
| Theclassname``PieContainer``nameisprovisionalandmaychangeinfuture | |
| toreflectdevelopmentofitsfunctionality. |
| # escape % (i.e. \%) if it is not already escaped | ||
| labels= [re.sub(r"([^\\])%",r"\1\\%",s)forsinlabels] | ||
| elif (nw:=len(container.wedges))!= (nl:=len(labels)): | ||
| raiseValueError(f'Found{nw} wedges but{nl} labels') |
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.
Optional: State what is required not just what is present.
| raiseValueError(f'Found{nw} wedges but{nl} labels') | |
| raiseValueError( | |
| f'The number of labels ({nl}) must match the number of wedges ({nw})') |
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:
pie_labelfunctionality should be covered by existingpietests that use thelabels and/orautopct parameters. Is is better to duplicate this coverage?PieContainerinherit 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