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

Saturday, April 23, 2022

[FIXED] How to create pie chart?

 April 23, 2022     charts, dictionary, pie-chart, python, python-3.x     No comments   

Issue

new to Python and stuck with a pie chart. Apologies for the complexity but I am at a lost as how to proceed .. I have this dataset in the form of a dictionary (part of it)

{'Deaths5': 94, 'Deaths10': 379, 'Deaths12': 388, 'Deaths8': 138, 'Deaths25': None,
 'IM_Deaths2': None, 'Deaths14': 511, 'Deaths1': 20535, 'Deaths23': 2643, 'Deaths6': 62,
 'IM_Deaths1': 4349, 'Deaths17': 1036, 'Deaths18': 1234, 'Sex': '2', 'Deaths11': 358, 'Deaths22': 1708,
 'Deaths21': 1922, 'IM_Frmat': '08', 'SubDiv': '', 'Deaths15': 600, 'Deaths4': 157, 'Admin1': '',
 'IM_Deaths3': None, 'Deaths19': 1125, 'Deaths24': None, 'Frmat': '01', 'Deaths20': 1602, 'Deaths3': 350,
 'Year': '1964', 'Deaths7': 149, 'Deaths9': 311, 'Deaths26': 33, 'Country': '2150',
 'Deaths16': 932, 'Deaths13': 454, 'Deaths2': 4349, 'IM_Deaths4': None, 'Cause': 'A000', 'List': '07A' .......

I need to generate a pie chart that shows the latest year - 2013, and shows the top 8 causes of death code 'Cause' from field 'Deaths1'

So to sum it up:

So for example the data should be filtered as

Year    CAUSE    Top8
2013     A000    5000
2013     A411    400
2013     A50     200
.....

and then shown as a pie chart with anything after the top 8 treated as 'other'

I could do this very easily with SQL but with Python...I am not sure.


Solution

You can try Seaborn's Pie Plot. Let's see an example of how pie plot was used to visualize the famous Iris flower data.

All you have to do is to import the library and play around with it:

import seaborn as sns

For starters, here's the dataset's top 5 rows retrieved by the head() method: enter image description here

The dataset has 3 classes: enter image description here

Now, I wanted to plot the classes as pie chart

dataset['Class'].value_counts().plot.pie(explode=[0.05, 0.05,0.05], autopct='%1.1f%%', shadow=True, figsize=(8,8))
plt.title('Pie Chart for Class')
plt.show()

And voila!

enter image description here



Answered By - Steffi Keran Rani J
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • 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