Search code examples
pythonpandasdataframematplotlibpie-chart

How to show values in pandas pie chart?


I would like to visualize the amount of laps a certain go-kart has driven within a pie chart. To achive this i would like to count the amount of laptime groupedby kartnumber. I found there are two ways to create such a pie chart:

1#

df.groupby('KartNumber')['Laptime'].count().plot.pie() 

2#

df.groupby(['KartNumber']).count().plot(kind='pie', y='Laptime')

print(df)

print(df)
     HeatNumber  NumberOfKarts KartNumber DriverName  Laptime
0           334             11          5    Monique   53.862
1           334             11          5    Monique   59.070
2           334             11          5    Monique   47.832
3           334             11          5    Monique   47.213
4           334             11          5    Monique   51.975
...         ...            ...        ...        ...      ...
4053        437              2         20       luuk   39.678
4054        437              2         20       luuk   39.872
4055        437              2         20       luuk   39.454
4056        437              2         20       luuk   39.575
4057        437              2         20       luuk   39.648

Output not with plot:

KartNumber
1       203
10      277
11      133
12      244
13      194
14      172
15      203
16      134
17      253
18      247
19      240
2       218
20      288
21       14
4       190
5       314
6        54
60       55
61        9
62       70
63       65
64       29
65       53
66       76
67       42
68       28
69       32
8        49
9       159
None     13

As you can see i have the kartnumbers and count of laptimes. But i would like to show the count of laptimes within the pie chart(or legend). I tried using autopct but couldnt get it working properly. Does anyone knows how to achive my desired situation?

Edit: For more information on this dataset please see: How to get distinct rows from pandas dataframe?


Solution

  • Complete awswer:

        autopct=lambda x: '{:.0f}'.format(x * (df['Laptime'].count()) / 100))