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

时间:2017-06-22 18:12:43

标签: python matplotlib

我有一个10x10x10 numpy矩阵,我试图在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=??)

我想要一个3d图,其中位置i,j,k的黑暗由M[i,j,k]给出。我打算如何将M传递给scatter(),以便正确执行此操作?它似乎想要一个二维数组,但我不明白在这种情况下它是如何工作的。

2 个答案:

答案 0 :(得分:2)

散点图需要的点数与颜色数组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)


plt.show()

enter image description here

答案 1 :(得分:-1)

尝试:

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

但是c!=无

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

相关问题