通过具有散景的Networkx节点属性为节点着色 [英] Color nodes by Networkx node attribute with Bokeh
问题描述
我正在尝试使用bokeh来创建交互式网络可视化.我了解如何向bokeh图添加属性数据,但是我不确定如何根据node属性分配填充颜色.
I'm trying to use bokeh to create an interactive network visualization. I understand how to add attribtue data to the bokeh graph, but I'm not sure how to assign a fill color based on the node attribute.
我一直在关注所有散景示例
I've been following all of the bokeh examples that I can find, but I can't seem to figure it out.
如何调整下面的代码以通过NetworkX节点属性为节点着色?
import networkx as nx
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
from bokeh.models import Circle, HoverTool, TapTool, BoxSelectTool
from bokeh.models.graphs import from_networkx
output_notebook()
# create a sample graph
G = nx.karate_club_graph()
# create the plot
plot = figure(x_range=(-1.1, 1.1), y_range=(-1.1, 1.1))
# add tools to the plot
plot.add_tools(HoverTool(tooltips=[("Name", "@name"),
("Club", "@club")]),
TapTool(),
BoxSelectTool())
# create bokeh graph
graph = from_networkx(G, nx.spring_layout, iterations=1000, scale=1, center=(0,0))
# add name to node data
graph.node_renderer.data_source.data['name'] = list(G.nodes())
# add club to node data
graph.node_renderer.data_source.data['club'] = [i[1]['club'] for i in G.nodes(data=True)]
# set node size
graph.node_renderer.glyph = Circle(size=10)
plot.renderers.append(graph)
show(plot)
推荐答案
问题有点模糊,因此我不确定下面的代码是否正是您所要的. ("node"属性是您复制到"names"列中的内容吗?我认为是这样...)但是无论如何,您都可以使用
The question is a little vague, so I am not sure the code below is exactly what you are asking for. (Is the "node" attribute what you copy in to the "names" column? I think so...) But regardless, you can use linear_cmap
to colormap the fill_color
according to any CDS column:
from bokeh.transform import linear_cmap
graph.node_renderer.glyph = Circle(
size=10,
fill_color=linear_cmap('name', 'Spectral8', min(G.nodes()), max(G.nodes()))
)
或者,如果该列是字符串因子,则也可以使用factor_cmap
.
Alternatively if the column is string factors, you could also use factor_cmap
.
这篇关于通过具有散景的Networkx节点属性为节点着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!