【问题标题】:How to set the values of args and args2 in Plotly's buttons in updatemenus?如何在更新菜单的 Plotly 按钮中设置 args 和 args2 的值?
【发布时间】:2021-08-23 15:06:00
【问题描述】:

我是 plotly 的新手,我正在尝试了解如何在 plotly 提供的更新菜单功能中设置 args 和 args2 的值(如文档 Updatemenus documentation 中所述)。


我尝试使用在 stackoverflow (Plotly: How to give different label names in a dropdown menu?) 的答案中找到的代码 sn-p。

这是修改后的代码:

# imports
import plotly.graph_objects as go
import numpy as np

# data
x = np.linspace(-np.pi, np.pi, 100)

y1 = np.sin(x)
y1b = y1-1

y2 = np.tan(x)
y2b = y2-1

# plotly setup
fig = go.Figure()

# Add one ore more traces
fig.add_traces(go.Scatter(x=x, y=y1,name='sin', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y1b,name='sin - 1', visible=False, showlegend=True))

fig.add_traces(go.Scatter(x=x, y=y2,name='tan', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y2b,name='tan - 1', visible=False, showlegend=True))

# construct menus
updatemenus = [{'active':-1,'buttons': [{'method': 'restyle',
                             'label': 'Sine',
                             'args': [{'y': [y1, y1b, y2, y2b],
                                       'label':'Sine',
                                       'name':['sin', 'sin - 1', 'tan', 'tan - 1'],
                                       'visible': True}, [0, 1]],
                             'args2': [
                                       {'visible': False}, ]
                              },
                            
                            {'method': 'restyle',
                             'label': 'Tan',
                             'args': [{'y': [y2, y2b, y1, y1b],
                                       'name':['tan', 'tan - 1', 'sin', 'sin - 1'],
                                       'visible':True}, [0, 1]],
                             'args2': [{
                                       'visible':False},  
                                      ],
                             }
                           ],
                'direction': 'down',
                'showactive': True,}]

# update layout with buttons, and show the figure
fig.update_layout(updatemenus=updatemenus)
fig.show()

这给了我想要实现的目标,即在我第一次单击下拉菜单中的 Sine 时显示正弦图,并在我再次单击 Sine 时清除该图(对于 Tan 也是如此)。

但是,我的问题是:

  • 我在文档或其他任何地方都找不到关于传递给 args 和 args2 的内容的任何信息。您能否提供任何文档参考?

  • 最后,我什至不知道如何理解 args 的行为,尤其是第一项(带有 'y' 和 'name' 键的字典)和我理解的第二项是跟踪索引列表(?)。在以下示例中,我在 Sine 和 Tan 按钮中将索引列表设置为 [2, 3]。因此,我希望通过单击 Sine 按钮来获得 Tan 图,反之亦然。但情节并没有改变。


    # construct menus
    updatemenus = [{'active':-1,'buttons': [{'method': 'restyle',
                                 'label': 'Sine',
                                 'args': [{'y': [y1, y1b, y2, y2b],
                                           'label':'Sine',
                                           'name':['sin', 'sin - 1', 'tan', 'tan - 1'],
                                           'visible': True}, [2, 3]],
                                 'args2': [
                                           {'visible': False}, ]
                                  },
    
                                {'method': 'restyle',
                                 'label': 'Tan',
                                 'args': [{'y': [y2, y2b, y1, y1b],
                                           'name':['tan', 'tan - 1', 'sin', 'sin - 1'],
                                           'visible':True}, [2, 3]],
                                 'args2': [{
                                           'visible':False},  
                                          ],
                                 }
                               ],
                    'direction': 'down',
                    'showactive': True,}]

您能否告诉我设置这些值以定位正确跟踪的正确方法?

提前谢谢你!

【问题讨论】:

  • 您可以将任何布局参数传递给 args 参数:title、yaxis、hovermode、autosize 等。
  • 感谢您的回答!我尝试了布局参数,但这不起作用。我可以弄清楚 args 接受跟踪参数(我们传递给 fig.update_trace() 的那些)。见link。不过,我不明白为什么情节没有用正确的跟踪更新。这部分有什么帮助吗?再次感谢
  • @Downforu 我的解释对你有意义吗?
  • 非常感谢您的详细解答!现在完全有道理了!

标签: python plotly


【解决方案1】:

1。文档

您可以在buttons 下找到argsargs2 的信息updatemenus

args 父级:layout.updatemenus[].buttons[] 类型:list 设置 要传递给 method 中设置的 Plotly 方法的参数值 点击。

args2 父级:layout.updatemenus[].buttons[] 类型:list 设置第二组 args,这些参数值被传递给 Plotly 方法集 在method 在活动状态下单击此按钮时。采用 用于创建切换按钮。

诚然,这比完全理解更容易找到。你应该首先关注的是Use this to create toggle buttons。因为这正是args2 的用途;提供一组在按钮为"not clicked" 时设置的参数。所以buttons有几个参数:

  1. args: 点击按钮会发生什么
  2. args2: 未点击时会发生什么(创建切换按钮)
  3. label:按钮/标签上写的是什么
  4. method: 按钮是改变情节、布局还是两者兼而有之

2。设置的正确方法

下面的完整 sn-p 将向您展示如何以您一直在运行测试的示例为例,并生成一个在显示 sinetangent 几个系列值之间切换的图形。

Plot1 - 点击按钮获取:

Plot 2 - 再次点击按钮得到:

如果您仔细查看下面的代码,您会发现我已经完全按照文档中的描述进行了设置:

argsargs2 中的第一个 dict 处理对跟踪/数据所做的更改:

{'y': [y1, y1b],
'name':['sin', 'sin - 1'],
'visible': True},

第二个字典处理对图形布局所做的更改:

{'title':'Sine'}

在下面的示例中,由于 method 已设置为 update,因此可以同时编辑数据和布局。

第三个元素,一个列表,确定应该对哪些跟踪进行更改:

[0, 1]

还有一件更重要的事情

似乎引发Plotly: How to give different label names in a dropdown menu? 问题的原因是buttonlabel 属性无法通过arg 中的参数进行编辑,因此button 的功能也无法编辑,因为labelarg 都是这样;按钮的参数,而不是按钮控制的 figure object。如果您将type 属性从button 更改为下拉菜单,这会变得更加明显。现在您可以看到,当按钮“未点击”/下拉选项未选中时,您将丢失标签:

情节3:

如果您想模拟按钮标签的切换功能,则处理此问题的唯一方法是添加另一个按钮,因为 argsargs2 中的参数无法“达到”该特定属性的按钮。我真的希望有人愿意在这件事上证明我错了,因为这些事情常常让我突然有点困惑。但我希望我已经能够让thins 至少让你感到困惑。

完整代码:

# imports
import plotly.graph_objects as go
import numpy as np

# data
x = np.linspace(-np.pi, np.pi, 10)

y1 = np.sin(x)
y1b = y1-1

y2 = np.tan(x)
y2b = y2-1

# plotly setup
fig = go.Figure()

# Add one ore more traces
fig.add_traces(go.Scatter(x=x, y=y1,name='sin', visible=True, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y1b,name='sin - 1', visible=True, showlegend=True))

fig.add_traces(go.Scatter(x=x, y=y2,name='tan', visible=False, showlegend=True))
fig.add_traces(go.Scatter(x=x, y=y2b,name='tan - 1', visible=False, showlegend=True))
fig.update_layout({'title' : 'Sine'})

# construct menus
updatemenus = [{
#                 'active':1,
                'buttons': [{'method': 'update',
                             'label': 'Toggle Sine / Tangent',
                             'args': [
                                      # 1. updates to the traces
                                      {'y': [y1, y1b],
                                       'name':['sin', 'sin - 1'],
                                       'visible': True}, 
                                      
                                      # 2. updates to the layout
                                      {'title':'Sine'},
                                      
                                      # 3. which traces are affected 
                                      [0, 1],
                                      
                                      ],
                             'args2': [
                                       # 1. updates to the traces  
                                       {'y': [y2, y2b],
                                       'name':['tan', 'tan - 1'],
                                       'visible':True},
                                      
                                       # 2. updates to the layout
                                       {'title':'Tangent'},
                                       
                                       # 3. which traces are affected
                                       [0, 1]
                                      ]
                              },
                            ],
                'type':'buttons',
#                 'type':'dropdown',
                'direction': 'down',
                'showactive': True,}]

# update layout with buttons, and show the figure
fig.update_layout(updatemenus=updatemenus)
fig.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多