I am trying to draw a graph in NetworkX. The network connects 30 nodes and about 130 edges. Nodes are set using a matrix with weight
A = [
[0, 1.51, 0, 1.71, 0],
[0, 0, 2.11, 1.81, 2.31],
[0, 0, 0, 1.31, 1.41],
[0, 0, 0, 0, 1.11],
[0, 0, 0, 0, 0]]
How to assign the names of the nodes to the matrix from list ("A1", "K2", ... "Z30")?
So far, I have only been possible to assign names to numbers.
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
A = [[0, 1.51, 0, 1.71, 0],
[0, 0, 2.11, 1.81, 2.31],
[0, 0, 0, 1.31, 1.41],
[0, 0, 0, 0, 1.11],
[0, 0, 0, 0, 0]]
G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
labels = nx.get_edge_attributes(G, "weight")
lab_node = dict((i, str(i)*3) for i in range(5))
nx.draw(G, layout)
nx.draw_networkx_nodes(G, layout, node_size=500)
nx.draw_networkx_edge_labels(G, layout, edge_labels=labels)
nx.draw_networkx_labels(G, layout, labels=lab_node, font_size=10, font_family='sans-serif')
plt.show()
