Skip to main content
 首页 » 编程设计

Python 如何通过 Bokeh 的 from_networkx 传递 networkx 布局参数

2024年09月07日6thcjp

我正在尝试使用带有 nx.spring_layout 参数的 bokeh 的 from_networkx 函数绘制带有 bokeh 的 networkx 图。我正在尝试尽可能多地在 Pandas 数据框中定义我的图表及其属性。我想初始化节点的位置;如何将这些位置传递给 spring_layout? (如果它被传递到 nx.spring_layout(...),我没有得到正确的格式。)任何方向都会有所帮助。

换句话说,对于 bokeh.from_networkx(G, networkx.spring_layout...),如何像不使用 bokeh 时那样将参数传递给 spring_layout(例如, networkx.spring_layout(G, dim=2, k=None, pos=None...))

简单的例子:

import networkx as nx 
import pandas as pd 
from bokeh.models import Plot, ColumnDataSource, Range1d, from_networkx, Circle,MultiLine 
from bokeh.io import show, output_file 
from bokeh.palettes import Viridis 
 
#define graph 
source = ['A', 'A', 'A','a','B','B','B','b'] 
target = ['a', 'B','b','b','a','b','A','a'] 
weight = [1,-1000,1,1,1,1, -1000, 1] 
df = pd.DataFrame([source,target,weight]) 
df = df.transpose() 
df.columns = ['source','target','weight'] 
G=nx.from_pandas_dataframe(df, 'source', 'target', ['weight']) 
 
#set node attributes 
node_color = {'A':Viridis[10][0], 'B':Viridis[10][9],'a':Viridis[10][4],'b':Viridis[10][4]} 
node_size = {'A':50, 'B':40,'a':10,'b':10} 
node_initial_pos = {'A':(-0.5,0), 'B':(0.5,0),'a':(0,0.25),'b':(0,-0.25)} 
nx.set_node_attributes(G, 'node_color', node_color) 
nx.set_node_attributes(G, 'node_size', node_size) 
nx.set_node_attributes(G, 'node_initial_pos', node_initial_pos) 
 
#source with node color, size and initial pos (perhaps ) 
source = ColumnDataSource(pd.DataFrame.from_dict({k:v for k,v in G.nodes(data=True)}, orient='index')) 
 
plot = Plot(plot_width=400, plot_height=400, 
            x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1)) 
 
graph_renderer = from_networkx(G, nx.spring_layout, scale=0.5, center=(0,0)) 
 
#style 
graph_renderer.node_renderer.data_source = source 
graph_renderer.node_renderer.glyph = Circle(fill_color = 'node_color',size = 'node_size', line_color = None) 
 
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5) 
 
 
plot.renderers.append(graph_renderer) 
output_file('test.html') 
 
show(plot) 

理想情况下,我不会为位置(或颜色或大小)创建字典,而是将它们作为传递给图形 G 的 df 的一部分输入。

Bokeh 0.12.14、Networkx 2.1、Python 3.6.3

请您参考如下方法:

编辑:您可以在 from_networkx(pos=) 函数中传递位置参数。

我用 networkx 2.2 和 python 3.5 测试了你的代码。稍作调整即可使用。

import networkx as nx 
import pandas as pd 
from bokeh.models import Plot, ColumnDataSource, Range1d, from_networkx, Circle,MultiLine 
from bokeh.io import show, output_file 
from bokeh.palettes import Viridis 
 
#define graph 
source = ['A', 'A', 'A','a','B','B','B','b'] 
target = ['a', 'B','b','b','a','b','A','a'] 
weight = [1,-1000,1,1,1,1, -1000, 1] 
df = pd.DataFrame([source,target,weight]) 
df = df.transpose() 
df.columns = ['source','target','weight'] 
G=nx.from_pandas_edgelist(df) # function signature changes 
 
#set node attributes 
node_color = {'A':Viridis[10][0], 'B':Viridis[10][9],'a':Viridis[10][4],'b':Viridis[10][4]} 
node_size = {'A':50, 'B':40,'a':10,'b':10} 
node_initial_pos = {'A':(-0.5,0), 'B':(0.5,0),'a':(0,0.25),'b':(0,-0.25)} 
nx.set_node_attributes(G,  node_color, name='node_color') # function signature changes 
nx.set_node_attributes(G,  node_size, name='node_size') # function signature changes 
nx.set_node_attributes(G,  node_initial_pos, name='node_initial_pos') # function signature changes 
 
#source with node color, size and initial pos (perhaps ) 
source = ColumnDataSource(pd.DataFrame.from_dict({k:v for k,v in G.nodes(data=True)}, orient='index')) 
 
plot = Plot(plot_width=400, plot_height=400, 
            x_range=Range1d(-1.1,1.1), y_range=Range1d(-1.1,1.1)) 
 
graph_renderer = from_networkx(G, nx.spring_layout, scale=0.5, center=(0,0), pos=node_initial_pos) 
 
#style 
graph_renderer.node_renderer.data_source = source 
graph_renderer.node_renderer.glyph = Circle(fill_color = 'node_color',size = 'node_size', line_color = None) 
 
graph_renderer.edge_renderer.glyph = MultiLine(line_color="#CCCCCC", line_alpha=0.8, line_width=5) 
 
 
plot.renderers.append(graph_renderer) 
output_file('test.html') 
 
show(plot) 

输出:没有位置参数

输出:带位置参数