【问题标题】:pie chart show percentage of specific values饼图显示特定值的百分比
【发布时间】:2020-09-02 11:52:36
【问题描述】:

文件由一些城市组和时间信息组成。我从 stackoverflow 周围的某个地方获取了一个代码并对其进行了一些更改:

fig, ax = plt.subplots(figsize=(8, 5.5), subplot_kw=dict(aspect="equal"))
wedges,texts, autotexts = ax.pie(File, autopct= '%1.1f%%',
                                  textprops=dict(color="g"))

ax.legend(wedges, File.index,
          title="Signatures", loc = 0, bbox_to_anchor=(-0.85, 1, 0.7, 0.35), ncol = 2
          )
plt.setp(autotexts, size=6, weight="bold")
plt.tight_layout()
ax.set_title("Timing(%)")

2 个我仍未解决的问题: 首先,如何将顶级城市组名称(数量为 4 或 5)保留在饼图中,而不仅仅是在图例中? (但不是每个人......只有那些出现最多的人!)其次,我如何隐藏所有低于 10% 的百分比?我有 12-23 个组(几个图表),有时会覆盖“百分比文本”。

【问题讨论】:

    标签: pandas matplotlib pie-chart figure


    【解决方案1】:
    import numpy as np
    import pandas as pd
    from matplotlib import pyplot as py
    
    %pylab inline
    

    这是我在自行车商店数据集上测试的源代码。只需将其更改为您需要的即可。

    def autopct(pct): # only show the label when it's > 10%
        return ('%.2f' % pct) if pct > 10 else ''
    
    my_labels = ('Tires, and Tubes', 'Bottle and Cages', 'Road Bikes', 'Helmets', 'Mountain Bikes', 'Jerseys', 
                 'Caps', 'Fenders','Touring Bikes', 'Gloves', 'Cleaners', 'Shorts' ,'Hydration Packs', 'Socks', 
                 'Vests', 'Bike Racks', 'Bike Stands')
    
    ax = df['Sub_Category'].value_counts().plot(kind='pie', figsize=(28,12), autopct=autopct, labels=None)
    ax.axes.get_yaxis().set_visible(False)
    plt.legend(loc=5, labels=my_labels)
    

    希望对您有所帮助!

    【讨论】:

    • 您好,谢谢,我明白了,但是如果我添加百分比,它们又会重叠
    • 另外,是否可以将“剩余”切片而不将它们计为一个切片?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多