plotly / plotly/plotly.py

Multiple traces in a FigureWidget break callbacks

Offen
#3,677 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug P3
Vorherrschende Sprache
Python
Sterne
18.8k
Forks
2.8k
Ø Merge
16 Std. 26 Min.
Gemergte PRs (30 T.)
21

Beschreibung

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:
Screen Shot 2022-04-12 at 16 32 48

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:
Screen Shot 2022-04-12 at 16 35 08

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):
Screen Shot 2022-04-12 at 16 35 16

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere das FigureWidget-Beispiel mit zwei Traces mit plotly 5.7.0, JupyterLab 3.3.2 und ipywidgets 7.7.0. Beginne damit, die Verarbeitung von f.data[i].on_click(callback) nachzuverfolgen und die Callbacks für den ersten und den letzten Trace zu vergleichen. Erledigt ist die Aufgabe, wenn das Klicken auf einen der beiden Traces den angeklickten Trace und den Punkt korrekt meldet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
jupyter-notebook, python
Bereich
data-visualization
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.