尝试使用 2 个滑块来控制
Scatter
绘图对象的 2 个子集。我正在尝试修改示例代码。这是我到目前为止所拥有的:
import plotly.io as pio
pio.renderers.default = "iframe"
import plotly.graph_objects as go
import numpy as np
# Create figure
fig = go.Figure()
print(type(fig.data))
print(f"fig.data: {fig.data}")
# Add traces, one for each slider step
for step in np.arange(0, 5, 0.1):
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="#00CED1", width=6),
name="𝜈 = " + str(step),
x=np.arange(0, 10, 0.01),
y=np.sin(step * np.arange(0, 10, 0.01))))
for step in np.arange(0, 5, 0.1):
fig.add_trace(
go.Scatter(
visible=False,
line=dict(color="red", width=6),
name="second" + str(step),
x=np.arange(0, 10, 0.01),
y=np.sin(step * np.arange(0, 10, 0.01))
)
)
# Make 10th trace visible
fig.data[10].visible = True
fig.data[80].visible = True
print("n data figs: ", len(fig.data))
# Create and add slider
steps = []
for i in range(0, 50):
step = dict(
method="restyle",
args=[{"visible": [False] * len(fig.data)},
{"title": "Slider switched to step: " + str(i)}], # layout attribute
)
step["args"][0]["visible"][i] = True # Toggle i'th trace to "visible"
steps.append(step)
steps2 = []
for i in range(50, len(fig.data)):
step = dict(
method="restyle",
args=[{"visible": [False] * len(fig.data)},
{"title": "Slider switched to step: " + str(i)}], # layout attribute
)
step["args"][0]["visible"][i] = True
steps2.append(step)
sliders = [
dict(active=10,currentvalue={"prefix": "Frequency (slider 1): "},pad={"t": 50},steps=steps),
dict(active=80,currentvalue={"prefix": "Frequency (slider 2): "},pad={"t": 150}, steps=steps2)
]
fig.update_layout(
sliders=sliders
)
fig.show()
所以这里的问题是,当我添加第二批滑块选项时,我将所有前 50 个滑块布局值设置为
visible=False
。这不是我想要的。理想情况下,第一个滑块应控制前 50 个Scatter
图的可见性,第二个滑块应控制后 50 个scatter
图的可见性,而不会影响前 50 个。
也许另一种方法是创建两个单独的
Figure
对象,但将它们渲染在同一个轴上。我一直无法弄清楚如何用 python 做到这一点,尽管也有可能我没有在谷歌上搜索正确的关键字。
任何想法将不胜感激。理想情况下,我希望能够将绘图导出为 html,以便它可以作为
iframe
. 嵌入