plotly / plotly/plotly.py

Multiple traces in a FigureWidget break callbacks

Đang mở
#3,677 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug P3
Ngôn ngữ chính
Python
Star
18.8k
Fork
2.8k
Merge trung bình
16 giờ 26 phút
Pull request đã merge (30 ngày)
21

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện ví dụ FigureWidget gồm hai trace với plotly 5.7.0, JupyterLab 3.3.2 và ipywidgets 7.7.0. Bắt đầu bằng cách theo dõi việc xử lý f.data[i].on_click(callback) và so sánh các callback của trace đầu tiên và trace cuối cùng. Hoàn thành khi việc nhấp vào một trong hai trace báo cáo chính xác trace và điểm đã được nhấp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
jupyter-notebook, python
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.