查看 3D 阵列

Viewing 3D array

我有 3D 数组标准化(0 到 1 之间)数据,我想以 python like this 的 3D 格式查看它,颜色与数组中的值匹配。

三维数组

您可以将 matplotlib.pyplot.scatter 与 3D 投影一起使用:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.random(size=(20, 10, 5))

ax = plt.subplot(projection='3d')

grid = np.meshgrid(np.arange(a.shape[2]),
                   np.arange(a.shape[1]),
                   np.arange(a.shape[0]))

x = ax.scatter(*grid, c=a)
plt.colorbar(x)

二维数组

您可以使用 matplotlib.pyplot.imshow:

import numpy as np
import matplotlib.pyplot as plt

a = np.random.random(size=(50,100))

plt.imshow(a, vmin=0, vmax=1)
plt.colorbar()

输出: