NetworkX創建圖節點並添加邊

In [1]: import networkx as nx

In [3]: import matplotlib.pyplot as plt

In [4]: G = nx.Graph()

In [5]: G.add_nodes_from([1,2,3])

In [6]: nx.draw_networkx(G);plt.show()

在這裏插入圖片描述

兩種添加單邊的方法
In [7]: G.add_edge(1,2)

In [8]: nx.draw_networkx(G);plt.show()
D:\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:579: MatplotlibDeprecationWarning:
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):

In [9]: e = (2,3)

In [10]: G.add_edge(*e)

In [11]: nx.draw_networkx(G);plt.show()
D:\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:579: MatplotlibDeprecationWarning:
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):

在這裏插入圖片描述

同時添加多條邊
In [12]: G.add_edges_from([(1,2),(1,3)])

In [13]: nx.draw_networkx(G);plt.show()
D:\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:579: MatplotlibDeprecationWarning:
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):

在這裏插入圖片描述

添加節點
In [14]: A = nx.Graph()

In [15]: H = nx.path_graph(10)

In [16]: A.add_node(H)

In [17]: nx.draw_networkx(A);plt.show()

In [18]: A.add_nodes_from(H)

In [19]: nx.draw_networkx(A);plt.show()

在這裏插入圖片描述

In [20]: A.add_edges_from(H.edges)

In [21]: nx.draw_networkx(A);plt.show()
D:\Anaconda3\lib\site-packages\networkx\drawing\nx_pylab.py:579: MatplotlibDeprecationWarning:
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
  if not cb.iterable(width):

在這裏插入圖片描述

查看邊
In [23]: print(G.edges)
[(1, 2), (1, 3), (2, 3)]

In [25]: print(A.edges)
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章