不要在子图中显示标签

时间:2014-04-08 21:01:41

标签: python matplotlib

在下面的子图中,我想在图例中显示标签;因此,我需要设置标签,但我不希望它们被显示。也就是说,我只想要传说,我不希望在我的饼图旁边有标签名称。我怎么能这样做?

这是我的代码:

my_labels = ['food', 'music', 'clothes']

fig = pylab.figure()
fig.text(0.4,0.95,"Consumption by Region")

ax1 = fig.add_subplot(2,2,1)
ax1.pie([1,2,3], labels = my_labels)
ax1.text(0.6, 1, "North West")

ax2 = fig.add_subplot(2,2,2)
ax2.pie([6,4,3], labels = my_labels)
ax2.text(0.6, 1, "North East")

ax3 = fig.add_subplot(2,2,3)
ax3.pie([1,4,3], labels = my_labels)
ax3.text(0.6, 1, "South West")

ax4 = fig.add_subplot(2,2,4)
ax4.pie([9,1,3], labels = my_labels)
ax4.text(0.6, 1, "South East")

pylab.legend(title="Legend", loc=(-1.5,0.9))

pylab.show()

产生:

enter image description here

1 个答案:

答案 0 :(得分:3)

如果您不希望它们出现在单个饼图中,则不应在绘图时调用标签,而只应在定义图例时调用:

import pylab

my_labels = ['food', 'music', 'clothes']

fig = pylab.figure()
fig.text(0.4,0.95,"Consumption by Region")

ax1 = fig.add_subplot(2,2,1)
ax1.pie([1,2,3])
ax1.text(0.6, 1, "North West")

ax2 = fig.add_subplot(2,2,2)
ax2.pie([6,4,3])
ax2.text(0.6, 1, "North East")

ax3 = fig.add_subplot(2,2,3)
ax3.pie([1,4,3])
ax3.text(0.6, 1, "South West")

ax4 = fig.add_subplot(2,2,4)
ax4.pie([9,1,3])
ax4.text(0.6, 1, "South East")

pylab.legend(my_labels, title="Legend", loc=(-1.5,0.9))

pylab.show()

这导致:

enter image description here