numpy 部分2D阵列的颜色图

li9yvcax  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(120)

H=[[0,0,0,0][0,1,2,3][0,3,4,3][0,5,2,3]]
是否有任何方法将H[i在范围(1,4)][j在范围(1,4)]与范围(1,4)中的x轴和范围(1,4)中的y轴绘制出来?

我想去掉图中最暗的部分
我试过了

import numpy as np
import matplotlib.pyplot as plt

H=[[0,0,0,0][0,1,2,3][0,3,4,3][0,5,2,3]]
V=np.array([[H[i][j] for j in range(1,4)] for i in range(1,4)])

plt.imshow(V)
plt.colorbar(orientation='vertical')
plt.show()

但该图显示H[1][1]在(x,y)=(0,0)上。我想让它显示(i,j)上的每个H[i][j]。我已经找了很长时间,但我找不到解决办法。请帮帮忙

kadbb459

kadbb4591#

IIUC,您可以将0替换为np.nan

# Setup
H =[[0,0,0,0], [0,1,2,3], [0,3,4,3], [0,5,2,3]]
V = np.array(H)

# Create a new array where 0 is now NaN
U = np.where(V==0, np.nan, V)

# Display the result
plt.imshow(U)
plt.colorbar(orientation='vertical')
plt.show()

输出:

>>> V
array([[0, 0, 0, 0],
       [0, 1, 2, 3],
       [0, 3, 4, 3],
       [0, 5, 2, 3]])

>>> U
array([[nan, nan, nan, nan],
       [nan,  1.,  2.,  3.],
       [nan,  3.,  4.,  3.],
       [nan,  5.,  2.,  3.]])

相关问题