Python根據(jù)已知鄰接矩陣繪制無向圖操作示例
本文實例講述了Python根據(jù)已知鄰接矩陣繪制無向圖操作。分享給大家供大家參考,具體如下:
有六個點:[0,1,2,3,4,5,6],六個點之間的鄰接矩陣如表格所示,根據(jù)鄰接矩陣繪制出相對應(yīng)的圖
|
0 |
1 |
2 |
3 |
4 |
5 |
6 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
1 |
2 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
3 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
4 |
0 |
1 |
0 |
1 |
1 |
1 |
1 |
5 |
1 |
1 |
1 |
1 |
1 |
0 |
0 |
6 |
0 |
1 |
0 |
1 |
1 |
0 |
0 |
將點之間的聯(lián)系構(gòu)造成如下矩陣
N = [[0, 3, 5, 1],
[1, 5, 4, 3],
[2, 1, 3, 5],
[3, 5, 1, 4],
[4, 5, 1, 3],
[5, 3, 4, 1],
[6, 3, 1, 4]]
代碼如下
# -*- coding:utf-8 -*- #! python3 import networkx as nx import matplotlib.pyplot as plt G=nx.Graph() point=[0,1,2,3,4,5,6] G.add_nodes_from(point) edglist=[] N = [[0, 3, 5, 1],[1, 5, 4, 3],[2, 1, 3, 5],[3, 5, 1, 4],[4, 5, 1, 3],[5, 3, 4, 1],[6, 3, 1, 4]] for i in range(7): for j in range(1,4): edglist.append((N[i][0],N[i][j])) G=nx.Graph(edglist) position = nx.circular_layout(G) nx.draw_networkx_nodes(G,position, nodelist=point, node_color="r") nx.draw_networkx_edges(G,position) nx.draw_networkx_labels(G,position) plt.show()
顯示結(jié)果:
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學運算技巧總結(jié)》、《Python正則表達式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
用pandas劃分數(shù)據(jù)集實現(xiàn)訓(xùn)練集和測試集
這篇文章主要介紹了用pandas劃分數(shù)據(jù)集實現(xiàn)訓(xùn)練集和測試集,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07win10+anaconda安裝yolov5的方法及問題解決方案
這篇文章主要介紹了win10+anaconda安裝yolov5的方法及問題解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04