python - 如何使用 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)
始终为我输入的所有矩阵提供完整的图表。这里有什么不正确的?
解决方案
这是您的代码,其中填充了一些位:
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)
回报:
我猜这就是你所追求的。
推荐阅读
- reactjs - ReactJs - Countdown reset unexpectedly
- javascript - How to fix - If unexpected token - on this exercice
- java - 如何在不提供静态列宽的情况下创建具有多列的 Java SWT 表
- javascript - 无法获取 api 数据并使用 vuex 存储 - vue.js
- javascript - 停止输入字段内的按键弹出窗口
- ios - ScrollView content not filling in SwiftUI
- python - Why query.get is faster than any alternative?
- python - Basic Python program, keeps telling me my elements in list are strings not integers?
- reactjs - 是否有可能摆脱每次重新渲染的 useContext 计算?
- python - 尝试从“txt”文件中删除字符串时出错 - 联系人列表程序