plotly / plotly/plotly.py

Multiple traces in a FigureWidget break callbacks

未关闭
#3,677 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P3
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 plotly 5.7.0、JupyterLab 3.3.2 和 ipywidgets 7.7.0 复现包含两条轨迹的 FigureWidget 示例。首先跟踪 f.data[i].on_click(callback) 的处理过程,并比较第一条和最后一条轨迹的回调。点击任一轨迹都能正确报告被点击的轨迹和点,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
jupyter-notebook, python
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。