Python - Plotly - Range滑块仅适用于垂直堆叠图形中的顶部图形

ig9co6j1  于 4个月前  发布在  Python
关注(0)|答案(1)|浏览(76)

我已经设法得到几乎正是我想从这个使用python的第一次,但我坚持与最后一块。
我不能让Plotly范围滑块影响两个图。我有垂直堆叠的子图,图1和图2。堆叠到this_figure。
我正在将数据写入CSV并从那里阅读到下面的数字中。

figure1 = px.line(df,  x = 'time', y = 'temp', title='Temperature')
    figure2 = px.line(df,  x = 'time', y = ['Dosinga','Dosingb'], title='Dosing')

    figure1_traces = []
    figure2_traces = []
    for trace in range(len(figure1["data"])):
        figure1_traces.append(figure1["data"][trace])
    for trace in range(len(figure2["data"])):
        figure2_traces.append(figure2["data"][trace])

    #Create a 1x2 subplot
    this_figure = sp.make_subplots(rows=2, cols=1, subplot_titles =['Temperature', 'Dosing'])
    this_figure.update_layout(height = 1000, width = 2500, title_text = "Fishtank")

    for traces in figure1_traces:
        this_figure.append_trace(traces, row=1, col=1)
    for traces in figure2_traces:
        this_figure.append_trace(traces, row=2, col=1)

    
    this_figure.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1D", step="day", stepmode="backward"),
                dict(count=7, label="1W", step="day", stepmode="backward"),
                dict(count=1, label="1M", step="month", stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
    )
    
    #the subplot as shown in the above image
    this_figure.write_html("/home/pi/shared/Graph.html")

字符串
我试过把范围滑块分别添加到每个图中,如下所示,但对两个图都是如此。我也试过一次只把范围滑块添加到其中一个图中。
所有这些尝试都没有引起代码问题,但是范围滑块和范围选择器按钮完全消失。

figure_1.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1, label="1D", step="day", stepmode="backward"),
                dict(count=7, label="1W", step="day", stepmode="backward"),
                dict(count=1, label="1M", step="month", stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
    )

yh2wf1be

yh2wf1be1#

如果你想为每个图添加一个范围滑块,就像你对this_figure.update_layout( xaxis=dict(等所做的那样添加第一个滑块,然后以同样的方式添加第二个滑块,但指定xaxis2
为了确保您可以看到屏幕上的所有内容,并且不会重叠,您可能需要在初始化子图时调整vertical_spacing。例如:make_subplots(rows=2, cols=1, subplot_titles=['Temperature', 'Dosing'], vertical_spacing=0.5)

相关问题