首页 > 解决方案 > 如何使用 numpy 矩阵构建 newtworkx 图?

问题描述

我正在尝试构建像 numpy 邻接矩阵一样给出的图形视图。例如,

matrix=[['1' '0' '0'], ['0' '1' '0'], ['0' '0' '1']]

matrix = np.array(matrix)

f = plt.figure(figsize=(5, 4))
a = f.add_subplot(111)
plt.axis('off')

G = nx.from_numpy_matrix(matrix)
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos=pos, ax=a)
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)

始终为我输入的所有矩阵提供完整的图表。这里有什么不正确的?

标签: pythonpython-3.xtkinternetworkx

解决方案


这是您的代码,其中填充了一些位:

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline  
#%matplotlib notebook

matrix=np.array([[1, 0, 0], [0, 1, 0], [0, 0,1]])
f = plt.figure(figsize=(5, 4))
a = f.add_subplot(111)
plt.axis('off')

G = nx.from_numpy_matrix(matrix)
pos = nx.circular_layout(G)
nx.draw_networkx(G, pos=pos, ax=a)

回报:

在此处输入图像描述

我猜这就是你所追求的。


推荐阅读