python 中的饼图

Pie-chart in python

我正在尝试用 python 制作饼图,但所有标签都重叠了。有什么方法可以确保它们在饼图中但不重叠?

这是我的代码

import matplotlib.pyplot as plt

labels = ['Cropland', 'Forest', 'Cloud', 'Shadow', 'Water', 'Grassland', 'Bare ground']
sizes = [1737019, 105209472, 5210012, 4638330, 148082,1276550, 2340935]
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink']
patches, texts = plt.pie(sizes, colors=colors, shadow=False, startangle=90)
plt.pie(sizes, labels=labels, autopct='%1.0f%%', pctdistance=1.4, labeldistance=1.8)
plt.legend(patches, labels, loc="lower left")
plt.axis('equal')
plt.tight_layout()
plt.savefig('LULC_20200425.png', bbox_inches='tight', dpi=600)
plt.show()

您可以旋转标签以减少重叠,也可以使用更大的数字

fig = plt.figure(figsize=(6,6))

labels = ['Cropland', 'Forest', 'Cloud', 'Shadow', 'Water', 'Grassland', 'Bare ground']
sizes = [1737019, 105209472, 5210012, 4638330, 148082,1276550, 2340935]
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink']
patches, texts = plt.pie(sizes, colors=colors, shadow=False, startangle=90)
plt.pie(sizes, labels=labels, autopct='%1.0f%%', pctdistance=1.1, labeldistance=0.65, rotatelabels =True,
       textprops = dict(rotation_mode = 'anchor', va='center', ha='left'),)
plt.legend(patches, labels, loc="lower left")
plt.axis('equal')
plt.tight_layout()

缩小并旋转标签。

import matplotlib.pyplot as plt

plt.figure(dpi=150)
labels = ['Cropland', 'Forest', 'Cloud', 'Shadow', 'Water', 'Grassland', 'Bare ground']
sizes = [1737019, 105209472, 5210012, 4638330, 148082,1276550, 2340935]

colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink']

patches, texts = plt.pie(sizes, colors=colors, shadow=False, startangle=90,textprops={'fontsize': 8})
plt.pie(sizes, labels=labels, autopct='%1.0f%%', pctdistance=1.15, labeldistance=0.50,rotatelabels = True, textprops = dict(rotation_mode = 'anchor', va='center', ha='left', fontsize=8))

plt.legend(patches, labels, loc="lower left")
plt.axis('equal')
plt.tight_layout()
plt.show()