简体   繁体   中英

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

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM