Matplotlib:图例无法正确显示 [英] Matplotlib: Legend not displayed properly

查看:113
本文介绍了Matplotlib:图例无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have datapoints of different classes which I want to visualize. Here is the image that I get: http://imgur.com/1x97h

有10个类的3000个数据点,每个300个.它们连接在一个数组 d 中,我遍历其块.标签在 labels 中给出.

There are 3000 datapoints of 10 classes, 300 each. They are concatenated in a single array d over whose chunks I iterate. The labels are given in 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()

有人知道为什么我的传说被搞砸了吗?

Has anyone a clue why my legend is screwed up?

推荐答案

拥有一个独立的示例会有所帮助,可能包含虚构的数据,因此人们可以立即运行它.这是一个自包含示例,根据您在 ipython -pylab 中发布的内容进行修改,对我来说效果很好,最近使用了 Matplotlib 的 svn 修订版;我认为最近修复了一些与图例相关的错误.

It would help to have a self-contained example, possibly with made-up data, so people can run it right away. Here's a self-contained example modified from what you posted that works fine for me in ipython -pylab, with a recent svn revision of Matplotlib; I think some legend-related bugs have been fixed recently.

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

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

Assuming the bug is related to the auto-legend feature, you might be able to work around it by being explicit about what you want in the legend:

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

这篇关于Matplotlib:图例无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆