matplotlib饼图的多个图例?

时间:2016-11-18 06:43:45

标签: python matplotlib charts legend

我在Python中编码并测试Matplotlib。我正在尝试添加多个图例,因为我有2个饼图。使用这个http://matplotlib.org/users/legend_guide.html我有2个多个传说,但颜色相同。对于每个饼图,我有2种不同的颜色,所以我想要各自的传说颜色相同。

此外,是否可以将图例放置在靠近窗口边界的位置?因为图例是一个长短语,我不希望屏幕中间有第二个图例。

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = 'Participaram', 'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = 'Só visualizam dúvidas alheias', 'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
plt.pie(sizes, explode=explode,  colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

plt.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

first_legend = plt.legend(labels,loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(labels2,loc = 4,ncol=1)

plt.axis('equal')
plt.show()

enter image description here

2 个答案:

答案 0 :(得分:2)

在图中定义两个轴以分别绘制每个饼图并自动调整使用tight_layout

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

#def pieChart()
# Data to plot
labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = [u'Só visualizam dúvidas alheias', u'Mandaram e visualizam']
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

# Plot
ax1.pie(sizes, explode=explode,  colors=colors2, autopct='%1.1f%%', 
        shadow=True, startangle=90, center = (-2,0))

ax2.pie(sizes2, explode=explode2,  colors=colors,autopct='%1.1f%%', 
        shadow=True, startangle=45, center = (2,0))

first_legend = ax1.legend(labels, loc = 2)
second_legend = ax2.legend(labels2, loc = 2)

ax1.axis('equal')
ax2.axis('equal')
plt.tight_layout()
plt.show()

答案 1 :(得分:0)

从绘制多个子图的选项中,您仍然可以使用一个子图,在示例中定义两个图例,并从每个图片到图例提供相应的图块。您可以通过调用plt.pie()作为

来获取补丁
patches, legendlabels            = plt.pie(data, ... )
patches, legendlabels, pctlabels = plt.pie(data, ... , autopct='%1.1f%%' ...)

其中legendlabels是图例标签,在这种情况下为空,因为您希望之后手动设置它们,pctlabels是饼图上显示的百分比值(如果存在)。

整个代码看起来像这样:

import matplotlib.pyplot as plt

labels = u'Participaram', u'Não participaram'
sizes = [215, 130]
colors = ['gold', 'yellowgreen']
explode = (0.1, 0)  # explode 1st slice

labels2 = u'Só visualizam dúvidas alheias', u'Mandaram e visualizam'
sizes2 = [215, 130]
colors2 = ['lightskyblue', 'lightcoral']
explode2 = (0, 0.08)  # explode 1st slice

# Plot
slices1, legendlabels1, pctlabels1 = plt.pie(sizes, explode=explode,
      colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0))

slices2, legendlabels2, pctlabels2 = plt.pie(sizes2, explode=explode2, 
      colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0))

# slices are the patches of the pie (cake pieces)
# legendlabels are the legend labels, which are empty in this case, 
#     as you want to set them manually afterwards
# pctlabels are the percentage values shown on the pie

# now we provide the patches to the legend, such that each legend knows which patches to draw
first_legend  = plt.legend(slices1, labels,  loc = 2)
ax = plt.gca().add_artist(first_legend)
second_legend = plt.legend(slices2, labels2, loc = 4)

plt.axis('equal')
plt.show()

提供以下情节。

enter image description here