本文介绍了NetworkX有向图,无权重和自弧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个从节点1到自身的自环.我尝试了G.add_edge(1,1)
,但是没有用.我的代码如下
I would like a self-loop from node 1 to itself. I tried G.add_edge(1,1)
but that did not work. My code is as follows
import networkx as nx
import pylab
G = nx.DiGraph()
G.add_node(1,pos=(1,1))
G.add_node(2,pos=(0,0))
G.add_node(3,pos=(2,0))
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(1,1)
pos=nx.get_node_attributes(G,'pos')
nx.draw(G,pos)
pylab.show()
推荐答案
边缘就在那里-NetworkX Matplotlib绘制代码并未绘制该边缘.您可以使用Graphviz:
The edge is there - it just isn't drawn by the NetworkX Matplotlib drawing code.You can use Graphviz:
import networkx as nx
import pylab
G = nx.DiGraph()
G.add_node(1,pos="100,100")
G.add_node(2,pos="0,0")
G.add_node(3,pos="200,0")
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(1,1)
print G.edges(data=True)
# [(1, 1, {}), (1, 2, {}), (1, 3, {})]
nx.write_dot(G,'graph.dot')
# use -n to suppress node positioning (routes edges)
# run dot -n -Tpng graph.dot >graph.png
这篇关于NetworkX有向图,无权重和自弧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!