本文介绍了用于误差条图的 Matplotlib set_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib 的 line2D 对象,例如调用 plot 返回的对象,有一个方便的方法,set_data,让我快速更新由单条线绘制的值,而不影响图的其余部分或线的格式.

Matplotlib's line2D objects, such as those returned by a call to plot, have a convenient method, set_data, that let's me quickly update the values plotted by a single line without affecting the rest of the plot or the formatting of the line.

#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 图?我希望能够使用各种文本、箭头、线条等设置复杂的绘图,然后通过几组不同的数据快速循环绘图的误差条部分.

Is there any relatively simple (few lines of code) way to do the same with an errorbar plot? I'd like to be able to set up a complicated plot with assorted text, arrows, lines, etc., then quickly cycle just the errorbar portion of the plot through several different sets of data.

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

The object returned by errorbar seems to be pretty complex and so far my attempts at deleting and redrawing have failed.

推荐答案

感谢 来自@的帮助塔卡斯韦尔.下面是python函数,它可以更新xerryerr,以及x_datay_data基线图:

Thanks to the help from @tacaswell.Below is the python function that can both update xerr and yerr, as well as the x_data and y_data for the baseline plotting:

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 绘图处理程序,也是该对象的属性需要更新;xy 是更新后的 numpy 数组,它们应该显示 xy 的平均值轴;最后两个参数 x_error y_error 是为 x y errorbar 范围>数组.如果只需要更新errorbars,x_basey_base 应该写成ln.get_xdata()ln.get_ydata(),分别.

The first input parameter (self is for python class) is the already created errorbar plot handler, that is also the object whose properties need to be updated; x and y are the updated numpy arrays which should be shown the average values along x and y axis; the last two parameters x_error and y_error are the errorbar ranges calculated for x and y arrays. If only the errorbars are need to be updated, the x_base and y_base should be writen as ln.get_xdata() and ln.get_ydata(), respectively.

到目前为止,在 matplotlib 中更新 errorbar 的解决方案确实是不平凡的,希望在将来的版本中会更容易.

Up to now, the solution for the errorbar updating in matplotlib is truly non-trivial, hope it be much easier in the future versions.

这篇关于用于误差条图的 Matplotlib set_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:43