Python 在matplotlib中散布二维numpy数组

Python 在matplotlib中散布二维numpy数组,python,numpy,matplotlib,Python,Numpy,Matplotlib,我有一个4x4数据阵列,如 data = np.array([[0,1,1,1], [1,0,0,1], [1,1,1,0], [0,0,0,1]]) 现在我想把这个数组分散在一个2D图上 如果数据[i,j]等于1,则在点(x,y)=(i,j)处应有一个色斑。我曾尝试在matplotlib中使用散点图,但不知何故无法使用。您可以使用 import numpy as np import matplotlib.pyplot as plt data = np.array([[0,1,1,1], [

我有一个4x4数据阵列,如

data = np.array([[0,1,1,1], [1,0,0,1], [1,1,1,0], [0,0,0,1]])
现在我想把这个数组分散在一个2D图上

如果
数据[i,j]
等于1,则在点(x,y)=(i,j)处应有一个色斑。我曾尝试在matplotlib中使用散点图,但不知何故无法使用。

您可以使用

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[0,1,1,1], [1,0,0,1], [1,1,1,0], [0,0,0,1]])

# get the indices where data is 1
x,y = np.argwhere(data == 1).T

plt.scatter(x,y)
plt.show()
但是,当您只想可视化4x4阵列时,可以使用
matshow

plt.matshow(data)
plt.show()