如何仅使用一个滑块处理所有子图? [Python-破折号]

How can I handle all my subplot graphs just using a single slider? [Python-Dash]

我正在为我的图使用子图结构,我想使用滑块在 x 轴上进行范围调整。我尝试同时浏览我的图表,但它不起作用。回调例程破坏子图结构(使用 plotly.tools.make_subplots 创建)。

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from plotly import tools
from plotly import graph_objs as go

fig = tools.make_subplots(rows=2, shared_xaxes=True)
fig.add_scatter(x=[1,2,3], y=[2,1,2])
fig.add_scatter(x=[1,2,3], y=[5,3,3], yaxis='y2')


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[

dcc.Graph(
    id='graph1',
    figure = fig
),

dcc.Slider(
    id='temp-slider',
    min=0,
    max=10,
    step=0.2,
    value=0
)

])

@app.callback(
    Output('graph1','figure'), [Input('temp-slider','value')]
)

def update_graph(value): 
    out = dict(
        data = fig.data,
        layout = dict(
            xaxis = dict(
                range = [value,value+1]
            )
        )
    )
    return out


if __name__ == '__main__':
    app.run_server(debug=True)

我需要一个子图,它使用相同的滑块对两个图进行排列

由于您再次重新创建图表,因此子图布局丢失,请仅更新现有 figure 的特定 range 属性。请参考下面的示例代码,如果这能解决您的问题,请告诉我。

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from plotly import tools
from plotly import graph_objs as go

fig = tools.make_subplots(rows=2, shared_xaxes=True)
fig.add_scatter(x=[1,2,3], y=[2,1,2])
fig.add_scatter(x=[1,2,3], y=[5,3,3], yaxis='y2')
fig.layout.xaxis.dtick=0.5

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div(children=[

dcc.Graph(
    id='graph1',
    figure = fig
),

dcc.Slider(
    id='temp-slider',
    min=0,
    max=10,
    step=0.2,
    value=0
)

])

@app.callback(
    Output('graph1','figure'), [Input('temp-slider','value')]
)

def update_graph(value): 
    fig.layout.xaxis.range = [value,value+1]
    return fig


if __name__ == '__main__':
    app.run_server(debug=True)