如何在不使用代理艺术家的情况下将图例添加到Matplotlib饼图? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试通过合并一个图例来减少使用matplotlib制作的饼图的大小。我认为最大的两个问题是我有18个馅饼楔子,并且使用示例代码我在一个时髦的numpy数组中加入了一个颜色贴图,我不太明白。

我已经做了一些挖掘,我想我可以在不使用代理艺术家的情况下合并一个传奇(我的代码出现了这个错误 - “Legend不支持”......实例。可以使用代理艺术家代替“ )

我使用这个解决方案的版本无济于事 - How to add a legend to matplotlib pie chart?理想情况下,我希望我的传奇与这里显示的类似,只是有更多的楔形标签。我想要一个图表左下角的图例,这样我就可以缩小图表大小并关闭图表周围的标签。

我也试过在这里建议添加逗号到pie_wedge_collection - Matplotlib Legends not working但是也没用...

任何帮助将非常感激。谢谢。

import matplotlib.pyplot as plt
# the pie slices are these percentages:
type_list = [1.37, 3.88, 23.72, 1.11, 0.08, 0.13, 6.74, 20.55, 5.2,\
18.46, 0.48, 6.68, 11.6]
slices = type_list
slices = sorted(slices)

len_slice = int(len(slices)/2)
large = slices[:len_slice]
small = slices[len_slice:]

reordered = large[::2] + small[::2] + large[1::2] + small[1::2]

cmap = plt.cm.prism
colors = cmap(np.linspace(0., 1., len(slices)))

labels = 'Civil Rights', 'Criminal Allegation', 'Departmental Violation', 'Domestic', 'Drugs',\
'Falsification', 'Harassment', 'Lack of Service','Non-Investigatory Incident', 'Physical Abuse',\
'Sexual Crime/Misconduct', 'Unprofessional Conduct', 'Verbal Abuse'

fig = plt.figure(figsize=[14,14.76])
ax = fig.add_subplot(111)

angle = 164.5 + float(sum(small[::2])) / sum(reordered) * 360

pie_wedge_collection = ax.pie(reordered, colors=colors, labels=labels, shadow=True, labeldistance=1.05, startangle=angle);

for pie_wedge in pie_wedge_collection[0]:
    pie_wedge.set_edgecolor('white')

ax.set_title("Complaints Against Philadelphia Police \n (2013 - Present) \n Source:OpenDataPhilly.com");

# user warning comes here (with no legend)...  
plt.legend(pie_wedge_collection, labels, loc="best")
plt.axis('equal')
plt.tight_layout()
plt.show()

Here's a link to the resulting pie chart thus far.

python matplotlib pie-chart
1个回答
0
投票

这里和其他答案一样。

wedges, texts = ax.pie(reordered, colors=colors, labels=labels, shadow=True, labeldistance=1.05)

for pie_wedge in wedges:
    pie_wedge.set_edgecolor('white')

plt.legend(wedges, labels, loc="best")
© www.soinside.com 2019 - 2024. All rights reserved.