I want to represent a binary 3D matrix in a 3D plot (if possible not with mayavi.mlab). At every location (x,y,z) where the matrix has a 1 a point should be plotted. My matrix is built the following way:
import numpy as np
size = 21
my_matrix = np.zeros(shape = (size, size, size))
random_location_1 = (1,1,2)
random_location_2 = (3,5,8)
my_matrix[random_location_1] = 1
my_matrix[random_location_2] = 1
Now at the coordinates (1,1,2) and (3,5,8) a dot should be visible, everywhere else just empty space. Is there any way to do this (e.g. with matplotlib?)
1 Answer 1
Sounds like you need a scatter plot. Take a look at this mplot3d tutorial. For me this worked:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
size = 21
m = np.zeros(shape = (size, size, size))
random_location_1 = (1,1,2)
random_location_2 = (3,5,8)
m[random_location_1] = 1
m[random_location_2] = 1
pos = np.where(m==1)
ax.scatter(pos[0], pos[1], pos[2], c='black')
plt.show()
answered Mar 16, 2017 at 10:25
Michael H.
3,5232 gold badges27 silver badges31 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py