How to show cycles in networkx graph drawing

2019-01-15 17:14发布

问题:

I have a simple graph constructed using NetworkX as follows:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from([(0,1), (0,2), (1,1), (1,2)])
nx.draw_networkx(G)
plt.show() 

When I draw this graph, I get this image:

The graph also has an edge (1,1) but it does not show this edge on image. How can i draw this edge, too? I think this is the result of not using arrows. This visualization is really very bad. How can I use arrows instead of this bold lines?

回答1:

Graphviz does a great job of drawing arrows and self loops. (Non-trivial to implement in Matplotlib). Here is an example:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
G.add_edges_from(\[(0,1), (0,2), (1,1), (1,2)\])
nx.write_dot(G,'graph.dot')

# then run dot -Tpng graph.dot > graph.png