Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matploblib:根据存储在数组中的索引绘制矩阵的单元格_Python_Matplotlib_Matrix - Fatal编程技术网

Python Matploblib:根据存储在数组中的索引绘制矩阵的单元格

Python Matploblib:根据存储在数组中的索引绘制矩阵的单元格,python,matplotlib,matrix,Python,Matplotlib,Matrix,我用Numpy构建了一个矩阵,并设置了矩阵单元格的值,然后在下面的代码中用Matplotlib绘制了它 import numpy as np import matplotlib.pyplot as plt matrix = np.loadtxt('C:\folder/matrix_values.txt', usecols=range(20)) matrix = np.int8(matriz) fig1, ax1 = plt.subplots() ax1.matshow(matrix

我用Numpy构建了一个矩阵,并设置了矩阵单元格的值,然后在下面的代码中用Matplotlib绘制了它

import numpy as np
import matplotlib.pyplot as plt


matrix = np.loadtxt('C:\folder/matrix_values.txt', usecols=range(20))  
matrix = np.int8(matriz)


fig1, ax1 = plt.subplots()

ax1.matshow(matrix, origin='upper', alpha=0, cmap=None, interpolation='nearest')

for i in xrange(20):
    for j in xrange(20):
        value = matrix[j,i]
        ax1.text(i, j, str(value), va='center', ha='center')


tick_labels = range(20)
ax1.set_xticks([], minor=False)
ax1.set_xticks(np.arange(-.5, 20, 1), minor=True)
ax1.set_yticks([], minor=False)
ax1.set_yticks(np.arange(-.5, 20, 1), minor=True)
ax1.set_xticklabels([], minor=False)
ax1.set_xticklabels(tick_labels, minor=True)
ax1.set_yticklabels([], minor=False)
ax1.set_yticklabels(tick_labels, minor=True)
ax1.xaxis.set_ticks_position('bottom')
ax1.grid(which='minor', color='grey', linestyle='-', linewidth=1)

plt.show()
结果:


现在我有了一个带有一些矩阵单元坐标的数组,比如[[0,1],[0,0],[0,2]…]。该数组是随机生成的,每次运行代码时,我都会在数组中放置12个单元格的坐标,另一次可能是17等。这是一个20x20矩阵,包含400个单元格。如何绘制坐标数组中所示单元格的背景

您可以首先创建一个与原始矩阵形状相同的数组,并用零填充它。然后,在坐标数组定义的位置,将它们放入该数组中。最后绘制该数组,而不是原始矩阵

import numpy as np
import matplotlib.pyplot as plt


matrix = np.random.randint(0,9, size=(10,10))
highlight = np.array([[1,3],[4,2],[6,8],[7,2]])

hm = np.zeros_like(matrix)
hm[highlight[:,1],highlight[:,0]] = 1

fig, ax = plt.subplots()

ax.matshow(hm, origin='upper', alpha=1, vmin=0, vmax=2, cmap="Blues")

for i in range(matrix.shape[1]):
    for j in range(matrix.shape[1]):
        ax.text(i, j, str(matrix[j,i]), va='center', ha='center')

n = min(matrix.shape)+1
tick_labels = range(n)
ax.set_xticks([], minor=False)
ax.set_xticks(np.arange(-.5, n-1, 1), minor=True)
ax.set_yticks([], minor=False)
ax.set_yticks(np.arange(-.5, n-1, 1), minor=True)
ax.set_xticklabels([], minor=False)
ax.set_xticklabels(tick_labels, minor=True)
ax.set_yticklabels([], minor=False)
ax.set_yticklabels(tick_labels, minor=True)
ax.xaxis.set_ticks_position('bottom')
ax.grid(which='minor', color='grey', linestyle='-', linewidth=1)

plt.show()