matplotlib - How to set the nodes color with a given graph -


i using networkx , matplotlib

now want set color of nodes,and read graph text file

g=nx.read_edgelist("edge.txt") nx.draw(g) plt.show() 

here edge file of example

0 1 0 2 3 4 

here did,and failed

import networkx nx import matplotlib.pyplot plt g = nx.read_edgelist("edge.txt") pos = nx.spring_layout(g) nx.draw_networkx_nodes(g,pos,node_list=[0,1,2],node_color='b') nx.draw_networkx_nodes(g,pos,node_list=[3,4],node_color='r') plt.show() 

the result lot of blue nodes without edges

so if want set nodelista=[0,1,2] blue, nodelistb=[3,4] red

how can that?

draw nodes explicitly calling top-level function, draw_networkx_nodes

and pass in node list value parameter node_list, , value parameter node_color, so

nx.draw_network_nodes(g, pos, node_list=nodelista, node_color="#5072a7") 

the argument pos python dictionary keys nodes of graph , values x, y positions; easy to supply pos pass graph object spring_layout return dictionary.

pos = nx.spring_layout(g) 

alternatively, can pass in dictionary directly, e.g.,

pos = {        0:(2,2),        1:(3,5),        2:(1,2),        3:(5,5),        4:(7,4) } 

the cause of code in op execute call read_edgelist; in particular, file passed in incorrectly formatted.

here's how check , how fix it:

g = nx.path_graph(5)  df = "/path/to/my/graphinit.edgelist"  nx.write_edgelist(g, df)    # save formatted edgelist file  g = nx.read_edgelist(df)    # read file in 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -