Python 用Matplotlib绘制网格中加权单元的矩阵
我有一个由随机整数数组构成的方阵,定义如下:Python 用Matplotlib绘制网格中加权单元的矩阵,python,matplotlib,matrix,grid,Python,Matplotlib,Matrix,Grid,我有一个由随机整数数组构成的方阵,定义如下: import numpy as np dim_low, dim_high = 0, 20 #array of random integers' dimensions matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high)) print(matrix) #the matrix of defined with repetiti
import numpy as np
dim_low, dim_high = 0, 20 #array of random integers' dimensions
matrix = np.random.random_integers(low = dim_low,high = dim_high, size=(dim_high,dim_high))
print(matrix) #the matrix of defined with repetitions of the array.
图片中的结果矩阵:
如何使用Matplotlib绘制网格中生成的矩阵,使每个单元格的值(权重)打印在每个单元格的中心,并且在x和y轴上有一个从0到20的比例,如下图所示(注意,'x'和'o'在示例中是文本,我需要的是整数形式的权重,而不是文本形式的权重):
(此处)适合此功能的模块是seaborn。它具有您要求的所有功能和更多功能…
尝试使用。我不会带您浏览不同的选项,因为它们都有很好的文档记录。
祝你好运
顺便说一句,您需要使用熊猫透视表以实现舒适的兼容性。我从中提取了大部分内容
如果您能够生成所示的图像,那么将矩阵元素放在文本中而不是放在一些字母中有什么区别呢?
import numpy as np
import matplotlib.pyplot as plt
low_dim = 0
high_dim = 20
matrix = np.random.randint(low_dim, high_dim, (high_dim,high_dim))
fig, ax = plt.subplots()
for i in range(0, high_dim):
for j in range(0, high_dim):
val = matrix[i,j]
ax.text(i+0.5, j+0.5, str(val), va='center', ha='center')
ax.set_xlim(low_dim, high_dim)
ax.set_ylim(low_dim, high_dim)
ax.set_xticks(np.arange(high_dim))
ax.set_yticks(np.arange(high_dim))
ax.grid()
plt.show()