gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:38:14 25 4
gpt4 key购买 nike

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

由于网络设置为占用浏览器窗口中的所有空间,因此按钮不会在窗口中呈现。根据您希望按钮出现的位置,我建议将 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')

关于python - 如何在 NetworkX Graph 的 pyvis 可视化中显示按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69036579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com