如何标记饼图?

时间:2018-10-31 06:29:15

标签: python pandas pie-chart

我的stud_alcoh数据集如下所示

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

我必须绘制一个数字为数字的饼图,True为111,False284。我写了number_of_drinkers.plot(kind='pie') 哪个Y标签以及数字(284和111)没有标签

2 个答案:

答案 0 :(得分:1)

这应该有效:

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

autopct参数为您提供了绘图内百分比的表示法,并在字母“ f”之前指示了所需的小数位数。因此,您可以将其更改为%.1f %%(仅保留一个小数)。 我个人不知道显示原始数字的方法,而仅显示百分比,但据我所知,这是馅饼的目的。

答案 1 :(得分:0)

您的问题已经有了很好的答案。您也可以尝试一下。我正在使用您共享的数据框。

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