使用按钮在Plotly / Python中隐藏注释

问题描述 投票:2回答:2

所以我用Plotly构建了一个网络图。输出很好。现在我想为网络的每个节点添加标签。为此,我使用了Plotly注释。 pos为网络中的每个节点保持{node_id:(x,y)}的位置; G是我的网络图。

layoutAnnotationList = []
for  k,p in pos.items():
    x = p[0]
    y = p[1]
    try:
        text = G.node[k]['hostname']
    except:
        text = k
    layoutAnnotationList.append( { 'x':x, 'y':y, 'text':text } )

在此之后,我将列表layoutAnnotationList添加到布局本身。

layout = { 'annotations': layoutAnnotationList } 

现在,我已经阅读了this关于如何使用relayout方法向布局添加按钮,但我真的不明白如何使这些按钮显示或隐藏注释。

我创建了一个layoutButtons列表,我必须在网页上显示它们,但我对它们的功能一无所知。

layout = { 'annotations': layoutAnnotationList, 'updatemenus':layoutButtons }

enter image description here

有关如何使用这些的任何提示?

谢谢!

python-3.x button label plotly networkx
2个回答
2
投票

因此,仔细阅读后,解决方案是在创建按钮时使用update方法。

layoutButtons = list([
                dict(type="buttons",
                     active=-1,
                     buttons=list([   
                        dict(label = 'Label:On',
                             method = 'update',
                             args = [{'visible': [True, True, True, True]},{'annotations':layoutAnnotationList}]
                             ),
                        dict(label = 'Label:Off',
                             method = 'update',
                             args = [{'visible':[True, True, False, False]},{'annotations':[]}]
                             ),
                            ]
                        )
                     )
                ]   
            )

我从here得到了这个想法。仍然不知道如何解释参数中的{'visible':[True, True, False, False]}字典,但它的工作原理。


1
投票

这个code在Slider Controls的绘图文档中表明{'visible':[True, False...]}字典被映射到数据列表,True意思是“显示我的数据列表的这个索引”和False“隐藏我的数据列表的这个索引”。换句话说,每个数据项应该有一个True / False。

在代码示例中,他们最初为visible中的每个项目设置dataFalse

data = [dict(
        visible = False, 
        line=dict(color='#00CED1', width=6),
        name = '𝜈 = '+str(step),
        x = np.arange(0,10,0.01),
        y = np.sin(step*np.arange(0,10,0.01))) for step in np.arange(0,5,0.1)]

然后他们将一个切换到True,因此在加载绘图时会显示一些数据:

data[10]['visible'] = True

接下来,在定义滑块的步骤时,它们将所有数据项'visible参数重置为False,然后将第i个切换为True

steps = []
for i in range(len(data)):
    step = dict(
        method = 'restyle',  
        args = ['visible', [False] * len(data)],
    )
    step['args'][1][i] = True # Toggle i'th trace to "visible"
    steps.append(step)

当在滑块中选择第i步时,第i个数据项将变为可见。

在这个问题的原始例子中,我们想要为visible按钮设置True的所有Label:On参数,并为False按钮设置所有Label:Off,所以你可以写:

'visible':[True] * len(pos)

'visible':[False] * len(pos)
© www.soinside.com 2019 - 2024. All rights reserved.