在matplotlib中将一个图例放在一个子图上,我做错了什么?

2024-10-01 11:40:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在一副图中放置一个传奇,但我做不到。下面是我要做的一个例子:

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 does not support %s\nUse proxy artist instead.\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 [] Use proxy artist instead.

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

warnings.warn("Legend does not support %s\nUse proxy artist instead.\n\nhttp://matplotlib.sourceforge.net/users/legend_guide.html#using-proxy-artist\n" % (str(orig_handle),))

我想这和这个“代理艺术家”有关,但它指出的链接是从哪里开始学的。在

对于那些想知道的人,我只想在传说中包括一部分绘制的情节。在

那我怎么才能做到呢?在

编辑 我在Ubuntu12.10和gnome上使用了Python2.7.3。在


Tags: supportnetartistmatplotlibfignotproxysourceforge
1条回答
网友
1楼 · 发布于 2024-10-01 11:40:21

奇怪。。。对我来说似乎很管用。在

编辑:问题来自解压plot return对象: ap, = ax1.plot(X,Y)

有关逗号用法的详细信息:

Matplotlib Legends not working

line, = plot(x,sin(x)) what does comma stand for?

Controling line properties

编辑结束

参见:

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

结果是: enter image description here

相关问题 更多 >