gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:31:35 25 4
gpt4 key购买 nike

我正在尝试使用带有 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)

输出:没有位置参数

enter image description here

输出:带位置参数

enter image description here

关于Python 如何通过 Bokeh 的 from_networkx 传递 networkx 布局参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49059916/

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