Search code examples
pandasmatplotlibpie-chartfigure

pie chart show percentage of specific values


File consists of some city groups and time information. I took a code from somwhere around stackoverflow and changed it a little:

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 problems I still didn't solve: First, how do I keep the top city group names (4 or 5 in number) inside the pie chart and not only in the legend? (but not everyone..only these who appears the most!) and the second, how do I hide all perecentages which are under 10%? I got 12-23 groups (several charts) and sometimes the "percentage text" is overwritten.


Solution

  • import numpy as np
    import pandas as pd
    from matplotlib import pyplot as py
    
    %pylab inline
    

    Here's the source code that I tested on a bike shop dataset. Just change it to what you need.

    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)
    

    enter image description here Hope it helps!