I am using an ipywidgets
slider to slice a 2D numpy
array along the y dimension in a Jupyter notebook.
The interaction with the slider seems very sluggish/laggy when using plotly
. An equivalent matplotlib
solution is much faster/reactive. I post a copy of my code below (the poor performance is more visible when 2 traces are plotted).
Am I doing anything wrong?
If not, what could be the reason?
I am suspecting some of the performance loss could come from copies of the data being made between the numpy
arrays and plotly
's Javascript backend. Maybe plotly
does a lot of extra work behind the scenes that also slow things down? (i’ve tried to keep the yrange constant to avoid constant auto layout calculation but this does not seem to help).
Thanks for any help!
import plotly.graph_objects as go
import ipywidgets as widgets
import numpy as np
import IPython.display as disp
import matplotlib.pyplot as plt
%matplotlib notebook
N = 1000
M = 500
xx = np.linspace(0.0, 12.0, N)
yy = np.linspace(0.0, 6.0, N)
x, y = np.meshgrid(xx, yy)
a = [np.sin(np.sqrt(x**2 + y**2))]
a.append(np.random.normal(a[0] * 0.1, 0.05))
fig1, ax = plt.subplots(1, 1)
ax.plot(xx, a[0][:, 0])
ax.plot(xx, a[1][:, 0])
ax.set_ylim([-1, 1])
sl = widgets.IntSlider(
value=0,
min=0,
max=M,
step=1,
continuous_update=True,
readout=True)
def update_y(change):
for i, line in enumerate(ax.lines):
line.set_ydata(a[i][:, change["new"]])
sl.observe(update_y, names="value")
disp.display(sl)
fig2 = go.FigureWidget(layout={"yaxis": {"range": [-1, 1]}})
fig2.add_trace(go.Scatter(x=xx, y=a[0][:, 0]))
fig2.add_trace(go.Scatter(x=xx, y=a[1][:, 0]))
sl2 = widgets.IntSlider(
value=0,
min=0,
max=M,
step=1,
continuous_update=True,
readout=True)
def update_y2(change):
for i in range(len(fig2.data)):
fig2.data[i].y = a[i][:, change["new"]]
sl2.observe(update_y2, names="value")
disp.display(widgets.VBox((fig2, sl2)))