更改matplotlib饼图上的autopct标签位置 [英] Change autopct label position on matplotlib pie chart
本文介绍了更改matplotlib饼图上的autopct标签位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在matplotlib中绘制一些饼图,并且我的一些图表上的百分比标签彼此重叠并且看起来很乱.有没有一种方法可以更改文本的位置,使文本全部可读? 下面是我得到的示例-类别其他"和"ALD2_OH"重叠且不可读.
I'm plotting some pie charts in matplotlib and the percentage labels on some of my charts overlap each other and look messy. Is there a way to change the position of the text so it is all readable? And example of what I'm getting is below - the category 'Others' and 'ALD2_OH' are overlapping and are unreadable.
我的绘图代码在这里:
matplotlib.rcParams.update({'font.size': 18})
plt.figure(figsize=(11,11))
labels = ['ALD2 + OH','PAN + $therm$','ALD2 + NO$_3$','ATOOH + $hv$',
'Others',]
colours = ['BlueViolet','DarkMagenta','DarkOrchid','DarkViolet','Purple'
]
patches, texts,autotexts = plt.pie(main_producers, labels=labels, colors = colours,
autopct='%1.1f%%', startangle = 90)
plt.title('Contribution to MCO$_3$ yeild')
希望有人可以提供帮助!
Hope someone can help!
谢谢
推荐答案
您可能想将autotexts
窄楔形物从楔形物的中心沿半径移动:
You might want to move autotexts
of narrow wedges from the center of the wedge along the radius:
for patch, txt in zip(patches, autotexts):
# the angle at which the text is located
ang = (patch.theta2 + patch.theta1) / 2.
# new coordinates of the text, 0.7 is the distance from the center
x = patch.r * 0.7 * np.cos(ang*np.pi/180)
y = patch.r * 0.7 * np.sin(ang*np.pi/180)
# if patch is narrow enough, move text to new coordinates
if (patch.theta2 - patch.theta1) < 10.:
txt.set_position((x, y))
这产生了(我在某种程度上模拟了您的数据):
This yields (I simulated your data to some extent):
这篇关于更改matplotlib饼图上的autopct标签位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文