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

时间:2018-03-01 22:46:31

标签: python networkx bokeh

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

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

简单示例:

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)

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

Bokeh 0.12.14,Networkx 2.1,Python 3.6.3

1 个答案:

答案 0 :(得分:1)

编辑:您可以传递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)

输出:没有位置参数

enter image description here

输出:带位置参数

enter image description here