Multiple traces in a FigureWidget break callbacks
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 18.8k
- Forks
- 2.8k
- Merge moyen
- 16 h 26 min
- PR mergées (30 j)
- 21
Description
Hello!
I am using
plotly 5.7.0
jupyterlab 3.3.2
ipywidgets 7.7.0
(these are the latest versions of each package)
I am not using Dash.
What I am trying to achieve is a simple FigureWidget with two scatter plots in it, each with a callback that prints the element the user has clicked on. However, the callbacks are fired incorrectly. The trace object given to the callback function is always the last added trace, not the one that the user clicked on.
Here is a minimal example which I adapted from the official documentation (https://plotly.com/python/click-events/#update-points-using-a-click-callback)
import plotly.graph_objects as go
from ipywidgets import Output
import numpy as np
import ipywidgets
np.random.seed(1)
scatter_1 = go.Scatter(x=np.random.rand(100), y=np.random.rand(100), mode='markers', hovertext="hello, I am scatter 1", marker={"symbol": "triangle-up", "size": 10, "color": "red"},)
scatter_2 = go.Scatter(x=np.random.rand(100), y=np.random.rand(100), mode='markers', hovertext="hello, I am scatter 2", marker={"symbol": "triangle-down", "size": 10, "color": "blue"},)
f = go.FigureWidget()
f.add_trace(scatter_1)
f.add_trace(scatter_2)
f.layout.hovermode = 'closest'
f
This correctly renders the figure:

Then, I add a callback, one to each trace, by manipulating f.data:
o = ipywidgets.Output()
@o.capture(clear_output=True)
def callback(trace, points, selector):
print(trace)
print(points)
print(selector)
f.data[0].on_click(callback)
f.data[1].on_click(callback)
o
Clicking a blue triangle works, it correctly shows the clicked trace and point:

Clicking a red triangle yields wrong output. The point object is somehow empty, and the trace object is wrong (trace 1 even though trace 0 was clicked):

Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Reproduisez l’exemple FigureWidget à deux traces avec plotly 5.7.0, JupyterLab 3.3.2 et ipywidgets 7.7.0. Commencez par suivre le traitement de f.data[i].on_click(callback) et comparez les callbacks de la première et de la dernière trace. C’est terminé lorsque cliquer sur l’une ou l’autre des traces indique correctement la trace et le point cliqués.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- jupyter-notebook, python
- Domaine
- data-visualization
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100