如何更新plotly中多个记录道的数据?

2024-05-17 07:00:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组图,我想画在同一个图中。每个图对应一个go.Scatter对象。在plotly中,我尝试使用go.Layout.updatemenus参数构建一个下拉菜单,允许选择要绘制的图对。你知道吗

选择新对时,必须更新两个go.Scatter对象中的数据,以及go.Layout对象中的数据。我试着做了如下的事情,但没有成功。你知道吗

    trace1 = go.Scatter(
       x = x_data1,
       y = y_data1,
       name = name1,
       mode = 'markers',
       marker = dict(
           size = 2
       )
    )

    trace2 = go.Scatter(
       x = x_data1,
       y = y_data1,
       name = name2,
       mode = 'lines',
       line = dict(
           color = 'Red',
           width = 3
       )
    )

    buttons_list = []
    for key in dictio.keys():
        buttons_list.append(dict(
                            label=key,
                            method='update',
                            args=[{"x": dictio[key].xdata1,
                                   "y": dictio[key].ydata1,
                                   "name": key},
                                  {"x":dictio[key].xdata2,
                                   "y":dictio[key].ydata2},
                                  {"annotations": get_annotations(key)}
                            ]
                        )
        )
    updatemenus = list([
        dict(
            active=0,
            buttons=buttons_list,
            direction='down',
            x = 0.05,
            xanchor='left',
            y = 1,
            yanchor='top'
        )         
    ])

    layout = go.Layout(
        updatemenus = updatemenus,
        annotations = annotations,
    )

    fig = go.Figure(data=data, layout=layout)
    plot(fig, auto_open=True)

Tags: 数据对象keynamegodictlistannotations