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

How to display buttons in pyvis visualization of NetworkX Graph?

我正在尝试修改此功能以正确显示交互按钮。我正在使用 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')

问题是您在实例化可视化时将 heightwidth 都设置为 '100%':

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

由于网络设置为占用浏览器 window 中的所有 space,所以按钮根本不会在 window 中呈现。根据您希望按钮出现的位置,我建议将 height 设置为固定像素值(例如,height='800px')或将 width 更改为可用 space(比如说,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')