草庐IT

python - Matplotlib 饼图 : How to replace auto-labelled relative values by absolute values

coder 2023-08-16 原文

我正在根据 matplotlib-demo 创建一个饼图:https://matplotlib.org/1.2.1/examples/pylab_examples/pie_demo.html

每个 frac 的百分比似乎是自动标记的。如何用 fracs[] 中的绝对值替换饼图上绘制的这些自动标记的相对值 (%)?

最佳答案

help(pie) 说:

  *autopct*: [ *None* | format string | format function ]
    If not *None*, is a string or function used to label the
    wedges with their numeric value.  The label will be placed inside
    the wedge.  If it is a format string, the label will be ``fmt%pct``.
    If it is a function, it will be called.

因此您可以通过乘以饼图的总大小并除以 100 将百分比恢复为原始值:

figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
fracs = [15, 30, 45, 10]
total = sum(fracs)
explode=(0, 0.05, 0, 0)
pie(fracs, explode=explode, labels=labels,
    autopct=lambda(p): '{:.0f}'.format(p * total / 100),
    shadow=True, startangle=90)
show()

关于python - Matplotlib 饼图 : How to replace auto-labelled relative values by absolute values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171021/

有关python - Matplotlib 饼图 : How to replace auto-labelled relative values by absolute values的更多相关文章

随机推荐