NetwokX,Barabasi-Albert模型

时间:2017-12-16 09:40:03

标签: python networkx

我对NetworkX Barabasi-Albert图的图有问题;我希望节点通过径向渐变着色。现在他们是石灰。

nx.draw_networkx_nodes(G, 
                       pos=p,
                       node_size=5, 
                       alpha=0.5,        
                       node_color='xkcd:lime')

如何将它们更改为径向颜色渐变?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用matplotlib库中的cmap。

以下是您可以使用的颜色贴图的名称:https://matplotlib.org/examples/color/colormaps_reference.html

然后你的代码看起来像这样:

import matplotlib.pyplot as plt


cmap = plt.get_cmap('viridis')

sequence_value=[0,1,2,8,4,17] #some values you want your nodes to be colored by (if you just simply want them in different colors you could also write

# sequence_value=range(0,14) # in this case you have 14 nodes (this list must always be as long as your len(G.nodes).

nx.draw_networkx_nodes(G, 
                   pos=p,
                   node_size=5, 
                   alpha=0.5,        
                   node_color=sequence_value, 
                   cmap=cmap)