user10508414
user10508414

Reputation:

How to label the Pie chart?

My stud_alcoh data set is given below

school  sex     age     address     famsize     Pstatus     Medu    Fedu    Mjob    Fjob    reason  guardian    legal_drinker
0   GP  F   18  U   GT3     A   4   4   AT_HOME     TEACHER     course  mother  True
1   GP  F   17  U   GT3     T   1   1   AT_HOME     OTHER   course  father  False
2   GP  F   15  U   LE3     T   1   1   AT_HOME     OTHER   other   mother  False
3   GP  F   15  U   GT3     T   4   2   HEALTH  SERVICES    home    mother  False
4   GP  F   16  U   GT3     T   3   3   OTHER   OTHER   home    father  False

number_of_drinkers = stud_alcoh.groupby('legal_drinker').size()
number_of_drinkers

legal_drinker
False    284
True     111
dtype: int64

I have to draw a pie chart with number_of_drinkers with True as 111 and False 284. I wrote number_of_drinkers.plot(kind='pie') which Y label and also the number(284 and 111) is not labeling

Upvotes: 1

Views: 174

Answers (2)

neehari
neehari

Reputation: 2612

Your question already has a good answer. You could also try this. I'm using the data frame you shared.

import pandas as pd

df = pd.read_clipboard()
df

school  sex age address famsize Pstatus Medu    Fedu    Mjob    Fjob    reason  guardian    legal_drinker
0   GP  F   18  U   GT3 A   4   4   AT_HOME TEACHER course  mother  True
1   GP  F   17  U   GT3 T   1   1   AT_HOME OTHER   course  father  False
2   GP  F   15  U   LE3 T   1   1   AT_HOME OTHER   other   mother  False
3   GP  F   15  U   GT3 T   4   2   HEALTH  SERVICES    home    mother  False
4   GP  F   16  U   GT3 T   3   3   OTHER   OTHER   home    father  False

number_of_drinkers = df.groupby('legal_drinker').size() # Series
number_of_drinkers

legal_drinker
False    4
True     1
dtype: int64

number_of_drinkers.plot.pie(label='counts', autopct='%1.1f%%') # Label the wedges with their numeric value

enter image description here

Upvotes: 0

BossaNova
BossaNova

Reputation: 1527

This should work:

number_of_drinkers.plot(kind = 'pie', label = 'my label', autopct = '%.2f%%')

The autopct argument gives you a notation of percentage inside the plot, with the desired number of decimals indicated right before the letter "f". So you can change this, for example, to %.1f%% for only one decimal. I personally don't know of a way to show the raw numbers inside but only the percentage, but to the best of my understanding this is the purpose of a pie.

Upvotes: 1

Related Questions