I want to change the opacity of the edge based on the weight in a directed network. This is the code I found and changed a bit but it doesn't work, it still gives the same opacity. Any suggestions?
A = np.array([[0, 0, 0],[2, 0, 3],[5, 0, 0]])
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
layout = nx.spring_layout(G)
nx.draw(G, layout, with_labels=True)
for edge in G.edges(data="weight"):
nx.draw_networkx_edges(G, layout, edgelist=[edge], alpha = (edge[2]/10))
plt.show()
The reason it's not working is because you call nx.draw
before drawing your edges. Instead draw your edges first and then draw your nodes.
See code below:
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
A = np.array([[0, 0, 0],[2, 0, 3],[5, 0, 0]])
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)
layout = nx.spring_layout(G)
for edge in G.edges(data="weight"):
nx.draw_networkx_edges(G, layout, edgelist=[edge], alpha = (edge[2]/10))
nx.draw_networkx_nodes(G, layout)
nx.draw_networkx_labels(G,layout)
plt.show()
And the output gives: