密谋:如何使用updatemenus更新一条特定的迹线? [英] Plotly: How to update one specific trace using updatemenus?

查看:256
本文介绍了密谋:如何使用updatemenus更新一条特定的迹线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对Plotly的后续问题:

This is a follow-up question to Plotly: Plotly: How do the buttons for the update menus really work?

请考虑下面的代码段生成的以下图形:

Consider the following plotly figure produced by the code snippet below:

情节:

代码:

# imports
import plotly.graph_objs as go
import pandas as pd
import numpy as np

# data
df1 = pd.DataFrame({'index': ['1','2','3'], 'A': [10,10,12], 'B': [11,11,11]})
df2 = pd.DataFrame({'index': ['1','2','3'], 'A': [10,10,10], 'B': [11,11,12]})

# plotly figure setup
fig=go.Figure()
fig.add_trace(go.Scatter(x=df1['index'], y=df1['A'], mode='lines'))
fig.add_trace(go.Scatter(x=df1['index'], y=df1['B'], mode='lines'))

f=fig.to_dict()
#fig.show()

buttons=list([dict(args=[{'y':[df1['A'],df1['B']]}],

                   label="df1",
                   method="restyle"
                ),
                dict(args=[{'y':[df2['A'], df2['B']]}],

                    label="df2",
                    method="restyle"
                )
            ])

fig.update_layout(
    updatemenus=[
        go.layout.Updatemenu(
            buttons=buttons,
            direction="down",
            pad={"r": 10, "t": 10},
            showactive=True,
            x=-0.25,
            xanchor="left",
            y=1,
            yanchor="top"
        ),
    ]
)

fig.show()

在上面的代码段中,我正在使用按钮和dict(args=[{'y':[df2['A'], df2['B']]}]更新'y'值.这样会为随该图指定的 迹线分配新值,例如fig-to_dict:

In the snippet above, I'm updating the 'y' values using buttons and dict(args=[{'y':[df2['A'], df2['B']]}]. This assigns new values to both traces specified withing the figure like this fig-to_dict:

'data': [{'mode': 'lines',
   'x': array(['1', '2', '3'], dtype=object),
   'y': array([10, 10, 12], dtype=int64),
   'type': 'scatter'},
  {'mode': 'lines',
   'x': array(['1', '2', '3'], dtype=object),
   'y': array([11, 11, 11], dtype=int64),
   'type': 'scatter'}]

由于我已经将列表[df2['A'], df2['B']]分配给了'y',因此可以很清楚地知道我打算更新上面代码段中的两个'y'实例.但是在按钮和更新菜单的上下文中,有一种方法可以指定 哪个 'y'进行更新(换句话说:什么特定的迹线或行). 如果我仅分配一个引用(在这种情况下为数组或熊猫数据框),则两条迹线将显示相同的值.因此,请更改以下部分:

Since I've assigned the list [df2['A'], df2['B']] to 'y', plotly knows that I intend to update both instances of 'y' in the snippet above. But within the context of buttons and update menus, is there a way I can specify which 'y' to update (in other words: what specific trace or line). If I assign only one reference (array or pandas dataframe in this case), both traces will show the same values. So changing the following part:

 args=[{'y':[df2['A'], df2['B']]}]

...与此:

args=[{'y':[df2['A']]}]

...单击df2时将产生以下图:

... will produce the following plot upon clicking df2:

我真的想保持所有未指定的'y'和跟踪保持不变.

And I'd really like to keep all unspecified 'y' and traces unchanged.

谢谢您的任何建议!

推荐答案

在要传递给args的每个button列表中,可以在dict之后添加一个整数以指示要更新的跟踪.例如,以下内容将仅更新第一条跟踪(即位于index = 0的跟踪)

In the list you are passing to args for each button, you can add an integer after the dict to indicate which trace you want to update. For example the following will update the first trace only (i.e. the one at index=0)

buttons=list([dict(args=[{'y':[df1['A'],df1['B']]}, [0]], # note the `, [0]` here!

                   label="df1",
                   method="restyle"
                ),
                dict(args=[{'y':[df2['A'], df2['B']]}, [0], # note the `, [0]` here!

                    label="df2",
                    method="restyle"
                )
            ])

这篇关于密谋:如何使用updatemenus更新一条特定的迹线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆