Button update does work when selecting specific traces
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.
Hi @gilbertovilarunc. It's currently not possible to switch to a completely new trace with distinct data and layout if that's what you're trying to accomplish.
See this discussion: https://community.plotly.com/t/change-a-specific-trace-data-with-button/71050/14
And the accompanying feature request: https://github.com/plotly/plotly.py/issues/3994
Hi - we are tidying up stale issues and PRs in Plotly's public repositories so that we can focus on things that are still important to our community. Since this one has been sitting for a while, I'm going to close it; if it is still a concern, please add a comment letting us know what recent version of our software you've checked it with so that I can reopen it and add it to our backlog. If you'd like to submit a PR, we'd be happy to prioritize a review, and if it's a request for tech support, please post in our community forum. Thank you - @gvwilson