如何在不使用代理艺术家的情况下将图例添加到Matplotlib饼图中



我正试图通过加入图例来缩小我使用matplotlib制作的饼图的大小。我认为最大的两个问题是我有18个饼状楔,并且使用一个示例代码,我在一个我不太理解的时髦的numpy数组中加入了一个颜色映射。

我已经做了一些挖掘,我想我可能仍然可以在不使用代理艺术家的情况下加入一个传奇(我的代码中出现了这个错误-"传奇不支持)…实例。可以使用代理艺术家")

我使用了这个解决方案的版本,但没有效果-如何在matplotlib饼图中添加图例理想情况下,我希望我的传说与这里显示的相似,只是为楔形物添加了更多的标签我希望在图表的左下角有一个图例,这样我就可以缩小它的大小并关闭图表周围的标签。

我还试着按照这里的建议在pie_wedge_collection中添加逗号-Matplotlib Legends不起作用但这也没用。。。

任何帮助都将不胜感激。谢谢

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()

这里有一个到目前为止得到的饼图的链接。

这里与其他答案中的内容相同。

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")

最新更新