I would like to set a “dynamic” visibility when using plotly.
I have a bunch of data which I have grouped in a drop-down menu.
I would like to be able to select a couple of series from each drop-down menu and keep it visible while I “browse” through different menus.
Here is MRE:
import plotly.graph_objects as go
import numpy as np
fig = go.Figure()
for i in range(1,4):
fig.add_trace(
go.Scatter3d
(
x=np.linspace(0, 1),
y=np.linspace(0, 1),
z=i*np.linspace(0, 1),
mode='markers',
name=f"g1_{i}",
visible = "legendonly",
legendgroup="" ,
)
)
for i in range(4,7):
fig.add_trace(
go.Scatter3d
(
x=np.linspace(0, 1),
y=np.linspace(0, 1),
z=i*np.linspace(0, 1),
mode='markers',
name=f"g2_{i}",
visible = "legendonly",
legendgroup="" ,
)
)
for i in range(7,10):
fig.add_trace(
go.Scatter3d
(
x=np.linspace(0, 1),
y=np.linspace(0, 1),
z=i*np.linspace(0, 1),
mode='markers',
name=f"g3_{i}",
visible = "legendonly",
legendgroup="" ,
)
)
fig.update_layout(
updatemenus=[
dict(
buttons=list([
dict(
args=[{"visible": [True if trace.visible else "legendonly" for trace in fig['data']]}],
label=("All"),
method="restyle",
),
dict(args=[{"visible": ["legendonly" if ("g2" in trace.name) else False for trace in fig['data']]}],
label="g2",
method="restyle",
),
]),
direction = "down",
showactive=True,
x=0.5,
xanchor="center",
y=1.1,
yanchor="top"
)])
Is it possible to define a function (callback) when pressing the “G2” button to keep whatever was visible and just show with “legendonly” was is available in the group defined in “G2”?
Best Regards
Tpj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.