Matplotlib饼图标签对齐

时间:2013-09-05 17:20:22

标签: python charts matplotlib

我试图在matplotlib中创建一个饼图,并希望将标签放在楔形内。我使用以下代码来执行此操作:

import matplotlib.pyplot as plt

fig = plt.figure(1, figsize=(8,8), dpi=60)
ax=fig.add_axes([0.1,0.1,0.8,0.8])
labels = ['label0','label1','label2','label3','label4','label5','label6','label7','label8',\
          'label0','label1','label2','label3','label4','label5','label6','label7','label8']
colors = list('w' for _ in range(18))
fracs=list(20 for _ in range(18))
ax.pie(fracs, labels=labels, colors = colors, startangle=10,labeldistance=0.8)
plt.show()

似乎标签未在楔形内正确对齐,如下图所示。有没有办法修改(或旋转)标签,以便它们可以在楔形内正确显示?

谢谢!

Matplotlib generated image

1 个答案:

答案 0 :(得分:5)

返回标签后调整标签的对齐应该可以解决问题:

patches, texts = ax.pie(fracs, labels=labels, colors = colors, 
                        startangle=10, labeldistance=0.8)
for t in texts:
    t.set_horizontalalignment('center')

plt.show()

我真的不明白第二个问题,因为看起来你已经使用startangle参数将切片移动了10度。不管怎样,通常最好在不同的帖子中列出单独的问题。

相关问题