首页 > 解决方案 > 如何在 plotly 中更新来自多个跟踪的数据?

问题描述

我有一组图表,我想在同一个图中绘制。每个图对应一个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)

标签: pythonpython-3.xplotly

解决方案


推荐阅读