python draw multigraph

2019-02-28 02:01发布

I try to draw multigraph in Python using graphviz.

For now I can draw usual graphs in Python somehow like:

import pygraphviz as pgv
G=pgv.AGraph()
G.add_node('a')
G.add_node('b')
G.layout()
G.add_edge('a','b','first')
G.add_edge('a','b','second')
sorted(G.edges(keys=True)) 
G.draw('file.png')

And I get on the output:

enter image description here

But actually I want get multigraph, i.e.

enter image description here

But documentation stays that it should differentiate :

enter image description here

I have no idea about drawing multigraph but not just graph.

Thanks for any help.

Addition:

it seems that there are no yet such libraries in python that can do it, so I did it using Wolfram Mathematica. But question is still opened.

Addition

Now working code looks so:

import pygraphviz as pgv
G=pgv.AGraph(strict=False)
G.add_node('a')
G.add_node('b')
G.layout()
G.add_edge('a','b','first')
G.add_edge('a','b','second')
sorted(G.edges(keys=True)) 
G.draw('file.png')

1条回答
手持菜刀,她持情操
2楼-- · 2019-02-28 02:18

As the documentation you quoted says, you need to specify strict=False when creating a multi-edge graph. Since you didn't do this your graph doesn't support parallel edges.

查看更多
登录 后发表回答