slider - 在Python中添加两个 slider

标签 slider plotly-python

我已阅读情节 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/

相关文章:

python - 我可以计算 p 值并使用 plotly 添加星号吗?

javascript - 简单的 JS slider - 旋转不起作用

javascript - 滑动 slider 。准备一张幻灯片并模拟过渡到它

javascript - 是否有类似附件的垂直盖流 slider ?

javascript - HTML Range 元素值未使用 Angular6 函数更新

python - 如何在plotly scattergeo中自定义颜色条?

python - Plotly:如何在不同的轨迹上绘制带有时间值的标记?

python - 为 plotly 人物的每个方面添加痕迹

python - 大小为 0 的标记在 Plotly Express 散点图中不可见

html - 输入类型范围 css - 背景颜色