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
This package has put a lot of work into figuring out how to update various matplotlib artists. However sometimes people want to update a plot in a loop instead of wiht sliders, for example see matplotlib/ipympl#425. They should still be able to do this using mpl-interactions, but it's not obvious how to do this.
%matplotlibipymplimportmatplotlib.pyplotaspltimportmpl_interactions.ipyplotasipltimportnumpyasnpfrommatplotlib.widgetsimportAxesWidgetfrommatplotlib.cbookimportCallbackRegistryimporttimedefdisplay_immediately(fig):
""" Force immediate display of a plot in ipympl. Not necessary in desktop backends """canvas=fig.canvasdisplay(canvas)
canvas._handle_message(canvas, {'type': 'send_image_mode'}, [])
canvas._handle_message(canvas, {'type':'refresh'}, [])
canvas._handle_message(canvas,{'type': 'initialized'},[])
canvas._handle_message(canvas,{'type': 'draw'},[])
# currently needs to inherit form axeswidget rather than# Widget due to limitations in mpl-interactionsclassprogress(AxesWidget):
"""A small widget to pass as a kwarg to mpl-interactions"""def__init__(self):
self._val=0self._observers=CallbackRegistry()
@propertydefval(self):
returnself._val@val.setterdefval(self, value):
# could add validation hereself._val=valueself._observers.process("changed", value)
defon_changed(self, func):
returnself._observers.connect("changed", lambdaval: func(val))
deff(t):
ift==0:
# otherwise line doesn't get made and we will# get weird errors later onreturn [0,0]
# make some fake datasteps=np.arange(t)
return [steps, np.sin(steps)]
# make our widget for updating the plotprog=progress()
withplt.ioff():
fig, ax=plt.subplots()
display_immediately(fig) # only necessary with ipympl backendiplt.plot(f, t=prog, parametric=True)
foriinrange(1, 10):
# do some work that will update our data sources# make the updates plotprog.val=i# mpl-interactions should be handling this draw for us# but it only ever does draw_idle# should add an option to draw immediately.fig.canvas.draw()
time.sleep(.5)
The text was updated successfully, but these errors were encountered:
Should probably come up with a better name for that widget and then incorporate it into the library. Would also be sweet if this worked with ipywidgets progressbars
oop turns out it just does work immediately - pretty sweet:
prog=IntProgress()
withplt.ioff():
fig, ax=plt.subplots()
display_immediately(fig) # only necessary with ipympl backendiplt.plot(f, t=prog, parametric=True)
foriinrange(1, 10):
# do some work that will update our data sources# make the updates plotprog.value=i# mpl-interactions should be handling this draw for us# but it only ever does draw_idle# should add an option to draw immediately.fig.canvas.draw()
time.sleep(.5)
Problem
This package has put a lot of work into figuring out how to update various matplotlib artists. However sometimes people want to update a plot in a loop instead of wiht sliders, for example see matplotlib/ipympl#425. They should still be able to do this using mpl-interactions, but it's not obvious how to do this.
attn: @henrypinkard for inspiring this
Suggested Improvement
Include an example of something like this:
The text was updated successfully, but these errors were encountered: