Plotting directed graphs in Python in a way that show all edges separately

The Graphviz tools appear to display distinct edges.

For example, giving this:

digraph G {
  A -> B;
  A -> B;
  A -> B;
  B -> C;

  B -> A;
  C -> B;
}

to dot produces:

example graph

Graphviz’s input language is pretty simple so you can generate it on your own, though searching for “python graphviz” does turn up a couple of libraries including a graphviz module on PyPI.

Here’s python that generates the above graph using the graphviz module:

from graphviz import Digraph

dot = Digraph()
dot.node('A', 'A')
dot.node('B', 'B')
dot.node('C', 'C')
dot.edges(['AB', 'AB', 'AB', 'BC', 'BA', 'CB'])

print(dot.source)
dot.render(view=True)

Leave a Comment