The following code generates adjacency matrix using nx.grid_2d
. I use an array P
to color the nodes but there is an error while generating colorbar. I want the colorbar to display the range of node colors.
import numpy as np
import networkx as nx
import matplotlib
import matplotlib.pyplot as plt
G = nx.grid_2d_graph(3,3)
new_nodes = {e: n for n, e in enumerate(G.nodes, start=1)}
new_edges = [(new_nodes[e1], new_nodes[e2]) for e1, e2 in G.edges]
G = nx.Graph()
G.add_edges_from(new_edges)
#nx.draw(G, with_labels=True)
A1 = nx.adjacency_matrix(G)
A=A1.toarray()
print([A])
fig, ax = plt.subplots(1, 1)
P=np.array([1,2,5,4,5,6,7,2,10])
D=nx.draw(G, with_labels=True, node_color=[P], node_size=1000, cmap='Blues')
fig.colorbar(D)
plt.show()
The error is
in <module>
fig.colorbar(D)
File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\figure.py", line 1176, in colorbar
cb = cbar.Colorbar(cax, mappable, **cb_kw)
File "C:\Users\USER\anaconda3\lib\site-packages\matplotlib\colorbar.py", line 1171, in __init__
if mappable.get_array() is not None:
AttributeError: 'NoneType' object has no attribute 'get_array'