如何在 NetworkX 图的 pyvis 可视化中显示按钮?

4

我试图修改这个函数以便正确显示交互式按钮。我使用pyvis来可视化在Networkx上创建的图。尽管包括N.show_buttons(filter_=True),但按钮不会出现在相应的HTML文件中。此外,我如何为生成的HTML页面添加标题?

def visualize(identity_id,html_name):
    #create subgraph using the provided identity_id
    classx = [n for n in G.nodes() if G.nodes[n]['modularity'] == 
              G.nodes[identity_id]['modularity']]
    SG = G.subgraph(classx)

    #instantiate the Network object
    N = Network(height='100%', width='100%', bgcolor='#ffffff', 
                font_color='black',notebook = True, directed=False)

    #this line effects the physics of the html File
    N.barnes_hut(spring_strength=0.006)

    #Change colors of nodes and edges
    for n in SG:
        if (SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE'):  # assign color to nodes based on cust status
            color = 'green'
            shape = 'square'
        if (SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='CLOSED'):  # assign color to nodes based on cust status
            color = 'red'
            shape = 'square'   
        elif SG.nodes[n]['category']=='app':# assign shape to nodes based on cust versus app
            color = 'blue'
            shape = 'triangle'
        N.add_node(n, label=n, color=color,shape = shape)

    for e in SG.edges:
        if e in SG.edges:  # add the edges to the graph
            color = 'black'
            width = 2
        N.add_edge(e[0],e[1],color=color, width=width)

    N.show_buttons(filter_=True)
    #generate html file
    N.show(f'subgraph_{html_name}.html')

欢迎来到Stack Overflow,这是一个很棒的第一次提问!: ^) 通常情况下,如果您在代码中包含最少量的数据以重现问题,您将更可靠(并更快速)地获得响应。请参见下面的答案以获取示例。再次感谢您的出色提问。欢迎继续发布! - Frodnar
1个回答

4
问题在于您在实例化可视化时将 heightwidth 都设置为 '100%'
N = Network(height='100%', width='100%', bgcolor='#ffffff', 
            font_color='black',notebook = True, directed=False)

由于网络设置为占用浏览器窗口的所有空间,因此按钮根本不会在窗口中呈现。根据您希望按钮出现的位置,我建议将height设置为固定像素值(例如height='800px'),或者将width更改为可用空间的较小百分比(例如 75%)。

我编造了虚拟数据使您的代码能够正常工作,但以下是完整的可复制代码,供其他读者尝试重新创建此问题。

import networkx as nx
from pyvis.network import Network


def visualize(identity_id,html_name):
    # Generate synthetic data
    G = nx.complete_bipartite_graph(3, 4)
    nx.set_node_attributes(G, 3, 'modularity')
    nx.set_node_attributes(G, 'cust', 'category')
    nx.set_node_attributes(G, 'ACITVE', 'status')

    #create subgraph using the provided identity_id    
    classx = [n for n in G.nodes() if G.nodes[n]['modularity'] == 
              G.nodes[identity_id]['modularity']]
    SG = G.subgraph(classx)

    #instantiate the Network object
    N = Network(height='800px', width='100%', bgcolor='#ffffff', # Changed height
                font_color='black',notebook = True, directed=False)

    #this line effects the physics of the html File
    N.barnes_hut(spring_strength=0.006)

    #Change colors of nodes and edges
    for n in SG:
        if (SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='ACTIVE'):  # assign color to nodes based on cust status
            color = 'green'
            shape = 'square'
        if (SG.nodes[n]['category']=='cust') and (SG.nodes[n]['status']=='CLOSED'):  # assign color to nodes based on cust status
            color = 'red'
            shape = 'square'   
        elif SG.nodes[n]['category']=='app':# assign shape to nodes based on cust versus app
            color = 'blue'
            shape = 'triangle'
        else:
            color = 'blue'
            shape = 'triangle'
        N.add_node(n, label=n, color=color,shape = shape)

    for e in SG.edges:
        if e in SG.edges:  # add the edges to the graph
            color = 'black'
            width = 2
        N.add_edge(e[0],e[1],color=color, width=width)

    N.show_buttons(filter_=True)
    #generate html file
    N.show(f'subgraph_{html_name}.html')

visualize(3, 'test_name')

太棒了!非常感谢,这个方法非常有效! - AAA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接