如何用绝对值替代 Matplotlib 中的自动标记的相对值?


如需使用绝对值替换 matplotlib 中自动标记的相对值,我们可以使用 autopct=lambda p: <计算结果>

步骤

  • 设置图表大小并调整子图表之间和周围的填充。
  • 列出标签、分数、偏移位置,并得到分数的总和来计算百分比。
  • 使用标签、分数偏移,以及autopct=lambda p: <计算百分比>绘制饼图。
  • 要显示图表,请使用show()方法。

示例

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

labels = ('Read', 'Eat', 'Sleep', 'Repeat')
fracs = [5, 3, 4, 1]
total = sum(fracs)
explode = (0, 0.05, 0, 0)

plt.pie(fracs, explode=explode, labels=labels,
         autopct=lambda p: '{:.0f}%'.format(p * total / 100),
         shadow=True, startangle=90)

plt.show()

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

更新日期:2021 年 6 月 15 日

635 次浏览

开启你的 职业生涯

完成课程以获取认证

开始
广告