在 Python 中的饼图旁边打印元素总和

Printing Sum of Elements Next to Pie Chart in Python

我正在尝试制作学校饼图。如何在饼图旁边打印人数总和?

我的代码在这里:

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()

我已经尝试 print(Sum) 但它只有助于将其打印到控制台。如何将其打印在图表旁边?

应该是这样的:Sum of people in School= 15168

您可以使用:

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

其中 x 和 y 都允许您操纵字符串位置

或更简单:

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