networkx - 如何更改使用 holoviews/bokeh 绘制的 networkx 图中的颜色?
问题描述
如何更改以下示例中各个节点的颜色?
%pylab inline
import pandas as pd
import networkx as nx
import holoviews as hv
hv.extension('bokeh')
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)])
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.spring_layout).redim.range(**padding)
解决方案
感谢 Philippjfr,这是一个很好的解决方案(使用当前开发版本的 holoviews),它使用节点属性进行着色:
%pylab inline
import pandas as pd
import networkx as nx
import holoviews as hv
hv.extension('bokeh')
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)])
attributes = {ndx: ndx%2 for ndx in ndxs}
nx.set_node_attributes(G, attributes, 'some_attribute')
%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.spring_layout)\
.redim.range(**padding)\
.options(color_index='some_attribute', cmap='Category10')
推荐阅读
- python - 碰到左边缘时如何使物体循环回右边缘?
- mysql - MySQL 模拟延迟以减少旧服务器上的冗余项目
- function - 如何执行这个反应查询突变?
- python - pyttsx3 字符串转数值
- python - 如何使用 | 更新 YML 文件 分隔符
- activerecord - 通过带有绑定参数的 Arel 往返活动记录查询
- python - 如何使用 python 访问/更改 JSON 值?
- c++ - 在 C++ 中使用 Windows 合成引擎的每像素透明窗口
- r - 函数调用中的回归不返回与函数外回归相同的输出
- visual-studio-code - jsdoc typedef 全局到本地,为什么不覆盖?