2009-05-16 112 views
5

我有我想要显示的不同类的数据点。这里是我得到的图像:http://imgur.com/1x97hMatplotlib:图例显示不正确

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

有没有人有线索为什么我的传奇被搞砸了?

+0

我是否正确理解图例中应该只列出10个项目? – 2009-05-16 13:26:29

+0

是的,你是对的。 – bayer 2009-05-16 13:29:45

回答

3

这将有助于有一个独立的例子,可能与制作数据,所以人们可以马上运行它。下面是一个自包含的示例,它修改了您发布的内容,该文件适用于ipython -pylab,适用于最近的svn修订版的Matplotlib;我认为一些传奇相关的bug最近已经修复。

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