简体   繁体   English

在 plotly 中添加一个带有子图的滑块

[英]Adding a slider to figure with subplots in plotly

I am trying to use plotly to show how 4 different functions change as one of their common parameters changes.我正在尝试使用 plotly 来显示 4 个不同的函数如何随着它们的公共参数之一的变化而变化。 I want 4 stacked subplots (one for each function) with a slider bar underneath it for that changing parameter.我想要 4 个堆叠的子图(每个函数一个),其下方有一个用于更改参数的滑块。 Essentially I'm hoping that it looks something like the Subplots with Shared X-Axes example on this page , except with a slider like the one shown here underneath it.从本质上讲,我希望它看起来像使用共享的X轴例如次要情节这个页面,除了与像所示的一个滑块在这里它的下面。 This page looks deceptively similar to what I need, except that I don't want a range slider here.这个页面看起来和我需要的很相似,只是我不想要这里的范围滑块。 This is not the actual code I'm using, but I'll post some code similar to mine in structure for convenience:这不是我正在使用的实际代码,但为了方便起见,我将发布一些与我的结构类似的代码:

def f(rho):
    dom = np.linspace(0, 1, 50)
    f1 = (dom - rho) ** 2
    f2 = np.sin(dom * rho)
    f3 = np.abs(dom - rho)
    f4 = dom ** rho
    return f1, f2, f3, f4

I want to see how these 4 functions change with rho in np.linspace(0.5, 2, 101) , so rho is the variable controlled by the slider.我想看看这 4 个函数如何随np.linspace(0.5, 2, 101) rho 变化,所以 rho 是由滑块控制的变量。 I like plotly because of some customizations I'd like to do and the ability to scroll over a figure to see function values.我喜欢 plotly 因为我想做一些自定义以及滚动图形以查看函数值的能力。

I eventually found this post on the plotly community forums, which answers the question with the following code example:我最终在 plotly 社区论坛上找到了这篇文章,它用以下代码示例回答了这个问题:

import plotly.graph_objs as go
from plotly.tools import make_subplots

fig = make_subplots(1, 2)

fig.add_scatter(y=[1, 3, 2], row=1, col=1, visible=True)
fig.add_scatter(y=[3, 1, 1.5], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[2, 2, 1], row=1, col=1, visible='legendonly')
fig.add_scatter(y=[1, 3, 2], row=1, col=2, visible=True)
fig.add_scatter(y=[1.5, 2, 2.5], row=1, col=2, visible='legendonly')
fig.add_scatter(y=[2.5, 1.2, 2.9], row=1, col=2, visible='legendonly')

steps = []
for i in range(3):
    step = dict(
        method = 'restyle',  
        args = ['visible', ['legendonly'] * len(fig.data)],
    )
    step['args'][1][i] = True
    step['args'][1][i+3] = True
    steps.append(step)

sliders = [dict(
    steps = steps,
)]

fig.layout.sliders = sliders

go.FigureWidget(fig)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM