将 0/1 矩阵转换为 python 中的二维网格图
convert 0/1 matrix to a 2D grid graph in python
给定一个包含 0/1 矩阵的 .txt 文件,在 python.I 中实现网格图应该为该矩阵创建一个网格图,但问题是我应该在那些有 0 的部分创建网格和块中有 1s 的部分。
所以我的 txt 文件包含这个:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
我应该根据上面的矩阵创建波纹管网格但不知道如何:
我们可以用 nx.grid_2d_graph
定义一个网格图,并只保留矩阵中值为 0
的那些节点。然后我们可以使用网格图中的坐标来定位节点:
from matplotlib import pyplot as plt
import numpy as np
import networkx as nx
# lines to 2d array
with open('myfile.txt') as f:
a = np.array([list(map(int,i.split())) for i in f.readlines()])
# define grid graph according to the shape of a
G = nx.grid_2d_graph(*a.shape)
# remove those nodes where the corresponding value is != 0
for val,node in zip(a.ravel(), sorted(G.nodes())):
if val!=0:
G.remove_node(node)
plt.figure(figsize=(9,9))
# coordinate rotation
pos = {(x,y):(y,-x) for x,y in G.nodes()}
nx.draw(G, pos=pos,
node_color='grey',
width = 4,
node_size=400)
给定一个包含 0/1 矩阵的 .txt 文件,在 python.I 中实现网格图应该为该矩阵创建一个网格图,但问题是我应该在那些有 0 的部分创建网格和块中有 1s 的部分。 所以我的 txt 文件包含这个:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
我应该根据上面的矩阵创建波纹管网格但不知道如何:
我们可以用 nx.grid_2d_graph
定义一个网格图,并只保留矩阵中值为 0
的那些节点。然后我们可以使用网格图中的坐标来定位节点:
from matplotlib import pyplot as plt
import numpy as np
import networkx as nx
# lines to 2d array
with open('myfile.txt') as f:
a = np.array([list(map(int,i.split())) for i in f.readlines()])
# define grid graph according to the shape of a
G = nx.grid_2d_graph(*a.shape)
# remove those nodes where the corresponding value is != 0
for val,node in zip(a.ravel(), sorted(G.nodes())):
if val!=0:
G.remove_node(node)
plt.figure(figsize=(9,9))
# coordinate rotation
pos = {(x,y):(y,-x) for x,y in G.nodes()}
nx.draw(G, pos=pos,
node_color='grey',
width = 4,
node_size=400)