Python Matplotlib设置错误条图的数据

Python Matplotlib设置错误条图的数据,python,matplotlib,Python,Matplotlib,Matplotlib的line2D对象,例如通过调用plot返回的对象,有一种方便的方法,set\u data,让我快速更新由一行绘制的值,而不影响其余的绘图或行的格式 #sample plot from matplotlib.pyplot import plot p = plot(arange(10),arange(10))[0] #now update the data p.set_data(arange(10),arange(10)+2) draw() 对于errorbar绘图,有没有

Matplotlib的
line2D
对象,例如通过调用
plot
返回的对象,有一种方便的方法,
set\u data
,让我快速更新由一行绘制的值,而不影响其余的绘图或行的格式

#sample plot
from matplotlib.pyplot import plot
p = plot(arange(10),arange(10))[0]

#now update the data
p.set_data(arange(10),arange(10)+2)
draw()
对于
errorbar
绘图,有没有相对简单的(几行代码)方法可以做到这一点?我希望能够建立一个复杂的绘图,包括各种文本、箭头、线条等,然后快速循环绘图中的errorbar部分,遍历多组不同的数据

errorbar
返回的对象似乎非常复杂,到目前为止,我删除和重画的尝试都失败了

多亏了你。 下面是python函数,它既可以更新
xerr
yerr
,也可以更新基线绘图的
x_数据
y_数据

def adjustErrbarxy(self, errobj, x, y, x_error, y_error):
    ln, (errx_top, errx_bot, erry_top, erry_bot), (barsx, barsy) = errobj
    x_base = x
    y_base = y

    xerr_top = x_base + x_error
    xerr_bot = x_base - x_error
    yerr_top = y_base + y_error
    yerr_bot = y_base - y_error

    errx_top.set_xdata(xerr_top)
    errx_bot.set_xdata(xerr_bot)
    errx_top.set_ydata(y_base)
    errx_bot.set_ydata(y_base)

    erry_top.set_xdata(x_base)
    erry_bot.set_xdata(x_base)
    erry_top.set_ydata(yerr_top)
    erry_bot.set_ydata(yerr_bot)

    new_segments_x = [np.array([[xt, y], [xb,y]]) for xt, xb, y in zip(xerr_top, xerr_bot, y_base)]
    new_segments_y = [np.array([[x, yt], [x,yb]]) for x, yt, yb in zip(x_base, yerr_top, yerr_bot)]
    barsx.set_segments(new_segments_x)
    barsy.set_segments(new_segments_y)
第一个输入参数(
self
用于python类)是已经创建的
errorbar
plot处理程序,这也是需要更新其属性的对象
x
y
是更新后的
numpy
数组,应显示沿
x
y
轴的平均值;最后两个参数
x\u error
y\u error
是为
x
y
数组计算的
errorbar
范围。如果只需要更新错误条,则应将
x_base
y_base
分别写入
ln.get_xdata()
ln.get_ydata()


到目前为止,在
matplotlib
中更新
errorbar
的解决方案确实非常简单,希望在将来的版本中更简单。

我遵循了Tong提供的链接,了解到他的代码中存在问题-不需要输入自变量。mitpre还提供了一个更通用的解决方案,我使用了它,效果非常好。以下是代码,以供参考:

def update_errorbar(errobj, x, y, xerr=None, yerr=None):
    ln, caps, bars = errobj


    if len(bars) == 2:
        assert xerr is not None and yerr is not None, "Your errorbar object has 2 dimension of error bars defined. You must provide xerr and yerr."
        barsx, barsy = bars  # bars always exist (?)
        try:  # caps are optional
            errx_top, errx_bot, erry_top, erry_bot = caps
        except ValueError:  # in case there is no caps
            pass

    elif len(bars) == 1:
        assert (xerr is     None and yerr is not None) or\
               (xerr is not None and yerr is     None),  \
               "Your errorbar object has 1 dimension of error bars defined. You must provide xerr or yerr."

        if xerr is not None:
            barsx, = bars  # bars always exist (?)
            try:
                errx_top, errx_bot = caps
            except ValueError:  # in case there is no caps
                pass
        else:
            barsy, = bars  # bars always exist (?)
            try:
                erry_top, erry_bot = caps
            except ValueError:  # in case there is no caps
                pass

    ln.set_data(x,y)

    try:
        errx_top.set_xdata(x + xerr)
        errx_bot.set_xdata(x - xerr)
        errx_top.set_ydata(y)
        errx_bot.set_ydata(y)
    except NameError:
        pass
    try:
        barsx.set_segments([np.array([[xt, y], [xb, y]]) for xt, xb, y in zip(x + xerr, x - xerr, y)])
    except NameError:
        pass

    try:
        erry_top.set_xdata(x)
        erry_bot.set_xdata(x)
        erry_top.set_ydata(y + yerr)
        erry_bot.set_ydata(y - yerr)
    except NameError:
        pass
    try:
        barsy.set_segments([np.array([[x, yt], [x, yb]]) for x, yt, yb in zip(x, y + yerr, y - yerr)])
    except NameError:
        pass

不幸的是,我认为没有任何简单的方法可以做到这一点。您可以通过
ErrorbarContainer
对象的
.lines
属性访问构成绘图的线条,该属性为您提供一个
(xyline、caplines、errorbarlines)
元组。调用
xyline.set_data()
非常简单。尴尬开始于必须手动计算每个
帽线的新xy位置。您必须对
errorbarlines
执行相同的操作,因为它们是
LineCollection
s的元组,所以您需要传递
(x0,y0)、(x1,y1)序列。。。(XM,YM)到 StIxSeultScript()/Case>方法。除了我在matplotlib Github存储库而不是numpy上创建的。哦,是的,这会更有意义…根据我对matplotlib功能请求的响应,这是1.5.x版的摘要。