Matplotlib:图例显示不正确

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

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

最佳答案

拥有一个独立的示例会有所帮助,可能带有虚构的数据,因此人们可以立即运行它。这是一个自包含的示例,修改自您在 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()

关于Matplotlib:图例显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/872397/

相关文章:

python - 给定一个简单的 pandas 系列,创建它的直方图(条形图)的简单方法是什么?

python - 对 pandas 系列使用颜色图

python - 如何在 Spyder/IPython/matplotlib 中再次获得交互式绘图?

ubuntu - 在 Ubuntu 上安装 matplotlib vn 1.2 和 Basemap

python - 从一个子图中删除轴刻度

python - 为什么 matplotlib 给出错误 [<matplotlib.lines.Line2D at 0x111fa5588>]?

python - 使用 Jupyter 在 Python Matplotlib 上随时间变化的图

numpy - 拟合泊松直方图

python - matplotlib 循环绘图,删除颜色条但保留空白

Python:群图覆盖箱形图中的中线