rjpj1998
rjpj1998

Reputation: 419

Visualizing a 3d numpy array of 1's and 0's

Alright so guys I have this 3d array of 1's and 0's which is supposed to represent a 3d object. 0 means that there is nothing there. 1 means that the objects exists in that co-ordinate. I need to display the 3d-object on my screen. It would be ideal for me to have a discrete 3 dimensoinal graph with value depending colors. I tried looking into glumpy and vispy but the documentation page seems to be down right now.

Upvotes: 2

Views: 5368

Answers (3)

Eric
Eric

Reputation: 97565

I made a pull request to matplotlib that does exactly this, adding the ax3d.voxels function. Unfortunately, it hasn't been reviewed fully yet.

Update: This made it into matplotlib 2.1

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# your real data here - some 3d boolean array
x, y, z = np.indices((10, 10, 10))
voxels = (x == y) | (y == z)

ax.voxels(voxels)

plt.show()

resulting plot

Upvotes: 6

Tom Wyllie
Tom Wyllie

Reputation: 2086

Use np.where to extract the coordinates, and matplotlib for the 3D plot.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = np.zeros(shape=(20, 20, 20), dtype=np.bool_)
np.fill_diagonal(data, True)

fig = plt.figure()
Axes3D(fig).plot_wireframe(*np.nonzero(data))
plt.show()

This plots a basic 3D wireframe according to where the ones appeared in the matrix. You may wish to use plot_surface or scatter in place of plot_wireframe. See the documentation for more information.

Upvotes: 1

Martin Beckett
Martin Beckett

Reputation: 96119

Could you save the x,y,z coordinates of each '1' point to a file and display it with cloudcompare or meshlab?

Cloudcompare will even let you store other values after each point and choose how to map these onto colour

Upvotes: 0

Related Questions