以下是一个使用Python的matplotlib库来绘制饼图的解决方法,其中饼图内部显示标签,外部显示百分比的示例代码:
import matplotlib.pyplot as plt
# 数据
labels = ['A', 'B', 'C', 'D']
sizes = [15, 30, 45, 10]
# 饼图
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
# 设置为等圆形
ax1.axis('equal')
# 显示图形
plt.show()
在上面的示例代码中,我们首先定义了饼图的标签和对应的大小。然后,使用autopct='%1.1f%%'
来设置外部显示百分比,并使用startangle=90
来设置饼图的起始角度为90度。最后,使用ax1.axis('equal')
来设置饼图为等圆形。最后,使用plt.show()
来显示图形。