How to set colors for nodes in NetworkX?

后端 未结 2 862
南方客
南方客 2020-12-02 15:27

I created my graph, everything looks great so far, but I want to update color of my nodes after creation.

My goal is to visualize DFS, I will first show the initial

相关标签:
2条回答
  • 2020-12-02 16:09

    All you need is to specify a color map which maps a color to each node and send it to nx.draw function. To clarify, for a 20 node I want to color the first 10 in blue and the rest in green. The code will be as follows:

    G = nx.erdos_renyi_graph(20, 0.1)
    color_map = []
    for node in G:
        if node < 10:
            color_map.append('blue')
        else: 
            color_map.append('green')      
    nx.draw(G, node_color=color_map, with_labels=True)
    plt.show()
    

    You will find the graph in the attached image.

    0 讨论(0)
  • 2020-12-02 16:22

    Refer to node_color parameter:

    nx.draw_networkx_nodes(G, pos, node_size=200, node_color='#00b4d9')
    
    0 讨论(0)
提交回复
热议问题