Percentages in pie plot

How to show percentages also in pie plot ? I want to show percentages of “Will churn” and “Will not churn”.

I feel that you’ve been pointed to this before, but as a reminder, please read:

and follow the advice therein. In particular:

  1. Post actual code, not screenshots
  2. Make Minimum Working Examples for your questions which allow others to run the code you’re trying to run. This includes any packages used.

The second part is particularly important when asking about plotting packages - there are plenty of different options available, and answers are generally useful for one specific package only so asking a generic plotting question without specifying a package isn’t very useful.

i am using plots.jl and gr backend for plotting. code is lengthy so i haven’t putted it. you can tell me resources to read so that i could also add percentages in pie plot. i wasn’t able to find examples on internet to get percentqages in pie plot.

That’s why it’s called an MWE - the point is to only post code which illustrates your specific problem. In your case an MWE is:

julia> using Plots

julia> pie(["Y", "N"], [25, 75])

and one answer to annotating the plot is:

julia> annotate!([-0.25, 0.25], [-0.25, 0.25], ["75%", "25%"])

Sorry to revive the topic, but there seems to be no documentation available to seriously do this. The reply is also a bit disingenuous :frowning:

An ack of the (apparently) limited capabilities for these things would have been nice.

A new MWE

data = rand(1:10,100)
ps = [sum(data.==val)/100 for val in 1:10]
pie(["$(v)" for v in 1:10], ps)

How do we state the percentages here, so that they are properly located? That’s the question most people ending up here have, I think.

I hereby acknowledge that Plots.jl and Makie.jl need more work, it’s currently not easy to make pie plots with them that follow best practices.

There are other options though, like the one given here:

using PyPlot
pie([15,15,30,60], explode=[0,0.3,0,0], labels=["A","B","C","D"],
    autopct="%1.1f%%", shadow=true, startangle=90)

image

3 Likes

Thank you so much :slight_smile:

I think pie plots themselves are regarded as not being best practice, so I guess that’s why interest in them has been relatively low. The niche features take longer to implement because no one wants to do it :slight_smile:

1 Like

With Plots.jl, for the record:

using Plots, Printf

data = [125, 125, 250, 500]
labels = ["A", "B", "C", "D"]
tot = sum(data)
datapct = [@sprintf("%.1f%%", x/tot*100) for x in data]

θ = (cumsum(data) - data/2) .* 360/sum(data)
scθ = sincosd.(θ)

p = pie(labels, data)
for (s, sci) in zip(datapct, scθ)
    annotate!(0.6*sci[2], 0.6*sci[1], text(s, 9, :black))
end
p

1 Like

Totally! The last time I remember “needing” a pie chart was to show in a course how to do it with Makie. I thought of making a PR but that was not enough motivation. I’m waiting for a real use case.

I think there are cases where it’s good practice though: when you have just a few values and want to show for each what is the fraction of the total. See Have I Resolved the Pie Chart Debate?

1 Like