如何使用 matplotlib 将 3d 矩阵映射到 3d 散点图中的颜色值?

the*_*man 6 python matplotlib

我有一个10x10x10numpy 矩阵,我试图在 3d 中进行可视化:

from mpl_toolkits.mplot3d import Axes3D
M = np.random.rand(10, 10, 10)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
counter = range(10)
ax.scatter(counter, counter, counter, c=??)
Run Code Online (Sandbox Code Playgroud)

我想要一个 3d 绘图,其中位置的黑暗i,j,kM[i,j,k]. 我到底应该如何传递Mscatter()它才能正确执行此操作?它似乎想要一个二维数组,但我不明白在这种情况下它是如何工作的。

Imp*_*est 8

散点需要与颜色数组相同的点数c。因此,对于 1000 种颜色,您需要 1000 点。

import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.mplot3d import Axes3D
M = np.random.rand(10, 10, 10)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
counter = range(10)
x,y,z = np.meshgrid(counter, counter, counter)
ax.scatter(x,y,z, c=M.flat)


plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


mau*_*uve -1

尝试:

scatter3D(xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True, *args, **kwargs)
Run Code Online (Sandbox Code Playgroud)

但 c != 无

https://matplotlib.org/mpl_toolkits/mplot3d/api.html