簡體   English   中英

NetworkX有向圖,無權重和自弧

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

我想要一個從節點1到自身的自環。 我嘗試了G.add_edge(1,1)但是沒有用。 我的代碼如下

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:

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