簡體   English   中英

如何標記餅圖?

[英]How to label the 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

我必須用 number_of_drinkers 繪制餅圖,True 為 111,False 284。我寫了number_of_drinkers.plot(kind='pie')其中 Y 標簽以及 number(284 和 111) 未標記

這應該有效:

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

autopct 參數為您提供繪圖內的百分比符號,在字母“f”之前指示所需的小數位數。 例如,您可以將其更改為 %.1f%% 僅保留一位小數。 我個人不知道有什么方法可以顯示里面的原始數字,而只顯示百分比,但據我所知,這就是餡餅的目的。

你的問題已經有了很好的答案。 你也可以試試這個。 我正在使用您共享的數據框。

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM