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.1and will be removed in3.3. Use np.iterable instead.ifnot 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.1and will be removed in3.3. Use np.iterable instead.ifnot 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.1and will be removed in3.3. Use np.iterable instead.ifnot 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.1and will be removed in3.3. Use np.iterable instead.ifnot 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)]