如何使用具有相同位置的节点和边缘的holoviews / bokeh绘制networkx图?

时间:2018-05-20 00:34:58

标签: networkx bokeh holoviews

我想使用holoview和bokeh绘制使用networkx生成的图形。 Networkx运行图的优化,它似乎是绘图的一部分。当我将图形提供给全息图时,图形看起来非常不同。我不确定会发生什么。我猜,边缘权重未正确传​​输,或者未执行优化。 (jupyter笔记本代码)。

%pylab inline

import pandas as pd
import networkx as nx
import holoviews as hv

hv.extension('bokeh')
np.random.seed(111)
G=nx.Graph()
ndxs = [1,2,3,4]
G.add_nodes_from(ndxs)
G.add_weighted_edges_from([(1,2,0), (1,3,1), 
                           (1,4,-1), (2,4,1),
                           (2,3,-1), (3,4,10)])
nx.draw(G)
# or you set the random_state for the spring layout
# to make sure the figure is reproducible
nx.draw(G, nx.spring_layout(G, random_state=100))

asdf

hv.extension('bokeh')
%opts Graph [width=400 height=400]
padding = dict(x=(-1.1, 1.1), y=(-1.1, 1.1))
hv.Graph.from_networkx(G, nx.layout.circular_layout).redim.range(**padding)

enter image description here

在使用holoviews / bokeh进行绘图时,如何保存节点的位置?

2 个答案:

答案 0 :(得分:1)

上面的示例使用nx.circular_layout如果更改为nx.spring_layout,则执行相同的优化并且图表看起来类似。我不知道如何设置random_state

enter image description here

答案 1 :(得分:0)

您可以仅将位置保存为变量,然后将其用于nx和hv。 例如:

position = nx.spring_layout(G, scale=2)
nx.draw(G,position)
hv.extension('bokeh')
%opts Graph [width=400 height=400]
padding = dict(x=(-1.1, 1.1), y=(-1.1, 1.1))
hv.Graph.from_networkx(G, position).redim.range(**padding)

否则,使用随机种子位置的事实将使得不可能连续两次完全相同地绘制图形。