Skip to content

[BUG] Resample does not dynamically update for multiple xaxis #330

@clayton13

Description

@clayton13

Describe the bug 🖍️
When a go.Scattergl is created with an additional xaxis an exception is created and no dynamic updates occur. Looks similar to #322, where the self._grid_ref and self._xaxis_list do not appear to be set properly preventing updates beyond the initial xaxis.

Reproducing the bug 🔍

import plotly.graph_objects as go
import numpy as np
from plotly_resampler import FigureResampler

x = np.arange(1_000_000)
noisy_sin = (np.sin(x / 200) + np.random.randn(len(x)) / 10) * 1000

fig = FigureResampler(go.Figure())
fig.add_trace(go.Scattergl(name='noisy sine', showlegend=True),
              hf_x=x,
              hf_y=noisy_sin)
fig.add_trace(
    go.Scattergl(name='noisy sine 2', showlegend=True, xaxis='x2'),
    hf_x=x,
    hf_y=-1 * noisy_sin,
)
fig.update_layout(
    {'xaxis2': {
        'title': 'xaxis2 title',
        'overlaying': 'x',
        'side': 'top'
    }})
fig.show_dash(mode='inline')

Leading to a traceback:

...
  File "c:\Users\claytonwhite\ee_venv\lib\site-packages\plotly_resampler\figure_resampler\figure_resampler_interface.py", line 485, in _check_update_figure_dict
    trace_xaxis_filter: List[str] = layout_trace_mapping[layout_xaxis_filter]
KeyError: 'xaxis2'

Expected behavior 🔧
Expect both traces to update.

Environment information:

  • OS: Windows 10
  • Python environment:
    • Python version: Python 3.10.6
    • plotly-resampler environment: Dash web app, Chrome
  • plotly-resampler version: 0.10.0
  • plotly version: 5.24.1

Additional context
Hotfix (or rather hack) can be applied to dynamically populate for this case:

# edge case: an empty `go.Figure()` does not yet contain axes keys
if self._grid_ref is None:
return {"xaxis": ["x"]}

        if self._grid_ref is None:
            axis =  list(set([getattr(x, 'xaxis') or 'x' for x in self.data]))
            mappings = {}
            for ax in axis:
                val = list(map(int, re.findall(r'\d+', ax)))
                if val:
                    mappings[f'xaxis{val[0]}'] = [ax]
                else:
                    mappings[f'xaxis'] = ['x']
            return mappings # {'xaxis2': ['x2'], 'xaxis': ['x']}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions