我已阅读情节 slider 说明:https://plotly.com/python/sliders/ ,但仍然找不到在一个图中创建两个 slider 的方法。我是情节新手,所以任何建议都会有所帮助。
最佳答案
显然这是不可能的(参见 plotly community post ) - 但由于这篇文章来自 18 年,现在可能有一个解决方案(我不知道)。 如果您使用的是 Jupyter 笔记本,则可以使用FigureWidget(按照帖子中的建议),如下所示:
import plotly.graph_objects as go
import numpy as np
def linear_function(a,b,x):
return a*x+b
a0 = 0.1
b0 = 0.1
x = np.arange(-10,10,0.1)
data = [go.Scatter(x=x,y=linear_function(a=a0,b=b0,x=x))]
fig = go.FigureWidget(data=data)
fig.update_yaxes(autorange=False,range=(-3,3))
@interact(a=(0, 1, 0.1), b=(0, 1, 0.1))
def update(a=a0, b=b0):
with fig.batch_update():
fig.data[0].y = linear_function(a=a,b=b,x=x)
return fig
但是,使用交互时,渲染有时会出现明显的延迟。因此,我想指出,在很多情况下,修复一个变量并仅交互设置另一个变量可能是更好的解决方案:
# Fixing a
a = 0.1
fig = go.Figure()
fig.update_layout(title = f"Linear function a*x+b with a fixed at {a} and b adjustable")
fig.update_yaxes(autorange=False,range=(-3,3))
parameters = np.arange(0, 5, 0.1)
for b in parameters:
fig.add_trace(
go.Scatter(
visible=False,
x=x,
y=linear_function(a=a,b=b,x=x),
))
# Make 0th trace visible
fig.data[0].visible = True
# Create and add slider
steps = []
for i in range(len(fig.data)):
step = dict(
method="update",
label=f"{parameters[i]: .2f}",
args=[{"visible": [False] * len(fig.data)}],
)
step["args"][0]["visible"][i] = True
steps.append(step)
sliders = [dict(
active=0,
pad={"t": 50},
steps=steps
)]
fig.update_layout(
sliders=sliders
)
fig.show()
我希望这有帮助!
关于slider - 在Python中添加两个 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71143764/