【问题标题】:How to display actual values instead of percentages on my pie chart using matplotlibs [duplicate]如何使用matplotlibs在我的饼图中显示实际值而不是百分比[重复]
【发布时间】:2019-05-15 21:27:02
【问题描述】:

我希望我使用 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()

【问题讨论】:

  • 只需删除“autopct=”部分。

标签: python matplotlib


【解决方案1】:

如果您想显示饼图切片的实际值,您必须向标签提供这些值:

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))

matplotlib 资源中提到 autopct 可以是字符串格式和函数,因此,我们创建了一个自定义函数,该函数将每个 pct 格式化,以显示该功能常用的百分比的实际值。

【讨论】:

  • 这行得通,但我也需要它来显示标题(职位)。我怎么能这样做?
  • 检查我的新编辑
  • return '{v:,}'.format(v=val) 用逗号分隔千位,...
猜你喜欢
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
相关资源
最近更新 更多