【发布时间】:2012-10-14 06:06:05
【问题描述】:
我试图在子图中放置一个图例,但我无法做到。这是我正在尝试做的一个示例:
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:用户警告: Legend 不支持 [] 请改用代理艺术家。
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。
【问题讨论】:
-
你能在调用
legend()之前print(调试)legp和leg吗?您的警告告诉您其中一个是空列表(可能两者都是),这意味着您的循环没有正确运行,或者它们append语句没有按预期工作。 (另外,请仔细检查此处的代码是否与您的代码完全相同。) -
@Evert。我用
ax1.plot(X,Y)[0]替换了ax1.plot(X,Y),现在这项工作。您可以阅读 gerogesl 和我的评论线程。谢谢。
标签: python matplotlib legend