0

I was trying to create a pie chart with percentages next to the graph. The data that i have is the following.

users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']

And i´m trying to get something like this.

enter image description here

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
fiona
  • 89
  • 7

2 Answers2

1

Try setting autopct to what you need:

plt.pie(users, 
        labels=os, 
        explode=[0, 0, 0.05, 0, 0, 0, 0],
        pctdistance = 1.2, 
        labeldistance = 1.4,
        autopct=lambda x: f'{x:.1f}%\n({(x/100)*sum(users):.0f} users)',
        textprops={"family": "Arial", "size": 12},
        radius = 2,
        colors = ["#9BC2E6", "#FF6600", "#F4B084", "#00B050", "#C6E0B4", "#8633FF", "#CCCCFF"]
        )
plt.legend(loc="best", bbox_to_anchor=(2.5,0), title="Operating systems")

Output:

enter image description here

not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • This worked wonders! The only thing is that I don´t know hot ot only put the percentages and the numbers of users in bold. Because when I try to do it, it also changes the operating systems. – fiona Oct 02 '21 at 14:39
1

Try using plotly.express:

import plotly.express as px
 
users = [80, 40, 1000, 300, 50, 80, 10]
os = ['MacOS', 'Chrome', 'Windows', 'Linux', 'Devian', 'Ubuntu', 'Arch Linux']
 
fig = px.pie(values=users, names=os, 
             color_discrete_sequence=px.colors.sequential.RdBu)
 
fig.update_traces(textposition='outside', 
                  textinfo='percent+label+value',
                  marker=dict(line=dict(color='#FFFFFF', width=2)),
                  textfont_size=12)
 
fig.show()

The result is quite nice:

enter image description here

ipj
  • 3,488
  • 1
  • 14
  • 18