简体   繁体   English

NetworkX有向图,无权重和自弧

[英]NetworkX directed graph, no weights and self arc

I would like a self-loop from node 1 to itself. 我想要一个从节点1到自身的自环。 I tried G.add_edge(1,1) but that did not work. 我尝试了G.add_edge(1,1)但是没有用。 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()

The edge is there - it just isn't drawn by the NetworkX Matplotlib drawing code. 优势就在这里-NetworkX Matplotlib绘制代码并未绘制它。 You can use Graphviz: 您可以使用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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM