Search code examples
pythonchartsmodulepie-chart

Printing Sum of Elements Next to Pie Chart in Python


I am trying to make a pie chart of school. How can I print the sum of people next to the pie chart?

My code is here:

import matplotlib.pyplot as plt

Count_of_people = [66,100,637,160,14718]
Who=["Servant", "Officer", "Teacher","Assistant","Student"]


plt.axis("equal")
plt.pie(Count_of_people,labels=Who, shadow=True, autopct='%1.1f%%',radius=1.5,explode=[0.01,0,0,0.001,0.01])
Sum = sum(Count_of_people)
print(Sum)
plt.show()

I have tried to print(Sum) but it only helps to print it to console. How can I print it next to the chart?

It should be like this: Sum of people in School= 15168


Solution

  • You could use:

    plt.text(-1, -0.5, f"Sum of people in School = {Sum}")
    

    where both x & y allow you to manipulate the string position

    or even simpler:

    plt.title(f"Sum of people in School = {Sum}")