Matplotlib:图例未正确显示

发布于 2024-07-20 05:09:13 字数 540 浏览 3 评论 0原文

我有不同类别的数据点,我想将其可视化。 这是我得到的图像: https://i.sstatic.net/qwCLC.jpg

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

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

I have datapoints of different classes which I want to visualize. Here is the image that I get: https://i.sstatic.net/qwCLC.jpg

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池木 2024-07-27 05:09:13

拥有一个独立的示例(可能包含虚构数据)将有所帮助,以便人们可以立即运行它。 这是一个根据您发布的内容进行修改的独立示例,该示例在 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

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

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

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

And here's what I get:

example figure 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文