plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Button update does work when selecting specific traces

Open gilbertovilarunc opened this issue 3 years ago • 0 comments

Thanks for your interest in Plotly.py!

Before opening an issue, please search for existing and closed issues :)

Please accompany bug reports with a reproducible example. Please use the latest version of plotly.py in your report unless not applicable.

Note that GitHub Issues are meant to be used for bug reports and feature requests only. Implementation or usage questions should be asked on community.plotly.com or on Stack Overflow (tagged plotly).

Similar issue reported here: https://chart-studio.plotly.com/~empet/15569/updatemenus-bug-when-a-single-trace-is/#/

My example here:

import pandas as pd
import numpy as np


def data_sample():
    start_date = pd.Timestamp("2020-01-01")
    end_date = pd.Timestamp("2021-01-31")
    index = pd.date_range(start_date, end_date, freq="D")
    df = pd.DataFrame(index=index)
    df["x1"] = (np.random.randn(len(index)) + 1) * 10000
    df["x2"] = (np.random.randn(len(index)) + 1) * 20000
    df["x3"] = (np.random.randn(len(index)) + 1) * 30
    df["x4"] = (np.random.randn(len(index)) + 1) * 40
    df["x5"] = (np.random.randn(len(index)) + 1) * 500
    df['y'] = np.random.randn(len(index)) +\
        5*df["x1"] + 10*df["x2"] + 20*df["x3"] + 30*df["x4"] - 40*df["x5"]
    return df.clip(lower=0)
ts_data = data_sample().iloc[:10]

from plotly.subplots import make_subplots
fig_go = make_subplots(rows=2, cols=1, shared_xaxes=True)
for col in ts_data:
    fig_go.add_trace(
        go.Bar(
            x=ts_data.index, y=ts_data[col], name=col
        ),
        row=1,
        col=1
    )
for col in ts_data:
    fig_go.add_trace(
        go.Bar(
            x=ts_data.index, y=ts_data[col], name=col
        ),
        row=2,
        col=1
    )
    
my_button_type = [
    {
        'label': "Bar",
        'method': "update",
        'args': [{"type": "bar"}, {}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Scatter",
        'method': "update",
        'args': [{"type": "scatter", 'mode': 'markers'}, {}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Line",
        'method': "update",
        'args': [{"type": "scatter", "mode": "lines"}, {}, [0, 1, 2, 3, 4, 5]]
    }
]
my_button_stack = [
    {
        'label': "Non-stacked",
        'method': "update",
        'args': [{"fill": None, "stackgroup": None}, {"barmode": "group"}, [0, 1, 2, 3, 4, 5]]
    },
    {
        'label': "Stacked",
        'method': "update",
        'args': [{'fill': 'tozeroy', "stackgroup": 1}, {"barmode": "relative"}, [0, 1, 2, 3, 4, 5]]
    }
]
my_button_pct = [
    {
        'label': "Absolute",
        'method': "update",
        'args': [{"visible": abs_visible}, {}, [0, 1, 2, 3, 4, 5]],
    },
    {
        'label': "Percentage",
        'method': "update",
        'args': [{"visible": pct_visible}, {}, [0, 1, 2, 3, 4, 5]]
    }
]

fig_go.update_layout({
        'updatemenus': [
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.1,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_type
            },
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.25,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_stack
            },
            {
                'type': "dropdown",
                'direction': 'down',
                'x': 0.4,
                'y': 1.15,
                'showactive': True,
                'active': 0,
                'buttons': my_button_pct
            }
    ]
})
fig_go.update_layout(hovermode='x unified')
fig_go.show()

The problem seems to be only on the "Stack/Non-stacked" dropdown. Maybe because its the only one that changes both data and layout of the traces.

gilbertovilarunc avatar Aug 29 '22 02:08 gilbertovilarunc