I am relatively new to using Networkx and pygraphviz (please bear with me...) I have a graph which I visualised using Networkx and the graphviz_layout.
After this line:
pos=graphviz_layout(G, prog='dot')
I retrieved lists of x,y coordinates in the graph like so:
#coordinates of the nodes
node_x = []
node_y = []
for(node,(x,y)) in pos.items():
node_x.append(x)
node_y.append(-y)
Is there a way I can retrieve the coordinates of edges in the graph and append it to lists? E.g.
#coordinates of the edges:
edge_x = []
edge_y = []
#how do i get the edge coordinates set by graphviz_layout here?
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)
Any help would be much appreciated! Thanks in advance!
As I don't have graphviz_layout
installed, and supposing it works similar as the other layout functions, I created a test with a standard layout function. The coordinates can be obtained by looping through the edges.
The following code uses list comprehension to make the code more compact. At the end plt.plot(edge_x, edge_y)
is used to visualize the created lists.
import networkx as nx
import matplotlib.pyplot as plt
G = nx.complete_graph(20)
pos = nx.circular_layout(G)
node_x = [x for x, y in pos.values()]
node_y = [-y for x, y in pos.values()]
edge_x = [x for n0, n1 in G.edges for x in (pos[n0][0], pos[n1][0], None)]
edge_y = [y for n0, n1 in G.edges for y in (-pos[n0][1], -pos[n1][1], None)]
plt.plot(edge_x, edge_y, color='purple', lw=0.5)
plt.scatter(node_x, node_y, color='crimson', s=50, zorder=3)
plt.gca().set_aspect('equal')
plt.axis('off')
plt.show()