Plotly updatemenus - 只更新特定参数

问题描述 投票:0回答:1

我正在寻找一种在绘图图形上有两个更新菜单按钮的方法。 一种是更改数据和 y 轴标题,一种是从线性刻度切换到对数刻度。 一般来说,下面的代码可以工作,但根据我使用的方法,我会丢失一些东西。

如果

buttons
方法是
update
那么当我切换参数时它会默认回到线性比例。 如果我使用
restyle
,y 轴标题将停止更新。 有没有一种方法可以只更新我错过的部分内容?

import plotly.graph_objects as go
import pandas as pd


def main():
    figure = go.Figure()

    df = pd.DataFrame([['sample 1', 1, 5, 10],
                       ['sample 2', 2, 20, 200],
                       ],
                      columns=['sample id', 'param1', 'param2', 'param3']
                      )
    options = ['param1', 'param2', 'param3']

    figure.add_trace(go.Bar(x=df['sample id'], y=df[options[0]]))
    figure.update_yaxes(title=options[0])

    buttons = []
    for option in options:
        buttons.append({'method': 'restyle',  ### this can be update or restyle, each has different issues
                        'label': option,
                        'args': [{'y': [df[option]],
                                  'name': [option]},
                                 {'yaxis': {'title': option}},
                                 [0]]
                        })
    scale_buttons = [{'method': 'relayout',
                      'label': 'Linear Scale',
                      'args': [{'yaxis': {'type': 'linear'}}]
                      },
                     {'method': 'relayout',
                      'label': 'Log Scale',
                      'args': [{'yaxis': {'type': 'log'}}]
                      }
                     ]
    figure.update_layout(yaxis_title=options[0],
                         updatemenus=[dict(buttons=buttons,
                                           direction='down',
                                           x=0,
                                           xanchor='left',
                                           y=1.2,
                                           ),
                                      dict(buttons=scale_buttons,
                                           direction='down',
                                           x=1,
                                           xanchor='right',
                                           y=1.2,
                                           )],
                         )
    figure.show()


if __name__ == '__main__':
    main()
python plotly
1个回答
0
投票

您需要对第一个下拉列表使用

update
方法(
restyle
仅更新 data
update
更新数据和布局)。

现在要解决这个问题,关键是在参数中使用

'yaxis.title': option
而不是
'yaxis': {'title': option}
,否则更新会将title以外的 yaxis 参数
重置
为其默认值(同样适用于 yaxis
type 
):

buttons = []
for option in options:
    buttons.append({
        'method': 'update',
        'label': option,
        'args': [{
            'y': [df[option]],
            'name': [option]
        }, {
            'yaxis.title': option
        }]
    })

scale_buttons = [{
    'method': 'relayout',
    'label': 'Linear Scale',
    'args': [{'yaxis.type': 'linear'}],
}, {
    'method': 'relayout',
    'label': 'Log Scale',   
    'args': [{'yaxis.type': 'log'}]
}]
© www.soinside.com 2019 - 2024. All rights reserved.