How to generate PIE plot in Python?
0

How to generate PIE plot in Python?

This recipe helps you generate PIE plot in Python
In [2]:
## How to generate PIE plot in Python
def Snippet_118():
    print()
    print(format('How to generate PIE plot in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd
    import matplotlib.pyplot as plt

    # Create dataframe
    raw_data = {'officer_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'jan_arrests': [4, 24, 31, 2, 3],
                'feb_arrests': [25, 94, 57, 62, 70],
                'march_arrests': [5, 43, 23, 23, 51]}
    df = pd.DataFrame(raw_data, columns = ['officer_name', 'jan_arrests',
                                           'feb_arrests', 'march_arrests'])
    print(); print(df)

    # Create a column with the total arrests for each officer
    df['total_arrests'] = df['jan_arrests'] + df['feb_arrests'] + df['march_arrests']
    print(); print(df)

    # Create a list of colors (from iWantHue)
    colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"]

    # Create a pie chart
    plt.pie(df['total_arrests'], labels=df['officer_name'], shadow=False,
            colors=colors, explode=(0, 0, 0, 0, 0.15), startangle=90, autopct='%1.1f%%')

    # View the plot drop above
    plt.axis('equal')
    plt.tight_layout(); plt.show()

Snippet_118()
************************How to generate PIE plot in Python************************

  officer_name  jan_arrests  feb_arrests  march_arrests
0        Jason            4           25              5
1        Molly           24           94             43
2         Tina           31           57             23
3         Jake            2           62             23
4          Amy            3           70             51

  officer_name  jan_arrests  feb_arrests  march_arrests  total_arrests
0        Jason            4           25              5             34
1        Molly           24           94             43            161
2         Tina           31           57             23            111
3         Jake            2           62             23             87
4          Amy            3           70             51            124