我是plotly的新手,我试图了解如何在plotly提供的updatemenus功能中设置args和args2的值(如文档Updatemenus 文档中所述)。
我尝试使用我在 stackoverflow 的答案中找到的代码片段(Plotly:How to Give different label names in a dropdown menu?)。
这是修改后的代码:
# 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()
Run Code Online (Sandbox Code Playgroud)
这给了我想要实现的目标,即当我第一次单击下拉菜单中的“正弦”时显示正弦图,并在再次单击“正弦”时清除该图(对于正弦也是如此)。
但是,我的问题是:
我在文档或其他任何地方找不到有关传递给 args 和 args2 的内容的任何信息。您能否提供任何文档参考?
最后,我什至不确定如何理解 args 的行为,尤其是第一项(带有“y”和“name”键的字典)和第二项,我所理解的是跟踪索引列表(?) 。在以下示例中,我将 Sine 和 Tan 按钮中的索引列表设置为 [2, 3]。因此,我希望通过单击“正弦”按钮来获得 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,}]
Run Code Online (Sandbox Code Playgroud)
您能否告诉我设置这些值以定位正确跟踪的正确方法?
先感谢您 !
ves*_*and 10
args您可以在args2 以下buttons位置找到信息updatemenus:
\n\nargs 父级:layout.updatemenus[].buttons[] 类型:列表 设置要传递到 on\nclick 中设置的 Plotly 方法的参数值
\nmethod。args2 父级:layout.updatemenus[].buttons[] 类型:列表 设置第二组,当处于活动状态时单击此按钮时,
\nargs这些参数值将传递给 Plotly 方法集。method使用\n它可以创建切换按钮。
诚然,找到这一点比完全理解更容易。你首先应该关注的是Use this to create toggle buttons。因为这正是我们args2的目的;提供一组在按钮 时设置的参数"not clicked"。所以buttons有几个参数:
args:单击按钮时会发生什么args2:未单击它\xe2\x80\x99s 时会发生什么(创建切换按钮)label:按钮/标签上写的是什么method:按钮是否更改绘图、布局或两者下面的完整代码片段将向您展示如何采用您一直在运行测试的示例,并生成一个在几个系列的显示sine和值之间切换的图形。tangent
如果您仔细查看下面的代码,您会发现我已完全按照文档中的描述进行设置:
\nargsand中的第一个字典args2处理对跟踪/数据所做的更改:
{\'y\': [y1, y1b],\n\'name\':[\'sin\', \'sin - 1\'],\n\'visible\': True},\nRun Code Online (Sandbox Code Playgroud)\n第二个字典处理对图形布局所做的更改:
\n{\'title\':\'Sine\'}\nRun Code Online (Sandbox Code Playgroud)\nmethod在下面的示例中,由于已设置为 ,因此可以编辑数据和布局update。
第三个元素是一个列表,确定应对哪些跟踪进行更改:
\n[0, 1]\nRun Code Online (Sandbox Code Playgroud)\n似乎是什么触发了 Plotly 问题:如何在下拉菜单中给出不同的标签名称?事实上,label的属性button无法通过 中的参数进行编辑arg,因此 的功能也是如此button,因为label和arg正是如此;按钮的参数,而不是figure object按钮控制的参数。如果将type属性从更改button为下拉列表,这一点会变得更加明显。现在您可以看到,当“未单击”按钮/未选择下拉选项时,您将丢失标签:
如果您想模拟按钮标签的切换功能,处理此问题的唯一方法是添加另一个按钮,因为 或 中的参数args无法args2“到达”按钮的特定属性。我真的希望有人愿意在这一点上证明我是错的,因为这些事情常常突然让我有点困惑。但我希望至少能够让你不再感到困惑。
# imports\nimport plotly.graph_objects as go\nimport numpy as np\n\n# data\nx = np.linspace(-np.pi, np.pi, 10)\n\ny1 = np.sin(x)\ny1b = y1-1\n\ny2 = np.tan(x)\ny2b = y2-1\n\n# plotly setup\nfig = go.Figure()\n\n# Add one ore more traces\nfig.add_traces(go.Scatter(x=x, y=y1,name=\'sin\', visible=True, showlegend=True))\nfig.add_traces(go.Scatter(x=x, y=y1b,name=\'sin - 1\', visible=True, showlegend=True))\n\nfig.add_traces(go.Scatter(x=x, y=y2,name=\'tan\', visible=False, showlegend=True))\nfig.add_traces(go.Scatter(x=x, y=y2b,name=\'tan - 1\', visible=False, showlegend=True))\nfig.update_layout({\'title\' : \'Sine\'})\n\n# construct menus\nupdatemenus = [{\n# \'active\':1,\n \'buttons\': [{\'method\': \'update\',\n \'label\': \'Toggle Sine / Tangent\',\n \'args\': [\n # 1. updates to the traces\n {\'y\': [y1, y1b],\n \'name\':[\'sin\', \'sin - 1\'],\n \'visible\': True}, \n \n # 2. updates to the layout\n {\'title\':\'Sine\'},\n \n # 3. which traces are affected \n [0, 1],\n \n ],\n \'args2\': [\n # 1. updates to the traces \n {\'y\': [y2, y2b],\n \'name\':[\'tan\', \'tan - 1\'],\n \'visible\':True},\n \n # 2. updates to the layout\n {\'title\':\'Tangent\'},\n \n # 3. which traces are affected\n [0, 1]\n ]\n },\n ],\n \'type\':\'buttons\',\n# \'type\':\'dropdown\',\n \'direction\': \'down\',\n \'showactive\': True,}]\n\n# update layout with buttons, and show the figure\nfig.update_layout(updatemenus=updatemenus)\nfig.show()\nRun Code Online (Sandbox Code Playgroud)\n