Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何通过Bokeh';s来自网络X_Python_Networkx_Bokeh - Fatal编程技术网

Python如何通过Bokeh';s来自网络X

Python如何通过Bokeh';s来自网络X,python,networkx,bokeh,Python,Networkx,Bokeh,我正在尝试使用bokeh的from_networkx函数和nx.spring_布局参数绘制一个带有bokeh的networkx图。我试图尽可能多地在一个数据帧中定义我的图形及其属性。我想初始化节点的位置;如何将这些位置传递到spring_布局?(如果它被传递到nx.spring_布局中(…),我没有得到正确的格式。)任何方向都会有帮助 换句话说,对于bokeh.from_networkx(G,networkx.spring_布局…),如何像不使用bokeh时一样将参数传递给spring_布局(例

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

换句话说,对于bokeh.from_networkx(G,networkx.spring_布局…),如何像不使用bokeh时一样将参数传递给spring_布局(例如。, networkx.spring\u布局(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,Python3.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)
输出:不带位置参数

输出:带位置参数


我可以得到这样的图表。我不明白如何将position之类的参数传递到spring_布局中。我将对这个问题添加更多的描述来说明这一点。from_networkx函数描述:任何关键字参数都将传递给布局函数,在您的答案中更改这一行,我可以接受它:graph_renderer=from_networkx(G,nx.spring_布局,比例=0.5,中心=(0,0),pos=node_initial_pos)我现在明白你的问题了。很高兴我能帮你自己解决这个问题。