Skip to content

[ENH]: Ability to Swap autopct and label positions #24789

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

Closed
jet082 opened this issue Dec 20, 2022 · 17 comments
Closed

[ENH]: Ability to Swap autopct and label positions #24789

jet082 opened this issue Dec 20, 2022 · 17 comments

Comments

@jet082
Copy link

jet082 commented Dec 20, 2022

Problem

It would be nice to be able to have the labels applied on top of pie chart slices, rather than on the edges. Likewise, it would be nice to have the percentages on the outside of the pie chart slices instead of on the inside.

Proposed solution

Allow an option to swap the autopct and label positions. Also, allow the autopct position to be autorotated via a flag like rotateautopct.

@vitorfrois
Copy link

Hello, I'd like to contribute on this issue.

@story645
Copy link
Member

story645 commented Dec 20, 2022

Hi @vitorfrois, I recommend that you wait until a few maintainers have chimed in on whether this feature should go into matplotlib and hashed out an API- if you have suggestions you're welcome to propose them. Otherwise (and possibly either way) this will get hashed out on the PR.

The request seems reasonable to me but I'm not sure whether the API would be simple enough to be feasible- do we want three more kwargs on this plotting method, is there a more straightforward way to do this (was thinking allow label and autoprct to take each other's kwargs but that will maybe be messy internal logic) or is it simple?

@vitorfrois
Copy link

Actually it has a very simple solution.
At def pie(...):, a swaplabelpct=False parameter was added. Inside the function, I have the statement
if swaplabelpct: labeldistance, pctdistance = pctdistance, labeldistance+0.05
(the +0.05 allows some space between %s and the chart)
And when I print a pie chat with swaplabelpct=True, I get the following result:
Figure_1

@story645
Copy link
Member

story645 commented Dec 20, 2022

If you've already implemented something, you're more than welcome to open the PR. I'm just trying to be upfront that it may not get in.

labeldistance+0.05

Does this padding work for all wedge sizes/ a wide range of percentages?

swaplabelpct=False

I think this is cleaner than my proposal (I was thinking of label and prct locations cause that would provide lots of flexibility) but I'm not totally sold on whether this or a more general solution makes more sense.

@jklymak
Copy link
Member

jklymak commented Dec 20, 2022

I'm not in favour of this request: labeldistance and pctdistance are both provided as kwargs. The user can swap these themselves, or put in whatever value they prefer, if they do not like the default values.

@story645
Copy link
Member

I'm not in favour of this request: labeldistance and pctdistance are both provided as kwargs.

I didn't know about those and should have checked 😶 but then maybe the resolution here is to better documenting that by adding a section to https://matplotlib.org/devdocs/gallery/pie_and_polar_charts/pie_and_donut_labels.html#sphx-glr-gallery-pie-and-polar-charts-pie-and-donut-labels-py

@jklymak
Copy link
Member

jklymak commented Dec 20, 2022

You can make it more explicit if you like, but the documentation is readily available - not everything needs to be spelled out explicitly in an example.

@story645
Copy link
Member

story645 commented Dec 20, 2022

not everything needs to be spelled out explicitly in an example.

Yeah, but I think feature requests are good candidates for making more explicit, and here it's adding a section to an example rather than creating a new one. I'd honestly just move them next to each other in the docs but can't in this case 'cause of the signature (though the existing order doesn't match the signature either...)
image

ETA: maybe make the docstring more explict: Set to 1.5 to put autoprct outside, set to less than 1 to put labels inside?

@oscargus
Copy link
Member

Are pctdistance and labeldistance using the same ""units"? Not so clear from the descriptions in that case....

@jklymak
Copy link
Member

jklymak commented Dec 21, 2022

They are the same, but they should be spelled the same here.

vitorfrois added a commit to vitorfrois/mytplotlib that referenced this issue Dec 21, 2022
)

A parameter in def pie(): allows the user to swap the position of %s and labels. It is set to False by default. Tested with basic graphs.
@timhoffm
Copy link
Member

I'd honestly just move them next to each other in the docs but can't in this case 'cause of the signature

Just do it. Reorder according to the signature and then move shadow one place. Nobody will notice.

Additionally, we can start a kwarg only deprecation at explode to have the ability of reordering the signature in the future.

@story645
Copy link
Member

Just do it. Reorder according to the signature and then move shadow one place. Nobody will notice.

I also have this question about the sanctity of the signature and matching things up w.r.t. #24470 b/c I currently put hatch next to color and really don't want it at the bottom.

story645 added a commit to story645/matplotlib that referenced this issue Dec 21, 2022
example
cleaned pct and label positioning arg docs b/c of matplotlib#24789
@oscargus
Copy link
Member

They are the same, but they should be spelled the same here.

OK! Then I think that spelling them the same way (not clear which, I may be inclined to think that radial distance is a bit clearer, although that is also expressed as a ratio, so the unit of the value is not so clear) will take this a long way.

@oscargus
Copy link
Member

I'm a bit against not having the parameters in the "correct" order, but on the other hand pro making most methods with gazillion arguments keyword only after the most obvious ones (like data being positional, styling kw-only).

(btw, the current doc-string is already out of order with normalize before labeldistance)

@timhoffm
Copy link
Member

I'm a bit against not having the parameters in the "correct" order,

The rough order should be the same, but I think nobody will be confused or complain if the signature is

pctdistance=0.6, shadow=False, labeldistance=1.1,

but the documentation is

pctdistance
labeldistance
shadow

Personally, I mostly ignore the parameters in the signature because the parameter list is much more readable: https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.pie.html#matplotlib.axes.Axes.pie
I only cross-check in very special cases.

@timhoffm
Copy link
Member

I also have this question about the sanctity of the signature and matching things up w.r.t. #24470 b/c I currently put hatch next to color and really don't want it at the bottom.

My take on this:

  • Signature is API. Reordering can only be done after making parameters keyword-only.
  • Generally, the order of the documented parameters should follow the signature.
  • Slight reordering of documented parameters is ok if we think that logical parameter grouping will be be more beneficial to the reader than the exact order-matching to the signature. If we deviate, we should start a keyword-only deprecation to be able to adjust the signature parameter order later.

story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
expanded basic pie example to include small examples of labels,
autopct, colors, and hatch
cleaned pct and label positioning arg docs b/c of matplotlib#24789
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
expanded basic pie example to include small examples of labels,
autopct, colors, hatch, and distance
reworded pctdistance and labeldistance b/c of matplotlib#24789
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
expanded basic pie example to include small examples of labels,
autopct, colors, hatch, and distance
reworded pctdistance and labeldistance b/c of matplotlib#24789

fixed link
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
expanded basic pie example to include small examples of labels,
autopct, colors, hatch, and distance
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Dec 22, 2022
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Dec 25, 2022
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 8, 2023
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 8, 2023
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 10, 2023
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 10, 2023
reworded pctdistance and labeldistance b/c of matplotlib#24789

Co-authored-by: Oscar Gustafsson <oscar.gustafsson@gmail.com>
Co-authored-by: sledziu32 <sledziu32@gmail.com>
Co-authored-by: Jody Klymak <jklymak@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 11, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789
story645 added a commit to story645/matplotlib that referenced this issue Jan 11, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789
story645 added a commit to story645/matplotlib that referenced this issue Jan 11, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 12, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 12, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 13, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 19, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 19, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
story645 added a commit to story645/matplotlib that referenced this issue Jan 19, 2023
added small examples of labels,
autopct, colors, hatch, and distance in an attempt to scaffold up to
exploding example
addresses some of matplotlib#24789

Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: David Stansby <dstansby@gmail.com>
@oscargus
Copy link
Member

oscargus commented Mar 5, 2023

@oscargus oscargus closed this as completed Mar 5, 2023
@QuLogic QuLogic added this to the v3.7.0 milestone Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants