Matplotlib Pie Chart Labels Alignment

佐手、 提交于 2020-01-13 04:55:12

问题


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?

Thank you!


回答1:


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

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.



来源:https://stackoverflow.com/questions/18642315/matplotlib-pie-chart-labels-alignment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!