Skip to main content
 首页 » 编程设计

python之具有小值的 Pyplot 饼图错误

2024年10月24日11开发

我尝试在饼图上绘制一组小值,但得到以下结果:

values = [0.077, 0.028, 0.006, 0.149, 0.081] 
 
fig = plt.figure(figsize=(6,6)) 
ax = fig.add_subplot(111) 
ax.pie(values, startangle=90) 
plt.show() 

但是,我只需将这些值乘以 10 即可得到正确的结果:

values = [v*10 for v in values] 

知道为什么会发生这种情况,以及如何解决它吗?

(注意:我使用的是 Python 2.7、matplotlib 1.4.3,并且我正在从 IPython 笔记本运行脚本)

请您参考如下方法:

这不是错误,而是预期的行为。来自documentation :

Make a pie chart of array x. The fractional area of each wedge is given by x/sum(x). If sum(x) <= 1, then the values of x give the fractional area directly and the array will not be normalized.

它看起来不像pyplot.pie对于总和小于 1 的数组,有一个自动归一化数组的选项,所以如果你想填充饼图,我建议你自己归一化它们(或者只是将它们乘以某个数字以确保总和为 > 1 ,正如您已经发现的那样)。