问题描述
我正在尝试使用NetworkX和Bokeh构建网络图.我正在使用NetworkX from_pandas_edgelist
函数为图形添加数据.我想根据初始数据输入中的列为图的节点着色.
I am attempting to build a network graph using NetworkX and Bokeh. I am using the NetworkX from_pandas_edgelist
function to add data for the graph. I would like to color the node of the graph based on the column in the initial data input.
relation
数据帧如下:
company client
Google AT&T
Google Cisco
Amazon Facebook
Amazon Snap
Amazon Microsoft
Apple Intel
Apple IBM
Apple Visa
上面的代码片段只是DataFrame的一部分.
The above snippet is only a portion of the DataFrame.
我希望company
中的所有节点以不同的颜色返回client
.
I would like all of the nodes from company
to return in a different color to client
.
下面的代码生成一个网络图,其中所有节点都是相同的颜色.
The code below produces a network graph where all nodes are the same color.
G=nx.from_pandas_edgelist(relation, 'company', 'client')
# Show with Bokeh
plot = Plot(plot_width=1000, plot_height=800,
x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Company - Client Network"
node_hover_tool = HoverTool(tooltips=[("Company Name", "@index")])
plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())
graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.glyph = Circle(size=20)
graph_renderer.edge_renderer.glyph = MultiLine(line_color="red", line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)
output_file("interactive_graphs.html")
show(plot)
任何人都可以提供的任何帮助将不胜感激.
Any assistance anyone could provide would be greatly appreciated.
推荐答案
在旧版编辑之后:
不能提供太多的上下文信息,因为我对bokeh并不十分熟悉,但是看起来您可以使用与我最初的方法类似的方法,而不必通过"color_map"来完成绘制功能,因此必须粘贴此处graph_renderer.node_renderer.data_source.data['colors']
中的数据无论如何,这似乎可以解决问题,祝你好运.老兄.
Can't give too much context as I am not super familiar with bokeh, but looks like you can use a similar approach to what I did initially just instead of passing the "color_map" do your draw function you have to stick your data in here graph_renderer.node_renderer.data_source.data['colors']
Anyway this seems to do the job, Good luck dude.
relation = pd.DataFrame({
"company":["Google", "Google", "Amazon", "Amazon", "Amazon",
"Apple", "Apple", "Apple"],
"client":["AT&T", "Cisco", "Facebook", "Snap", "Microsoft",
"Intel", "IBM", "Visa"]})
G=nx.from_pandas_edgelist(relation, 'company', 'client')
colors = []
for node in G:
if node in relation["client"].values:
colors.append("blue")
else: colors.append("green")
plot = Plot(plot_width=1000, plot_height=800,
x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1))
plot.title.text = "Company - Client Network"
node_hover_tool = HoverTool(tooltips=[("Company Name", "@index")])
plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool())
graph_renderer = from_networkx(G, nx.spring_layout, scale=1, center=(0, 0))
graph_renderer.node_renderer.data_source.data['colors'] = colors
graph_renderer.node_renderer.glyph = Circle(size=20, fill_color='colors')
graph_renderer.edge_renderer.glyph = MultiLine(line_color="red", line_alpha=0.8, line_width=1)
plot.renderers.append(graph_renderer)
output_file("boo.html")
show(plot)
这篇关于根据列名称为NetworkX节点分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!