yas*_*eco 11 python matplotlib pie-chart plot-annotations
这是我当前的代码
values = pd.Series([False, False, True, True])
v_counts = values.value_counts()
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct='%.4f', shadow=True);
Run Code Online (Sandbox Code Playgroud)
目前,它仅显示百分比(使用autopct
)
我想同时显示百分比和实际值(我不介意位置)
Diz*_*ahi 16
创建您自己的格式化函数。请注意,您必须以某种方式根据该函数中的百分比重新计算实际值
def my_fmt(x):
print(x)
return '{:.4f}%\n({:.0f})'.format(x, total*x/100)
values = pd.Series([False, False, True, True, True, True])
v_counts = values.value_counts()
total = len(values)
fig = plt.figure()
plt.pie(v_counts, labels=v_counts.index, autopct=my_fmt, shadow=True);
Run Code Online (Sandbox Code Playgroud)
San*_*ado 12
@diziet-asahi 的代码对我不起作用,您可以使用:
def autopct_format(values):
def my_format(pct):
total = sum(values)
val = int(round(pct*total/100.0))
return '{:.1f}%\n({v:d})'.format(pct, v=val)
return my_format
plt.pie(mydata,labels = mylabels, autopct=autopct_format(mydata))
Run Code Online (Sandbox Code Playgroud)
这是我的输出:
注意:如果您想要更多小数,只需更改 my_format(pct) 返回中的数字即可