本文實例講述了Python根據(jù)已知鄰接矩陣繪制無向圖操作。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站制作與策劃設計,通許網(wǎng)站建設哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設十多年,網(wǎng)設計領域的專業(yè)建站公司;建站業(yè)務涵蓋:通許等地區(qū)。通許做網(wǎng)站價格咨詢:18980820575有六個點:[0,1,2,3,4,5,6],六個點之間的鄰接矩陣如表格所示,根據(jù)鄰接矩陣繪制出相對應的圖
|
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()