Search code examples
pythonpandaspie-chart

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


Solution

  • 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.