incorrect behavior of on_selection callback attached to multiple subplots on Linux
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 18.8k
- フォーク
- 2.8k
- 平均マージ
- 16時間 26分
- マージ済み PR(30日)
- 21
説明
From discussion here: https://community.plotly.com/t/re-behavior-of-on-selection-callback-attached-to-multiple-subplots/59824 ,
It seems like it could be a bug present on Linux but not on Windows.
I have a project with many subplots in one figure, and I'm trying to use a callback function to define behavior when a selection is made in a subset of the subplots. Since the selection behavior I want for each subplot is very similar, I really would like to avoid writing a unique callback function for each unique subplot--so I wrote one handler and attached it to all the subplots. However, it is having some strange issues:
- When making selections in some subplots, the behavior works perfectly as expected.
- For other subplots, I get erratic behavior. Sometimes none of the code in the callback is run, sometimes part of it runs. If I watch the plotting window very closely, I can actually see the callback execute correctly for a brief moment, maybe a few milliseconds. Then it reverts back to an inconsistent state from either before the callback ran, or as though part of it ran, but crashed.
I have tried to boil my issue down to the smallest code example I can. In the example, there are two subplots stacked vertically. The single callback function is attached to both plots.
Intended Behavior
When a horizontal box selection is made in either of the subplots, the same selection range is made in the other subplot.
Actual Behavior
Selections made in the lower plot behave as designed. Selections made in the upper plot flash the correct result for a split second, then revert back to pre-callback state.
Gif of the issue happening:

import pandas as pd
import plotly.graph_objects as go
from plotly.callbacks import BoxSelector
from plotly.express import colors
from plotly.subplots import make_subplots
# generate two sample data for subplots
df1: pd.DataFrame = pd.DataFrame(
{"x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], "y": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
)
df1.name = "df1"
df2: pd.DataFrame = pd.DataFrame(
{
"x": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
"y": [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3],
}
)
df2.name = "df2"
# generate plotting window
fig: go.FigureWidget = go.FigureWidget(make_subplots(rows=2, cols=1, shared_xaxes=True))
fig.add_trace(
go.Scattergl(
x=df1["x"],
y=df1["y"],
name=df1.name,
selected=dict(marker=dict(color=colors.qualitative.Dark24[5])),
),
row=1,
col=1,
)
fig.add_trace(
go.Scattergl(
x=df2["x"],
y=df2["y"],
name=df2.name,
selected=dict(marker=dict(color=colors.qualitative.Dark24[5])),
),
row=2,
col=1,
)
def selection_callback_handler(trace, points, selector) -> None:
# Can't use 0 points
if not points.xs:
return
if len(points.point_inds) < 2: # redundant I guess
return
# Don't use lasso select
if not isinstance(selector, BoxSelector):
return
# Only use continuous horizontal select
if not all(y - x == 1 for x, y in zip(points.point_inds, points.point_inds[1:])):
return
# Determine from which DataFrame we should pull the selection boundary
context_df: pd.dataFrame
if points.trace_name == df1.name:
context_df = df1
fig.layout.title = "df1"
elif points.trace_name == df2.name:
context_df = df2
fig.layout.title = "df2"
else:
return
# Maybe we are reentrant when a selection is added/modified from this callback?
# Try to guard but probably won't work in race condition...
# TODO try lock or semaphore with timeout?
if fig.data[0].selectedpoints and fig.data[1].selectedpoints:
return
# get selection boundary
x_min_point: int = points.point_inds[0]
x_max_point: int = points.point_inds[-1]
x_min: int = context_df.iloc[x_min_point]["x"]
x_max: int = context_df.iloc[x_max_point]["x"]
fig.data[0].selectedpoints = df1[
df1["x"].between(x_min, x_max, inclusive=True)
].index.values
fig.data[1].selectedpoints = df2[
df2["x"].between(x_min, x_max, inclusive=True)
].index.values
fig.data[0].on_selection(selection_callback_handler)
fig.data[1].on_selection(selection_callback_handler)
fig
$ uname -a
Linux [REDACTED] 5.15.0-2-amd64 #1 SMP Debian 5.15.5-2 (2021-12-18) x86_64 GNU/Linux
$ code --version
1.63.2
899d46d82c4c95423fb7e10e68eba52050e30ba3
x64
$ python --version
Python 3.10.1
$ pip freeze | grep -e pandas -e plotly -e ipywidgets -e ipykernel
ipykernel==6.6.0
ipywidgets==7.6.5
pandas==1.3.5
plotly==5.5.0
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、issue にある最小限の FigureWidget 再現例を Linux 上で実行し、selection_callback_handler と 2 つの on_selection 登録に注目します。上側と下側のサブプロットで選択の挙動を比較し、続いて上側の選択が元に戻る理由を特定します。両方のサブプロットで共有される水平選択が同期されたままになれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- jupyter-notebook, pandas, plotly, python
- 領域
- data-visualization
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100