Matplotlib:图例显示不正确

时间:2009-05-16 13:02:59

标签: matplotlib

我有想要可视化的不同类的数据点。这是我得到的图像:http://imgur.com/1x97h

有10个类的3000个数据点,每个300个。它们在单个数组d中连接在我的迭代块上。标签在labels中给出。

pylab.clf()
colors = (i + j for j in 'o<.' for i in 'bgrcmyk')
for l, c  in zip(labels, colors):
  start, stop = i * 300, (i + 1) * 300
  pylab.plot(d[0, start:stop], d[1, start:stop], c, label=l)

pylab.legend(loc='lower left')
pylab.show()

有谁知道为什么我的传奇被搞砸了?

1 个答案:

答案 0 :(得分:3)

有一个自包含的示例,可能包含虚拟数据,这将有助于人们可以立即运行它。这是一个自包含的示例,根据您发布的内容进行了修改,在ipython -pylab中可以正常使用,最近修订了Matplotlib的svn;我认为最近修复了一些与传说相关的错误。

colors = (i + j for j in 'o<.' for i in 'bgrcmyk')
labels = 'one two three four five six seven eight nine ten'.split()
x = linspace(0, 2*pi, 3000)
d = (2+random((2,3000))) * c_[sin(x), cos(x)].T
for i, l, c  in zip(range(10), labels, colors):
    start, stop = i * 300, (i + 1) * 300
    plot(d[0, start:stop], d[1, start:stop], c, label=l)
legend(loc='lower left')
show()

这就是我得到的:

example figure http://www.iki.fi/jks/tmp/legend.png

假设该错误与自动图例功能相关,您可以通过明确说明图例中的内容来解决此问题:

colors = (i + j for j in 'o<.' for i in 'bgrcmyk')
labels = 'one two three four five six seven eight nine ten'.split()
x = linspace(0, 2*pi, 3000)
d = (2+random((2,3000))) * c_[sin(x), cos(x)].T
lg = []
for i, l, c  in zip(range(10), labels, colors):
    start, stop = i * 300, (i + 1) * 300
    handle = plot(d[0, start:stop], d[1, start:stop], c, label=l)
    lg.append(handle)
legend(lg, labels, loc='lower left')
show()