網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
使用工具
#導(dǎo)入模塊
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
import scipy.sparse as sp
準(zhǔn)備數(shù)據(jù)
# 鄰接矩陣
Matrix = np.array(
[
[0, 1, 1, 1, 1, 1, 0, 0], # a
[0, 0, 1, 0, 1, 0, 0, 0], # b
[0, 0, 0, 1, 0, 0, 0, 0], # c
[0, 0, 0, 0, 1, 0, 0, 0], # d
[0, 0, 0, 0, 0, 1, 0, 0], # e
[0, 0, 1, 0, 0, 0, 1, 1], # f
[0, 0, 0, 0, 0, 1, 0, 1], # g
[0, 0, 0, 0, 0, 1, 1, 0] # h
]
)
轉(zhuǎn)化臨界矩陣
def get_matrix_triad(coo_matrix , data=False):
'''
獲取矩陣的元組表示 (row,col)
data 為 True 時(shí) (row,col,data)
:dependent scipy
:param coo_matrix: 三元組表示的稀疏矩陣 類型可以為 numpy.ndarray
:param data: 是否需要 data值
:return:
list
'''
# 檢查類型
if not sp.isspmatrix_coo(coo_matrix):
# 轉(zhuǎn)化為三元組表示的稀疏矩陣
coo_matrix = sp.coo_matrix(coo_matrix)
# nx3的矩陣 列分別為 矩陣行,矩陣列及對(duì)應(yīng)的矩陣值
temp = np.vstack((coo_matrix.row , coo_matrix.col , coo_matrix.data)).transpose()
return temp.tolist()
測(cè)試
edags = get_matrix_triad(Matrix)
-->
[[0.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
[0.0, 2.0, 1.0],
[0.0, 3.0, 1.0],
[0.0, 4.0, 1.0],
[0.0, 5.0, 1.0],
[1.0, 1.0, 1.0],
[1.0, 2.0, 1.0],
[1.0, 4.0, 1.0],
[2.0, 2.0, 1.0],
[2.0, 3.0, 1.0],
[3.0, 3.0, 1.0],
[3.0, 4.0, 1.0],
[4.0, 4.0, 1.0],
[4.0, 5.0, 1.0],
[5.0, 2.0, 1.0],
[5.0, 5.0, 1.0],
[5.0, 6.0, 1.0],
[5.0, 7.0, 1.0],
[6.0, 5.0, 1.0],
[6.0, 6.0, 1.0],
[6.0, 7.0, 1.0],
[7.0, 5.0, 1.0],
[7.0, 6.0, 1.0],
[7.0, 7.0, 1.0]]
創(chuàng)建圖
# 創(chuàng)建一個(gè)沒有邊,沒有節(jié)點(diǎn)的空?qǐng)DGraph
G = nx.Graph()
添加節(jié)點(diǎn)
按照節(jié)點(diǎn)的個(gè)數(shù)添加節(jié)點(diǎn)
H = nx.path_graph(Matrix.shape[0])
G.add_nodes_from(H)
添加邊
G.add_edges_from(edags) #添加邊
# 若數(shù)據(jù)含有權(quán)重,及 get_matrix_triad() 中 data = True ,則使用
G.add_weighted_edges_from(edags)
繪圖
colors = np.arange(Matrix.shape[0])
nx.draw(G,pos=nx.spring_layout(G),node_color=colors)
plt.show()
擴(kuò)展
美化圖
合理使用**draw_networkx ()**中的參數(shù),來(lái)美化圖
draw_networkx()
https://networkx.github.io/documentation/stable/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html#networkx.drawing.nx_pylab.draw_networkx
總結(jié)
原文鏈接:https://blog.csdn.net/yf289178199/article/details/90238328
相關(guān)推薦
- 2022-10-10 Python讀取xlsx文件報(bào)錯(cuò):xlrd.biffh.XLRDError:?Excel?xlsx?
- 2022-04-16 Python3如何將源目錄中的圖片用MD5命名并可以設(shè)定目標(biāo)目錄_python
- 2022-07-12 Linux中xargs命令的用法
- 2023-03-21 Flutter?web?bridge?通信總結(jié)分析詳解_Android
- 2022-12-03 PostgreSQL?數(shù)組類型操作使用及特點(diǎn)詳解_PostgreSQL
- 2022-03-27 .NET?Core?使用委托實(shí)現(xiàn)動(dòng)態(tài)流程組裝的思路詳解_實(shí)用技巧
- 2023-05-15 shell?Bash的數(shù)組與關(guān)聯(lián)數(shù)組的實(shí)現(xiàn)_linux shell
- 2022-05-11 Excel單元格空,設(shè)置為空字符串
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支