I am generating a graph with nodes
and edges
. I want the array Edges
to take values in the array Weights
such that Edges[0]=Weights[0],Edges[1]=Weights[1], Edges[2]=Weights[2]
and display different colors as shown in the expected output.
import networkx as nx
import numpy as np
N = 1
def pos():
x, y = 1, N 3 - 1
for _ in range(2 * N * (N 1)):
yield (x, y)
y -= (x 2) // (N 3)
x = (x 2) % (N 3)
G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N 1)):
G.add_node(u 1, pos=next(it_pos))
if u % (2 * N 1) < N:
for v in (u - 2 * N - 1, u - N - 1, u - N):
if G.has_node(v 1):
G.add_edge(u 1, v 1)
elif u % (2 * N 1) == N:
G.add_edge(u 1, u - N 1)
elif u % (2 * N 1) < 2 * N:
for v in (u - 1, u - N - 1, u - N):
G.add_edge(u 1, v 1)
else:
for v in (u - 1, u - N - 1):
G.add_edge(u 1, v 1)
nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold')
A=nx.adjacency_matrix(G).todense()
A = np.squeeze(np.asarray(A))
print([A])
##################################################
Edges=np.array([[1,2],[1,3],[1,4]])
Weights=np.array([[1.7],[2.9],[8.6]])
##################################################
The current output is
The expected output is
CodePudding user response:
Try this:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import ScalarMappable
N = 1
def pos():
x, y = 1, N 3 - 1
for _ in range(2 * N * (N 1)):
yield (x, y)
y -= (x 2) // (N 3)
x = (x 2) % (N 3)
G = nx.Graph()
it_pos = pos()
for u in range(2 * N * (N 1)):
G.add_node(u 1, pos=next(it_pos))
if u % (2 * N 1) < N:
for v in (u - 2 * N - 1, u - N - 1, u - N):
if G.has_node(v 1):
G.add_edge(u 1, v 1)
elif u % (2 * N 1) == N:
G.add_edge(u 1, u - N 1)
elif u % (2 * N 1) < 2 * N:
for v in (u - 1, u - N - 1, u - N):
G.add_edge(u 1, v 1)
else:
for v in (u - 1, u - N - 1):
G.add_edge(u 1, v 1)
nx.draw(G, nx.get_node_attributes(G, 'pos'), with_labels=True, font_weight='bold')
Edges=np.array([[1,2],[1,3],[1,4]])
Weights=np.array([[1.7],[2.9],[8.6]])
flat_weights = Weights.flatten()
weights_normalized = [x / max(flat_weights) for x in flat_weights]
edge_weight_map = dict(zip([tuple(e) for e in Edges.tolist()],weights_normalized))
my_cmap = plt.cm.get_cmap('Oranges')
colors = my_cmap([edge_weight_map.get(tuple(e), 0) for e in Edges.tolist()])
pos = nx.get_node_attributes(G, 'pos')
sm = ScalarMappable(cmap=my_cmap, norm=plt.Normalize(0,max(flat_weights)))
nx.draw_networkx_edges(G, pos, edge_color=colors,
edgelist=[tuple(e) for e in Edges.tolist()],
width=5);
plt.colorbar(sm)
Output: