【问题标题】:Change subplot y axis text via dropdown selection in plotly通过 plotly 中的下拉选择更改子图 y 轴文本
【发布时间】:2022-11-14 05:29:44
【问题描述】:

我有一个情节,其中有 3 个子情节和一个下拉菜单。当我从下拉列表中选择一个时,我想更改 yaxis 子图文本。 yaxis.title or yaxis2.title 不工作。

注意:不使用破折号

如果我只能更改第 3 个子图 yaxis 标题文本(而不是第 1 个和第 2 个子图),有可能吗?

import plotly.graph_objs as go
from plotly import subplots 
trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True,horizontal_spacing=0.5)
fig.add_trace(trace0, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace0, 2, 1)
fig.add_trace(trace0, 3, 1)

update_menus = [go.layout.Updatemenu(
        active=0,
        buttons=list(
            [dict(label = 'All',
                  method = 'relayout',
                  args = [{'visible': [True, True, True,True,True]},
                          {'title': 'all',
                           'showlegend':True},
                         {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
             dict(label = 'First',
                  method = 'relayout',
                  args = [{'visible': [True, False, False,True,True]}, # the index of True aligns with the indices of plot traces
                          {'title': 'first',
                           'showlegend':True},
                         {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
             dict(label = 'Second',
                  method = 'relayout',
                  args = [{'visible': [False, True, False,True,True]},
                          {'title': 'second',
                           'showlegend':True},
                         {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
             dict(label = 'Third',
                  method = 'relayout',
                  args = [{'visible': [False, False, True, False,False]},
                          {'title': 'third',
                           'showlegend':True},
                         {'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
            ])
        )
               ]           

fig.show()

【问题讨论】:

  • 我不明白如何通过下拉菜单更改第三个y轴的标题?
  • 你的意思是下拉菜单包含不同的标签,用户会选择一个标签吗?

标签: plotly plotly-python


【解决方案1】:
  • 您需要使用update 而不是relayout,因为您更改了子图的数据和布局。

  • 您还应该将text添加到yaxis3.title以成为yaxis3.title.text

  • 最后,您需要将所有布局更新合并到args 的同一个字典中。

     import plotly.graph_objs as go
     from plotly import subplots 
     trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
     trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
     trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
     fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True,horizontal_spacing=0.5)
     fig.add_trace(trace0, 1, 1)
     fig.add_trace(trace1, 1, 1)
     fig.add_trace(trace2, 1, 1)
     fig.add_trace(trace0, 2, 1)
     fig.add_trace(trace0, 3, 1)
    
     update_menus = [go.layout.Updatemenu(
             active=0,
             buttons=list(
                 [dict(label = 'All',
                       method = 'update',
                       args = [{'visible': [True, True, True,True,True]},
                               {'title': 'all',
                                'showlegend':True,
                              'yaxis.title.text':'fgv','yaxis2.title.text':'ram','yaxis3.title.text':'gen'}]),
                  dict(label = 'First',
                       method = 'update',
                       args = [{'visible': [True, False, False,True,True]}, # the index of True aligns with the indices of plot traces
                               {'title': 'first',
                                'showlegend':True,
                                'yaxis.title.text':'fgv','yaxis2.title.text':'ram','yaxis3.title.text':'gen'}]),
                  dict(label = 'Second',
                       method = 'update',
                       args = [{'visible': [False, True, False,True,True]},
                               {'title': 'second',
                                'showlegend':True,
                                'yaxis.title.text':'fgv','yaxis2.title.text':'ram','yaxis3.title.text':'gen'}]),
                  dict(label = 'Third',
                       method = 'update',
                       args = [{'visible': [False, False, True, False,False]},
                               {'title': 'third',
                                'showlegend':True,
                              'yaxis.title.text':'fgv','yaxis2.title.text':'ram','yaxis3.title.text':'gen'}]),
                 ])
             )
                    ]           
    
     fig.update_layout(updatemenus=update_menus)
    

如果你想通过下拉菜单控制yaxis3的标题,你可以这样做:

import plotly.graph_objs as go
from plotly import subplots 
trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True)
fig.add_trace(trace0, 1, 1)
fig.add_trace(trace1, 1, 1)
fig.add_trace(trace2, 1, 1)
fig.add_trace(trace0, 2, 1)
fig.add_trace(trace0, 3, 1)

fig.update_layout(height=550,margin={"t":10},
    updatemenus=[
        dict(
            buttons=list([
                dict(label = 'All',
                  method = 'update',
                  args = [{'visible': [True, True, True,True,True]},
                          {'title': 'all',
                           'showlegend':True}]),
             dict(label = 'First',
                  method = 'update',
                  args = [{'visible': [True, False, False,True,True]}, 
                          {'title': 'first',
                           'showlegend':True}]),
             dict(label = 'Second',
                  method = 'update',
                  args = [{'visible': [False, True, False,True,True]},
                          {'title': 'second',
                           'showlegend':True}]),
             dict(label = 'Third',
                  method = 'update',
                  args = [{'visible': [False, False, True, False,False]},
                          {'title': 'third',
                           'showlegend':True}]),
            ]),
            direction="down",
            showactive=True,
            x=0.18,
            y=1.1,
            xanchor="left",
            yanchor="middle"
        ),
        dict(
            buttons=list([
                dict(
                    args=[{"yaxis3.title.text":"Seconds"}],
                    label="Seconds",
                    method="relayout"
                ),
                dict(
                    args=[{"yaxis3.title.text":"Minutes"}],
                    label="Minutes",
                    method="relayout"
                ),
                dict(
                    args=[{"yaxis3.title.text":"Hours"}],
                    label="Hours",
                    method="relayout"
                )
            ]),
            direction="down",
            showactive=True,
            x=0.4,
            xanchor="left",
            y=1.1,
            yanchor="middle"
        )
    ],
    
)

【讨论】:

    【解决方案2】:

    您需要使用update 方法而不是relayout(因为您要同时更改数据和布局)。 here 列出了不同的方法。

    如果我没记错的话,您希望轨迹按照您将它们添加到子图中的顺序可见,这意味着'first' 下拉列表应该导致显示轨迹[True, True, True, False, False],依此类推,第二个下拉列表应该会导致显示痕迹[False, False, False, True, False],依此类推...

    import plotly.graph_objs as go
    from plotly import subplots 
    trace0 = go.Scatter(x=[1, 2, 3], y=[4, 5, 6], mode="lines+markers")
    trace1 = go.Scatter(x=[1, 2, 3], y=[5, 4, 6], mode="lines+markers")
    trace2 = go.Scatter(x=[1, 2, 3], y=[6, 5, 4], mode="lines+markers")
    fig = subplots.make_subplots(rows=3,cols=1,shared_xaxes=True,horizontal_spacing=0.5)
    fig.add_trace(trace0, 1, 1)
    fig.add_trace(trace1, 1, 1)
    fig.add_trace(trace2, 1, 1)
    fig.add_trace(trace0, 2, 1)
    fig.add_trace(trace0, 3, 1)
    
    update_menus = [go.layout.Updatemenu(
            active=0,
            buttons=list(
                [dict(label = 'All',
                      method = 'update',
                      args = [{'visible': [True, True, True,True,True]},
                              {'title': 'all',
                               'showlegend':True,
                               'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
                 dict(label = 'First',
                      method = 'update',
                      args = [{'visible': [True, True, True, False, False]}, # the index of True aligns with the indices of plot traces
                              {'title': 'first',
                               'showlegend':True,
                               'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
                 dict(label = 'Second',
                      method = 'update',
                      args = [{'visible': [False, False, False,True,False]},
                              {'title': 'second',
                               'showlegend':True,
                               'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
                 dict(label = 'Third',
                      method = 'update',
                      args = [{'visible': [False, False, False, False,True]},
                              {'title': 'third',
                               'showlegend':True,
                               'yaxis.title':'fgv','yaxis2.title':'ram','yaxis3.title':'gen'}]),
                ])
            )
        ]           
    fig.update_layout(updatemenus=update_menus)
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2017-04-22
      • 2020-07-17
      • 2023-03-17
      • 2022-01-08
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      相关资源
      最近更新 更多