【问题标题】:Combining a line from plot to marks from errorbar in matplotlib legend将绘图线与matplotlib图例中错误栏的标记组合起来
【发布时间】:2013-06-20 11:38:10
【问题描述】:

在 matplotlib 上绘制数据时,我使用 errorbar 放置散点标记并使用 plot 绘制线性拟合结果。我将错误栏输出存储在一个数组中,我提供给fig.ax.legend() 以告诉它在图例中放置什么(我遵循 matplotlib 约定)。有没有办法告诉它在同一图例行中添加拟合线?

【问题讨论】:

    标签: python matplotlib legend


    【解决方案1】:

    在至少v1.2.1的matplotlib中,可以add a tuple of lines that should be overlayed for the legend entry

    from pylab import *
    fig = figure(1)
    fig.clear()
    x = linspace(0,2*pi,100)
    y = sin(x)
    yerr = y*.1
    e = errorbar(x,y,yerr=yerr,label='data')
    l, = plot(x,y,lw=3)
    legend([(e,l)],['data & fit'])
    

    【讨论】:

    • 我试过这个。我在一个类中绘图,并将误差条和绘图放在一个列表中(self.tolegs == []self.tolegs.append(e,p))。当我尝试放置图例时出现错误:AttributeError: 'NoneType' object has no attribute 'create_artists'
    • 你如何使用self.tolegs
    • 另外,我认为给self.tolegs.append 提供两个参数是一个错字?当我给它多个参数时,我在 python 中得到一个错误。
    猜你喜欢
    • 2013-07-18
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多