本文介绍了如何更改用holoviews/bokeh绘制的networkx图的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下示例中,如何更改各个节点的颜色?
How can I change the color of individual nodes in the following example?
%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的当前开发版本),该解决方案使用节点属性进行着色:
Thanks to Philippjfr, here is a nice solution (using the current development version of holoviews) that uses node attributes for coloring:
%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')
这篇关于如何更改用holoviews/bokeh绘制的networkx图的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!