如何在Networkx/Graphviz中绘制平行边 [英] How to draw parallel edges in Networkx / Graphviz

查看:321
本文介绍了如何在Networkx/Graphviz中绘制平行边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NetworkX在两个节点之间添加平行边,但是由于以下错误而失败.我在做什么错了?

I am trying to add parallel edges between two nodes using NetworkX but it fails with the below error. What am I doing wrong?

import networkx as nx
import graphviz

g1 = nx.MultiGraph()

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key='one')
g1.add_edge(node1,node2,key='two')

A = nx.to_agraph(g1)
A.add_subgraph()

A.draw('test2.png', prog='dot')

错误:

Traceback (most recent call last):
  File "test2.py", line 12, in <module>
    A = nx.to_agraph(g1)
  File "C:\python27\lib\site-packages\networkx-1.11rc1-py2.7.egg\networkx\drawing\nx_agraph.py", line 152, in to_agraph
    A.add_edge(u,v,key=str(key),**str_edgedata)
  File "C:\python27\lib\site-packages\pygraphviz\agraph.py", line 481, in add_edge
    eh = gv.agedge(self.handle, uh, vh, key, _Action.find)
KeyError: 'agedge: no key'

推荐答案

您可以在不使用graphviz的情况下执行相同的操作.我将 connectionstyle 添加到nx.draw中:

You can do the same without using graphviz. I do it adding connectionstyle to nx.draw:

import networkx as nx

g1 = nx.DiGraph(directed=True)

node1 = 'a'
node2 = 'b'

g1.add_edge(node1,node2,key=1)
g1.add_edge(node2,node1,key=2)

nx.draw(g1, with_labels=True, arrows = True, connectionstyle='arc3, rad = 0.1')

这篇关于如何在Networkx/Graphviz中绘制平行边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆