【问题标题】:Change autopct label position on matplotlib pie chart更改 matplotlib 饼图上的 autopct 标签位置
【发布时间】:2015-03-20 12:27:56
【问题描述】:

我正在 matplotlib 中绘制一些饼图,我的一些图表上的百分比标签相互重叠,看起来很乱。有没有办法改变文本的位置,所以它都是可读的? 下面是我得到的示例-“其他”和“ALD2_OH”类别重叠且不可读。

我的绘图代码在这里:

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

希望有人能帮忙!

谢谢

【问题讨论】:

    标签: python matplotlib label pie-chart


    【解决方案1】:

    您可能希望从楔形中心沿半径移动autotexts 窄楔形:

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

    这会产生(我在某种程度上模拟了您的数据):

    【讨论】:

    • 设计思路:将百分比移动到标签下的第二行?
    • 谢谢安德烈 - 这很好用。还要感谢 cphlewis - 这不是一个坏主意。我想我只是懒得让 python 自动计算百分比并绘制它们,我想我可以添加另一行来自己计算 %ages!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多