I have two subplots: A surface plot on the left and a scatterplot on the right. My goal is to:
Left side: Animate successive points on the surface plot, via Scatterplot3d
Right Side: Animate successive regression lines on top of the scatterplot.
My solution is modeled after https://chart-studio.plotly.com/~empet/15243/animating-traces-in-subplotsbr/#/, but I have not been able to produce any of the animations, nor the left scatterplot.
Here’s my code so far:
# Initialize figure with 2 subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=("Gradient Descent", "Linear Regression"),
specs=[[{'type': "surface"}, {"type": "scatter"}]])
# Add surface and scatter plot
fig.add_trace(
go.Surface(x=theta0, y=theta1, z=Js, colorscale="YlGnBu", showscale=False),
row=1, col=1)
fig.add_trace(
go.Scatter(x=model.X_train_[:,1], y=model.y_train_,
name="ames",
mode="markers",
marker=dict(color="#1560bd")), row=1, col=2)
# Create frames definition
frames = [go.Frame(
dict(
name = k,
data = [
go.Scatter3d(x=[theta0[k]], y=[theta1[k]], z=[cost[k]], mode='markers', marker=dict(color="red", size=16)),
go.Scatter(x=xx, y=yy[k], mode="lines")
],
traces=[0,1])
) for k in range(n_frames)]
# Update the menus
updatemenus = [dict(type='buttons',
buttons=[dict(label="Play",
method="animate",
args=[[f'{k}' for k in range(n_frames)],
dict(frame=dict(duration=100, redraw=False),
transition=dict(duration=0),
easing="linear",
fromcurrent=True,
mode="immediate")])],
direction="left",
pad=dict(r=10, t=85),
showactive=True, x=0.1, y=0, xanchor="right", yanchor="top")]
sliders = [{"yanchor": "top",
"xanchor": "left",
"currentvalue": {"font": {"size": 16}, "prefix": "Iteration: ", "visible":True, "xanchor": "right"},
'transition': {'duration': 100.0, 'easing': 'linear'},
'pad': {'b': 10, 't': 50},
'len': 0.9, 'x': 0.1, 'y': 0,
'steps': [{'args': [[k], {'frame': {'duration': 100.0, 'easing': 'linear', 'redraw': False},
'transition': {'duration': 0, 'easing': 'linear'}}],
'label': k, 'method': 'animate'} for k in range(n_frames)
]}]
fig.update(frames=frames)
fig.update_layout(
xaxis=dict(range=[theta0_min, theta0_max], autorange=False, zeroline=False),
yaxis=dict(range=[theta1_min, theta1_max], autorange=False, zeroline=False),
title=dict(xanchor='center', yanchor='top', x=0.5, y=0.9),
font=dict(family="Open Sans"),
updatemenus=updatemenus,
sliders=sliders,
template='plotly_white')
if directory and filename:
filepath = os.path.join(directory, filename)
fig.write_html(filepath, include_mathjax='cdn')
pio.renderers.default = "browser"
fig.show()
I’d appreciate some help here, because I am a bit in the weeds on this one.