我希望我使用 matplotlib 创建的饼图显示实际值而不仅仅是百分比。这是我的代码:
pie_shares= [i for i in mean.values()]
positions = [i for i in mean.keys()]
plt.pie(pie_shares,labels=positions, autopct='%1.1f%%', )
plt.show()
Run Code Online (Sandbox Code Playgroud)
如果要显示饼图切片的实际值,必须向标签提供这些值:
def autopct_format(values):
def my_format(pct):
total = sum(values)
val = int(round(pct*total/100.0))
return '{v:d}'.format(v=val)
return my_format
plt.pie(pie_shares, labels = positions, autopct = autopct_format(pie_shares))
Run Code Online (Sandbox Code Playgroud)
在 matplotlib 资源中,提到 autopct 可以是字符串格式和函数,因此,我们创建了一个自定义函数,用于格式化每个 pct 以显示此功能常用的百分比的实际值。
归档时间: |
|
查看次数: |
5858 次 |
最近记录: |