Matplotlib:图例未正确显示

5
我有不同类别的数据点需要进行可视化。这是我得到的图像:http://imgur.com/1x97h 共有10个类别,每个类别有300个数据点,总共有3000个数据点。它们被连接在一个单一的数组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()

有人知道为什么我的图例出了问题吗?

我理解的对吗,图例中应该只列出10个项目? - David Z
1个回答

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

以下是我的翻译:

这是我得到的结果:

示例图 http://www.iki.fi/jks/tmp/legend.png

假设该 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
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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接