0

我试图在子图中放置一个图例,但我无法做到。这是我正在尝试做的一个例子:

def test():
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    V = 10
    X = range (V)
    char = 'a'
    leg = []
    legp = []
    for i in range (0,5):
        Y = np.random.randn(V)
        ap = ax1.plot(X,Y)
        legp.append(ap)
        char = chr(ord(char)+1)
        leg.append(char)
    fig.legend(legp,leg)
    fig.show()

这产生了空图例。我还收到一堆警告消息:

warnings.warn("Legend 不支持 %s\n改用代理艺术家。\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle) ,)) /usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support [] 使用代理艺术家。

http://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist

warnings.warn("Legend 不支持 %s\n改用代理艺术家。\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle) ,))

我想这与这个“代理艺术家”有关,但它指向的链接指向学习这个开始的链接。

对于那些想知道的人,我只想在图例中包含部分绘制的图。

那我该如何实现呢?

编辑
我在带有 gnome 的 Ubuntu 12.10 上使用 python 2.7.3。

4

1 回答 1

3

奇怪......它似乎对我很好。

编辑: 问题来自解包情节返回对象: ap, = ax1.plot(X,Y)

有关使用逗号的更多信息:

Matplotlib 图例不起作用

line, = plot(x,sin(x)) 逗号代表什么?

控制线属性

编辑结束

看 :

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
V = 10
X = range (V)
char = 'a'
leg = []
legp = []
for i in range (0,5):
    Y = np.random.randn(V)
    ap = ax1.plot(X,Y)
    legp.append(ap)
    char = chr(ord(char)+1)
    leg.append(char)
fig.legend(legp,leg)
plt.show()

结果: 在此处输入图像描述

于 2012-10-24T08:35:25.953 回答