【问题标题】:Increasing the label size in matplotlib in pie chart在饼图中增加 matplotlib 中的标签大小
【发布时间】:2021-07-05 08:22:20
【问题描述】:

我有以下字典

{'Electronic Arts': 66,
 'GT Interactive': 1,
 'Palcom': 1,
 'Fox Interactive': 1,
 'LucasArts': 5,
 'Bethesda Softworks': 9,
 'SquareSoft': 3,
 'Nintendo': 142,
 'Virgin Interactive': 4,
 'Atari': 7,
 'Ubisoft': 28,
 'Konami Digital Entertainment': 11,
 'Hasbro Interactive': 1,
 'MTV Games': 1,
 'Sega': 11,
 'Enix Corporation': 4,
 'Capcom': 13,
 'Warner Bros. Interactive Entertainment': 7,
 'Acclaim Entertainment': 1,
 'Universal Interactive': 1,
 'Namco Bandai Games': 7,
 'Eidos Interactive': 9,
 'THQ': 7,
 'RedOctane': 1,
 'Sony Computer Entertainment Europe': 3,
 'Take-Two Interactive': 24,
 'Square Enix': 5,
 'Microsoft Game Studios': 22,
 'Disney Interactive Studios': 2,
 'Vivendi Games': 2,
 'Sony Computer Entertainment': 52,
 'Activision': 45,
 '505 Games': 4}

现在我面临的问题是查看标签。标签非常小且不可见。 请任何人都可以建议如何增加标签大小。 我试过下面的代码:

plt.figure(figsize=(80,80))
plt.pie(vg_dict.values(),labels=vg_dict.keys())
plt.show()

【问题讨论】:

    标签: matplotlib data-visualization pie-chart


    【解决方案1】:

    plt.pie 方法中添加textprops 参数:

    plt.figure(figsize=(80,80))
    plt.pie(vg_dict.values(), labels=vg_dict.keys(), textprops={'fontsize': 30})
    plt.show()
    

    你可以查看Text对象here的所有属性。

    更新

    我不知道您的标签顺序是否重要?为避免标签重叠,您可以尝试修改起始角度(plt 从 x 轴逆时针开始绘制饼图),并将“拥挤”标签重新排序:

    vg_dict = {
        'Palcom': 1,
        'Electronic Arts': 66,
        'GT Interactive': 1,
        'LucasArts': 5,
        'Bethesda Softworks': 9,
        'SquareSoft': 3,
        'Nintendo': 142,
        'Virgin Interactive': 4,
        'Atari': 7,
        'Ubisoft': 28,
        'Hasbro Interactive': 1,
        'Konami Digital Entertainment': 11,
        'MTV Games': 1,
        'Sega': 11,
        'Enix Corporation': 4,
        'Capcom': 13,
        'Acclaim Entertainment': 1,
        'Warner Bros. Interactive Entertainment': 7,
        'Universal Interactive': 1,
        'Namco Bandai Games': 7,
        'Eidos Interactive': 9,
        'THQ': 7,
        'RedOctane': 1,
        'Sony Computer Entertainment Europe': 3,
        'Take-Two Interactive': 24,
        'Vivendi Games': 2,
        'Square Enix': 5,
        'Microsoft Game Studios': 22,
        'Disney Interactive Studios': 2,
        'Sony Computer Entertainment': 52,
        'Fox Interactive': 1,
        'Activision': 45,
        '505 Games': 4}
    
    plt.figure(figsize=(80,80))
    plt.pie(vg_dict.values(), labels=vg_dict.keys(), textprops={'fontsize': 35}, startangle=-35)
    plt.show()
    

    结果:

    【讨论】:

    • 它工作正常,但我遇到的一个问题是标签重叠,一些标签重叠,因为饼图很小。
    • 嘿@PythonLearner,我已经更新了帖子。
    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多