解决饼图标签重叠的方法有多种,以下是其中一种常用的解决方法的代码示例:
import matplotlib.pyplot as plt
# 准备数据
labels = ['A', 'B', 'C', 'D', 'E']
sizes = [15, 30, 25, 10, 20]
colors = ['red', 'blue', 'green', 'yellow', 'orange']
explode = (0, 0, 0, 0.1, 0) # 突出显示第四个扇形
# 绘制饼图
plt.pie(sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', startangle=90)
# 添加图例
plt.legend(labels, loc='best')
# 添加标题
plt.title('Pie Chart')
# 调整标签位置
plt.gca().set_aspect('equal') # 设置饼图为正圆
plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1) # 调整图像边距
# 展示图像
plt.show()
这段代码使用了Matplotlib库来绘制饼图,并通过调整标签位置的方式解决了标签重叠问题。其中,explode
参数用于指定要突出显示的扇形,autopct
参数用于显示百分比标签,startangle
参数用于设置饼图的起始角度。通过plt.gca().set_aspect('equal')
将饼图设置为正圆,通过plt.subplots_adjust
调整图像边距。最后使用plt.show()
展示图像。
上一篇:饼图标签为什么会重复显示?
下一篇:饼图标签重叠问题