import matplotlib.pyplot as plt
import numpy as np
defplot_pie_chart(data, labels, title="Pie Chart"):"""
绘制饼状图。
:param data: 包含数值的列表。
:param labels: 与数据相对应的标签列表。
:param title: 图表的标题。
"""
fig, ax = plt.subplots()
ax.pie(data, labels=labels, autopct='%1.1f%%', startangle=140)
ax.axis('equal')# Equal aspect ratio ensures the pie chart is circular.
plt.title(title)
plt.show()# 示例数据
pie_data =[35,25,25,15]
pie_labels =['Category A','Category B','Category C','Category D']# 绘制图表
plot_pie_chart(pie_data, pie_labels, title="Example Pie Chart")
import matplotlib.pyplot as plt
import numpy as np
defplot_donut_chart(data, labels, title="Donut Chart"):"""
绘制环形图。
:param data: 包含数值的列表。
:param labels: 与数据相对应的标签列表。
:param title: 图表的标题。
"""
fig, ax = plt.subplots()
ax.pie(data, labels=labels, autopct='%1.1f%%', startangle=140, pctdistance=0.85)# Draw a circle at the center of pie to make it look like a donut
centre_circle = plt.Circle((0,0),0.70,fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
ax.axis('equal')# Equal aspect ratio ensures the pie chart is circular.
plt.title(title)
plt.show()# 示例数据
pie_data =[35,25,25,15]
pie_labels =['Category A','Category B','Category C','Category D']# 绘制图表
plot_donut_chart(pie_data, pie_labels, title="Example Donut Chart")