这是我目前的代码
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);
目前,它只显示百分比(使用autopct
)。
我想说明百分比和实际价值(我不介意这个职位)。
该怎么做呢?
谢谢
发布于 2020-01-08 11:36:20
创建您自己的格式函数。请注意,您必须从该函数中的百分比中重新计算实际值。
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);
发布于 2022-03-17 15:32:28
@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))
这是我的输出:
注意:如果您想要更多的小数,只需更改返回my_format(pct)中的数字即可。
https://stackoverflow.com/questions/59644751
复制相似问题