本文介绍了Matplotlib饼图:如何用绝对值替换自动标记的相对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a pie-chart according to the matplotlib-demo: https://matplotlib.org/1.2.1/examples/pylab_examples/pie_demo.html

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

The percentage of each frac seems to be auto-labelled. How can I replace these auto-labelled relative values (%) plotted on the pie-chart by absolute values from 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除以将百分比转换回原始值:

so you can turn the percentages back into original values by multiplying by the total size of the pie and dividing by 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()

这篇关于Matplotlib饼图:如何用绝对值替换自动标记的相对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:52