plotly / plotly/plotly.py

incorrect behavior of on_selection callback attached to multiple subplots on Linux

Ouverte
#3,538 0 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

bug P3
Langage dominant
Python
Étoiles
18.8k
Forks
2.8k
Merge moyen
16 h 26 min
PR mergées (30 j)
21

Description

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:

  1. When making selections in some subplots, the behavior works perfectly as expected.
  2. 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:
0bd748fb934b826526ad2768a79553420ecd0c6f

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par exécuter sous Linux la reproduction minimale de FigureWidget présentée dans l’issue, en vous concentrant sur selection_callback_handler et les deux enregistrements on_selection. Comparez le comportement de la sélection dans les subplots supérieur et inférieur, puis identifiez pourquoi la sélection supérieure revient à son état précédent ; le travail est considéré comme terminé lorsque la sélection horizontale partagée reste synchronisée dans les deux subplots.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
jupyter-notebook, pandas, plotly, 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

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.