How to update an imshow inside a ipywidgets layout?

2.6k views Asked by At

Problem

I want to display a slider linked to a Play button from ipywidgets and then update an imshow plot from matplotlib. However I want the image to stay there and only draw the artists that changed and not have to clear the output to draw everything again. bqplot handles this situation quite well but the displaying a gridheatmap has revealed challenging for editing tick labels and other parts of the design that matplotlib handles so well. Some people have used interact but that approach limits my freedom to handle traitlets.

My code

slider=wd.IntSlider(value=0,min=0,max=49,continuous_update=False)
play_button=wd.Play(
    value=0,
    min=0,
    max=49,
    step=1,
    interval=850,
    description="Press play",
    disabled=False
)

def slider_update():
    global im
    global fig
    slider.value=change.new
    label.value="Time: {}".format(slider.value)
    
    with out:
        new_data=random_data[:,:,slider.value]
        im.set_data(new_data)
        im.set_clim(0,np.max(new_data))

        background = fig.canvas.copy_from_bbox(ax.bbox)
        fig.canvas.flush_events()
        fig.canvas.restore_region(background)
        ax.draw_artist(im)
        fig.canvas.blit(ax.bbox)
        
        plt.show(fig)
    
label=wd.Label("Time: 0")
# slider.observe(slider_update, "value")
wd.jslink((play_button,"value"),(slider,"value"))
widget=wd.interactive(slider_update,idx=slider)

out=wd.Output()
app=wd.VBox([wd.HBox([play_button,slider,label]),out])


display(app)

Output

enter image description here

Intended output

enter image description here

But the image updates only the artiosts so as to get a smooth animation (this last image was accomplished by having display(app,fig) which does not do what I want.

Can anyone give a hand?

0

There are 0 answers