注意
转到末尾下载完整的示例代码。
自环#
自环是指一条边,它的起点和终点是同一个节点。本示例展示了如何使用 nx_pylab
绘制自环。

import networkx as nx
import matplotlib.pyplot as plt
# Create a graph and add a self-loop to node 0
G = nx.complete_graph(3, create_using=nx.DiGraph)
G.add_edge(0, 0)
pos = nx.circular_layout(G)
# As of version 2.6, self-loops are drawn by default with the same styling as
# other edges
nx.draw(G, pos, with_labels=True)
# Add self-loops to the remaining nodes
edgelist = [(1, 1), (2, 2)]
G.add_edges_from(edgelist)
# Draw the newly added self-loops with different formatting
nx.draw_networkx_edges(G, pos, edgelist=edgelist, arrowstyle="<|-", style="dashed")
plt.show()
脚本总运行时间: (0 分钟 0.056 秒)