2

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

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.