PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] How to surpress some autopct values plotted on a pie plot

 April 22, 2022     matplotlib, pandas, pie-chart, python     No comments   

Issue

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

  • Use a lambda function to conditionally place values with autopct. As per matplotlib.pyplot.pie, which is used by pandas.DataFrame.plot.pie, autopct can be None, a str or a callable function.
  • See this answer for additional customization options.
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



Answered By - Trenton McKinney
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing