Matplotlib饼图标签对齐 [英] Matplotlib Pie Chart Labels Alignment

查看:171
本文介绍了Matplotlib饼图标签对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在matplotlib中创建一个饼图,并希望将标签放在楔形中。我使用以下代码来做到这一点:

I was trying to create a pie chart in matplotlib and would like to put the labels within wedges. I used the following code to do it:

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()

标签似乎在楔块内未正确对齐,如下图所示。有什么方法可以修改(或旋转)标签,以便可以在楔块内正确显示标签?

It seems that the labels are not properly aligned within wedges as shown in the image below. Is there any way to modify(or rotate) the labels so that they can be shown properly inside the wedges?

谢谢!

推荐答案

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

Adjusting the alignment of the labels after they are returned should do the trick:

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度。通常最好还是在不同的帖子中列出单独的问题。

I don't really understand the second question, as it seem you have already moved the slices by 10 degrees using the startangle parameter. It is typically better to list separate questions in different posts anyhow.

这篇关于Matplotlib饼图标签对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆