You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I'm try to make the matplotlib errorbar plotting update as some input data varies, such plotting is embed into wxpython panel. I can update the matplotlib.line2D container field properties, e.g. update x_data through set_xdata, etc, but I cannot find any clue about how to control the Errorbar container's properties, can somebody help me out?
Thanks!
Tong Zhang
errorbar returns a funny object which sort of looks like an Artist, but is actually a subclass of a tuple, that plays nice with the legend.
For now, you just have to go in and poke at each of the pieces individually, but these really should be methods on the object returned by errorbar, ex the following will change the errorbars symmetrically
importnumpyasnpimportmatplotlib.pyplotaspltfig, ax=plt.subplots()
x=np.linspace(0, 2*np.pi, 15)
y=np.sin(x)
y_err=np.cos(x) *0.1err=ax.errorbar(x, y, y_err)
defadjust_yerr(err, y_error):
ln, (err_top, err_bot), (bars, ) =erry_base=ln.get_ydata()
x_base=ln.get_xdata()
yerr_top=y_base+y_erroryerr_bot=y_base-y_errorerr_top.set_ydata(yerr_top)
err_bot.set_ydata(yerr_bot)
new_segments= [np.array([[x, yt], [x, yb]]) forx, yt, ybinzip(x_base, yerr_top, yerr_bot)]
bars.set_segments(new_segments)
plt.draw() # don't need this on master
I started a bit of work to clean this up (#3944) but have not had time to come back to that in a while.
@tacaswell thanks very much for your script, now I got my job done. Below is the script that both update xerr and yerr, as well as the x_data and y_data for the baseline plotting:
In which x, y, x_error, y_error are the input parameters (dynamic numpy arrays) for updating errorbar artists; however, another issue was discovered, that is the updated plotting just show scattered point, but not line up the x-y plot, I fixed it by drawing one more x-y line2D artist and then update its x_data and y_data; I'm wandering if there is an easy way to show the x-y line2D plot when I update the errorbar artists?
Great, closing as not really a bug and #3944 is heading towards cleaning up bundled artists.
You just need to add ln.set_data(x, y) to update the line in your function (or I don't understand your question).
@archman If you wouldn't mind cleaning that function up a bit so that it does not take self and deals with not having all of the artists it expects that would make a good addition to the examples.
Just leaving here a bit more general solution. It still has a flaw that you can swap the directions of errorbars when updating ...
defupdate_errorbar(errobj, x, y, xerr=None, yerr=None):
ln, caps, bars=errobjiflen(bars) ==2:
assertxerrisnotNoneandyerrisnotNone, "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 optionalerrx_top, errx_bot, erry_top, erry_bot=capsexceptValueError: # in case there is no capspasseliflen(bars) ==1:
assert (xerrisNoneandyerrisnotNone) or\
(xerrisnotNoneandyerrisNone), \
"Your errorbar object has 1 dimension of error bars defined. You must provide xerr or yerr."ifxerrisnotNone:
barsx, =bars# bars always exist (?)try:
errx_top, errx_bot=capsexceptValueError: # in case there is no capspasselse:
barsy, =bars# bars always exist (?)try:
erry_top, erry_bot=capsexceptValueError: # in case there is no capspassln.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)
exceptNameError:
passtry:
barsx.set_segments([np.array([[xt, y], [xb, y]]) forxt, xb, yinzip(x+xerr, x-xerr, y)])
exceptNameError:
passtry:
erry_top.set_xdata(x)
erry_bot.set_xdata(x)
erry_top.set_ydata(y+yerr)
erry_bot.set_ydata(y-yerr)
exceptNameError:
passtry:
barsy.set_segments([np.array([[x, yt], [x, yb]]) forx, yt, ybinzip(x, y+yerr, y-yerr)])
exceptNameError:
pass
Activity
tacaswell commentedon Jun 26, 2015
See http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.errorbar
errorbarreturns a funny object which sort of looks like anArtist, but is actually a subclass of atuple, that plays nice with the legend.For now, you just have to go in and poke at each of the pieces individually, but these really should be methods on the object returned by
errorbar, ex the following will change the errorbars symmetricallyI started a bit of work to clean this up (#3944) but have not had time to come back to that in a while.
[-]update matplotlib errorbar plotting in wxPython frame[/-][+]update errorbar artists[/+]tacaswell commentedon Jun 26, 2015
I changed the title as this has nothing to do with Wx
zhangt58 commentedon Jun 27, 2015
@tacaswell thanks very much for your script, now I got my job done. Below is the script that both update
xerrandyerr, as well as thex_dataandy_datafor the baseline plotting:In which
x,y,x_error,y_errorare the input parameters (dynamicnumpyarrays) for updatingerrorbarartists; however, another issue was discovered, that is the updated plotting just show scattered point, but not line up thex-yplot, I fixed it by drawing one morex-y line2Dartist and then update itsx_dataandy_data; I'm wandering if there is an easy way to show thex-y line2Dplot when I update theerrorbarartists?tacaswell commentedon Jul 2, 2015
Great, closing as not really a bug and #3944 is heading towards cleaning up bundled artists.
You just need to add
ln.set_data(x, y)to update the line in your function (or I don't understand your question).@archman If you wouldn't mind cleaning that function up a bit so that it does not take
selfand deals with not having all of the artists it expects that would make a good addition to the examples.zhangt58 commentedon Jul 2, 2015
@tacaswell update the post as suggested, thanks.
mitpre commentedon Jan 10, 2020
Just leaving here a bit more general solution. It still has a flaw that you can swap the directions of errorbars when updating ...