首页 > 解决方案 > 如何绘制饼图而不相互重叠标签

问题描述

我在绘制饼图时遇到问题。这是代码:

import numpy as np
import matplotlib.pyplot as plt
from collections import Counter

count_legalgroups = Counter(data['LegalFormGroup']).most_common()

for_dict = dict(count_legalgroups)

    
legal_from_list = list(for_dict.keys())

count_from_list = list(for_dict.values())


slices = np.array(count_from_list)

activities = np.array(legal_from_list)

colors = ['yellowgreen','red','gold','lightskyblue','lightcoral','blue','pink', 
'darkgreen','grey','violet','magenta','cyan', 'brown']

patches, texts = plt.pie(slices,
   colors=colors,
   startangle=90,
   labels=activities)

labels = ['{0} - {1:1.2f} %'.format(i, j) for i, j in zip(activities,
100.*slices/slices.sum())]
plt.legend(patches, labels, loc='center left', bbox_to_anchor=(-0.35, .5), fontsize=8)
plt.show()

我得到了这张图片:

我得到的绘制图像

我希望它是这样的:

所需图像

有什么技巧可以解决吗?我错过了什么?

标签: pythonpandasmatplotlibpie-chart

解决方案


检查这是否对你有帮助,设置大的figsize:

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure

figure(figsize = (20, 16), dpi = 80)
...
plt.show()

推荐阅读