python - 使用python将简单矩阵转换为关联矩阵
问题描述
我想从关联矩阵制作图形网络,但我没有关联矩阵,我只有一个简单的矩阵。所以我的问题是:如何将简单的矩阵转换为关联矩阵以使用 python 绘制图形网络?
解决方案
我希望这会有所帮助,输出显示在最后
import numpy as np
import networkx as nx #version 2.2
import matplotlib.pyplot as plt
import pandas as pd
# matrix goes here
Mat = [
[0,0,-1,0,0],
[1,1,-1,-1,0],
[1,-1,0,0,0],
[1,0,0,-1,0],
[1,0,1,-1,1]
]
A = pd.DataFrame(Mat)
#refine rowname and colnames
nodes = ["a","b","c","d","e"]
A.index = nodes
A.columns = nodes
#create graph
G0 = nx.from_pandas_adjacency(A)
#create weight labels
combs = {}
adjc = G0.adj
for i in adjc:
suba = adjc[i]
for j in suba:
combs[(i,j)] = suba[j]['weight']
#define network structure i.e shell
nx.draw_shell(G0, with_labels=True )
nx.draw_networkx_edge_labels(G0, pos=nx.shell_layout(G0), edge_labels=combs)
plt.draw()
推荐阅读
- image - 如何使用具有动态图像大小的 imagemagick 创建简单的反射?
- ios - nslayout 约束不使对象出现
- vba - 在 Access VBA 中读取文本文件
- reporting-services - Microsoft SSRS:如何制作水平流动的填充文本
- keras - 查询 Keras 中 LSTM 的输入输出形状
- javascript - 如何使用 three.js 渲染具有不透明度分布的管?
- python - 测试包装 api 路由的装饰器
- python - 如何在 Python 中使用 Google 的 Text-to-Speech API
- python - Python套接字 - 从服务器接收不起作用
- codemirror - 如何在codemirror中添加class和id