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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With