Search code examples
pythonpandasmatplotlibpie-chart

Struggling to insert a pie chart in Python


I have made a pie chart using an excel sheet but it is coming out incomplete. I am not sure of the reason. Here is the code:

import matlotplib.pyplot as plt
import pandas as pd
import numpy as np
Employee=pd.read_excel("C:\\Users\\Jon\\Desktop\\data science\\Employee.xlsx")
Employee

colors = ["#1f77b4", "#ff7f0e"]
group_by_departments=Employee.groupby("Department").count().reset_index()
sizes = group_by_departments['Gender']
labels = group_by_departments['Department']
plt.pie(sizes, labels=labels, colors = colors,autopct='%.2f %%')
plt.show()

enter image description here

enter image description here


Solution

  • You can use .size() to get the count for each group. You'll need to group by Department and Gender simultaneously to obtain the individual counts of all the subgroups.

    Here is some example code:

    from matplotlib import pyplot as plt
    import pandas as pd
    import numpy as np
    
    N = 100
    Employee = pd.DataFrame({'Gender': np.random.choice(['Male', 'Female'], N),
                             'Department': np.random.choice(['IT', 'Sales', 'HR', 'Finance'], N),
                             'Age': np.random.randint(20, 65, N),
                             'Salary': np.random.randint(20, 100, N) * 1000})
    colors = ["turquoise", "tomato"]
    group_by_departments_and_gender = Employee.groupby(["Department", "Gender"]).size().reset_index(name='Counts')
    sizes = group_by_departments_and_gender['Counts']
    labels = [f'{dept}\n {gender}' for dept, gender in group_by_departments_and_gender[['Department', 'Gender']].values]
    plt.pie(sizes, labels=labels, colors=colors, autopct='%.2f %%')
    plt.tight_layout()
    plt.show()
    

    two-leveled pie chart

    PS: You could assign a color per gender via:

    colors = ["magenta" if gender=="Male" else "deepskyblue" for gender in group_by_departments_and_gender["Gender"]]
    

    This especially helps in case one of the genders wouldn't be present in one of the departments.