从饼图matplotlib饼图获取图例中的百分比?

Car*_*.py 1 python charts matplotlib

我使用下面的matplotlib创建了一个饼图:

labels = ['dogs','cats','birds','fish']
sizes = [34, 24,18,13]
pie = plt.pie(sizes,autopct='%1.1f%%', startangle=90)
plt.axis('equal')
plt.legend( loc = 'right', labels=labels)
plt.show()
Run Code Online (Sandbox Code Playgroud)

(对不起,我不知道如何在这里显示饼图)

有没有办法将这些百分比放在图例中,以便图例显示:

狗,34%

猫,24%

鸟类,18%

鱼,13%

我知道我可以改变"标签"来阅读上面作为最快和最优雅的方式,但是如果在代码运行之前你不知道"大小"怎么办?

Ger*_*ges 5

我假设当你绘制传奇时,你应该知道sizes.像这样的东西会这样做:

plt.legend( loc = 'right', labels=['%s, %1.1f %%' % (l, s) for l, s in zip(labels, sizes)])