I have two traces: a 2D and a 3D scatters, with the same number of points (the points in the scatters are related). I want that when I hover over a point in one of the traces it’ll highlight that point and the matching point in the other trace.
I’ve implemented this with a FigureWidget, running in Juypter notebook.
My problem is: It runs too slow for practical use. when moving the cursor from one point to another, it takes too long until the figure is updated. I’ve added a sample code, but my actual data is: the top plot is a grayscale image (i.e. go.Heatmap) with a scatter plotted over it, and the lower plot is a 3D plot.
Can you think of a clever solution that’ll make this runs fast? Maybe a different approach (without Jupyter)?
Here’s a sample code
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
size_small=10
size=300
x = np.arange(0,2*np.pi, 0.02)
y = np.sin(5*x)
sizes = np.full_like(x, size_small); colors = ['blue'] * len(x)
scat1 = go.Scatter(x=x, y=y, mode='markers', marker=dict(size=sizes, color=colors), name="t1")
scat2 = go.Scatter3d(x=x, y=y,z=x, mode='markers',
marker=dict(size=sizes, color=colors), name="t2")
subplots_fig = make_subplots(rows=2, cols=1, specs = [ [{"type":"xy"}], [{"type":"scene"}] ],
vertical_spacing=0.05,
row_heights =[0.5,0.5])
subplots_fig.update_layout(autosize=False, width=1000, height=1000, margin=dict(l=20, r=20))
subplots_fig.add_trace(scat1, row=1, col=1)
subplots_fig.add_trace(scat2, row=2, col=1)
def update_point(trace, points, state):
if not points.point_inds:
return
i = points.point_inds[0]
with fw.batch_update():
for scat in fw.data:
new_sizes = np.copy(sizes)
new_sizes[i]=15
new_colors = ['blue'] * len(scat.x);
new_colors[i] = 'red'
scat.marker.size = new_sizes
scat.marker.color = new_colors
fw = go.FigureWidget(subplots_fig)
for scat in fw.data:
scat.on_hover(update_point)
fw
and here’s a picture of how this looks: