user9068137
user9068137

Reputation:

Edges don't show in a graph using Networkx - Python

I am plotting a directed graph in python using networkx and matplotlib. However, not all edges are visible. The nodes are so close to each other that it doesn't show a path between two nodes.

Code:

import networkx as nx

resultNodes = ['P0', 'P1', 'P2', 'P3', 'P4', 'P5', 'P6', 'P7', 'P8', 'P9']

resultEdges = [('P0', 'P4'), ('P0', 'P2'), ('P0', 'P1'), ('P1', 'P7'), ('P1', 'P8'),
               ('P1', 'P2'), ('P2', 'P3'), ('P4', 'P5'), ('P4', 'P6'), ('P5', 'P6'),
               ('P9', 'P2'), ('P9', 'P7'), ('P9', 'P1')]

G=nx.DiGraph()

G.add_nodes_from(resultNodes)
G.add_edges_from(resultEdges)

print("Nodes of graph: ")
print(G.nodes())
print("Edges of graph: ")
print(G.edges())

nx.draw(G,with_labels=True,font_size=15,node_color='yellowgreen',node_size=1000)

Output image shows no edge between P4,P5,P6 as those nodes are so close

Upvotes: 2

Views: 2444

Answers (1)

Lucas
Lucas

Reputation: 7341

You can calculate the positions of the nodes, and pass to the draw function. See the doc.

Example:

pos = nx.drawing.layout.circular_layout(G)

nx.draw(G, pos=pos, with_labels=True, font_size=15,
        node_color='yellowgreen', node_size=1000)

circular_layout

Upvotes: 1

Related Questions