Search code examples
pythonpython-3.xdictionarychartspie-chart

How to create pie chart?


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