Black Lives Matter. Please consider donating to Black Girls Code today.

Updating plot in notebook

I’m just learning plotly (thanks!) and wondering how I can dynamically update a plot. Here’s my first attempt at something simple:

from ipywidgets import IntSlider, VBox
import numpy as np
import plotly.graph_objects as go
x = np.random.randn(8)
y = np.random.randn(8)
fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours=dict(coloring='none')))
islide = IntSlider()
def on_value_change(change):
    global x,y,fig
    ival = change['new']
    x = np.random.randn(ival)
    y = np.random.randn(ival)
    fig = go.FigureWidget(data=go.Histogram2dContour(x=x, y=y, contours=dict(coloring='heatmap')))

islide.observe(on_value_change, names='value')
VBox([islide,fig])