使用 fig.update_layout Plotly 更新跟踪的可见性 [英] Update visibility of Traces with fig.update_layout Plotly
问题描述
从这个问题开始:设置sqrt 作为 yaxis 比例从下拉或按钮-Python/Plotly
我想:
- 定义包含所有轨迹的图:visible = False
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"], visible = False) #set both vis to False
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=np.sqrt(df["lifeExp"]), visible = False)
- 创建一个可以使 trace1 或 trace2 可见的更新按钮
# buttons for updatemenu
buttons = [dict(method='restyle',
label='linear',
visible=True,
args=[{'label': 'linear',
'visible':[True, False],
}
]),.....
um = [{'buttons':buttons,
'direction': 'down'}
]
fig.update_layout(updatemenus=um)
- 将初始条件设置为 trace1 = 可见,trace2 = 不可见
fig.update_layout(dict(args = {"visible": [True, False]}))
前2点已经解决了.我找不到预设初始显示条件的方法.在此示例中,我可以在创建跟踪时轻松更改可见性,但在我的实际问题中会更难.
The first 2 points have been solved. I cannot find a way to preset the initial display conditions. In this example i could easily change the visibility while creating the traces, but in my real problem it would be harder.
这是完整的例子:
import numpy as np
import plotly.graph_objects as go
import plotly.express as px
df = px.data.gapminder().query("year == 2007")
# figure setup
fig = go.Figure()
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"], visible = False) #set both vis to False
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=np.sqrt(df["lifeExp"]), visible = False)
# buttons for updatemenu
buttons = [dict(method='restyle',
label='linear',
visible=True,
args=[{'label': 'linear',
'visible':[True, False],
}
]),
dict(method='restyle',
label='sqrt',
visible=True,
args=[{'label': 'linear',
'visible':[False, True],
}
])]
# specify updatemenu
um = [{'buttons':buttons,
'direction': 'down'}
]
fig.update_layout(updatemenus=um)
#Update plot before showing to make 1st trace visible COMMENTED OUT CODE NOT WORKING
# fig.update_layout(dict(args = {"visible": [True, False]}))
fig.show()
推荐答案
在这种情况下,您可以有条件地更新跟踪,如图所示 此处.
In this case you can conditionally update the trace as shown here.
首先,当您添加每个跟踪时,给它一个 name
(在这种情况下使用 'linear' 和 'sqrt'):
First when you add each trace give it a name
(using 'linear' and 'sqrt' in this case):
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"],
visible = False, name='linear')
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=np.sqrt(df["lifeExp"]),
visible = False, name='sqrt')
然后使用条件更新,如果名称为线性",则设置 visible=True
:
Then later on use a conditional update that sets visible=True
if the name is "linear":
fig.for_each_trace(
lambda trace: trace.update(visible=True) if trace.name == "linear" else (),
)
完整示例:
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
df = px.data.gapminder().query("year == 2007")
fig = go.Figure()
# adding names for reference
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=df["lifeExp"],
visible = False, name='linear')
fig.add_scatter(mode="markers", x=df["gdpPercap"], y=np.sqrt(df["lifeExp"]),
visible = False, name='sqrt')
# buttons for updatemenu
buttons = [dict(method='restyle',
label='linear',
visible=True,
args=[{'label': 'linear',
'visible':[True, False],}]),
dict(method='restyle',
label='sqrt',
visible=True,
args=[{'label': 'linear',
'visible':[False, True],}])]
# specify updatemenu
um = [{'buttons':buttons, 'direction': 'down'}]
fig.update_layout(updatemenus=um)
# Conditionally Updating Traces
fig.for_each_trace(
lambda trace: trace.update(visible=True) if trace.name == "linear" else (),
)
fig.show()
这篇关于使用 fig.update_layout Plotly 更新跟踪的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!