我有一个2D数组,我想根据每个单元格的值用颜色对其进行着色;这有效:
import numpy as np
import matplotlib.pyplot as plt
W = np.zeros((3,3))
for x in range(3):
for y in range(3):
c = np.random.randint(1,4)
W[x][y]=c #2D array with 1, 2 or 3 in each cell
x = np.linspace(3,5,3)
y = np.linspace(3,5,3)
X, Y = np.meshgrid(x, y)
plt.pcolormesh(X,Y,W)
在3D中如何完成同样的事情?鉴于3D阵列,天真的想法不起作用。
W = np.zeros((3,3,3))
for x in range(3):
for y in range(3):
for z in range(3):
c = np.random.randint(1,4)
W[x][y][z]=c #3D array with 1, 2 or 3 in each cell
x = np.linspace(3,5,3)
y = np.linspace(3,5,3)
z = np.linspace(3,5,3)
X, Y, Z = np.meshgrid(x, y, z)
plt.pcolormesh(X,Y,Z,W)
不工作。建议?