Search code examples
pythongraphdata-sciencenetworkx

Using networkx to create node labels from an array


I have some nodes that are being generated from a numpy adjacency matrix. I also have an array of labels for said nodes already where node 0 in the matrix corresponds to label 0 in the array. What is the easiest way to apply those labels to the nodes in the graph?


Solution

  • You can specify the labels while drawing but you must first convert the array into a dictionary where the key value is the node number and the value is the label. After whuch you can specify labels with the label argument like so:

    nx.draw(G, labels=labelDict)