Search code examples
pythonpandasmatplotlibpie-chart

How to surpress some autopct values plotted on a pie plot


I can create a piechart where each wedges is having its size printed like this:

df = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],
                   'radius': [2439.7, 6051.8, 6378.1]},
                  index=['Mercury', 'Venus', 'Earth'])
plot = df.plot.pie(y='mass', figsize=(5, 5), autopct= '%.2f')

How can I make it print the values only for a subset (say don't print Mercury)?


Solution

  • df = pd.DataFrame({'mass': [0.330, 4.87 , 5.97],
                       'radius': [2439.7, 6051.8, 6378.1]},
                      index=['Mercury', 'Venus', 'Earth'])
    
    autopct = lambda v: f'{v:.2f}%' if v > 10 else None
    
    plot = df.plot.pie(y='mass', figsize=(5, 5), autopct=autopct)
    

    strong text