Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you map a 3d matrix to color values in a 3d scatter plot using matplotlib?

I have a 10x10x10 numpy matrix that I'm trying to visualize in 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=??)

I would like a 3d plot where the darkness at location i,j,k is given by M[i,j,k]. How exactly am I suppose to pass M to scatter() so that it does this correctly? It seems to want a 2d array, but I don't understand how that would work in this case.

like image 651
theQman Avatar asked Jun 22 '17 18:06

theQman


Video Answer


1 Answers

The scatter needs the same number of points than the color array c. So for 1000 colors you need 1000 points.

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()

enter image description here

like image 161
ImportanceOfBeingErnest Avatar answered Nov 14 '22 23:11

ImportanceOfBeingErnest