Building a 3D image using data in a NumPy array

I have a data file in a NumPy array, I would like to see a 3D image. I use an example where I can view a 2D image of size (100, 100), this is a slice in the xy plane at z = 0.

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
X, Y, Z = np.mgrid[-10:10:100j, -10:10:100j, -10:10:100j]
T = np.sin(X*Y*Z)/(X*Y*Z)
T=T[:,:,0]
im = plt.imshow(T, cmap='hot')
plt.colorbar(im, orientation='vertical')
plt.show()

Slice at z = 0

How can I view a 3D image of T shape data (100, 100, 100)?

+4
source share
2 answers

I think the main problem is that you have 4 information for each point, so you are actually interested in a 4-dimensional object. Building this is always difficult (maybe even impossible). I suggest one of the following solutions:

  • : x, y, z, , z = f(x,y)

  • , , 100 z, 5, 5 , .

, , :

. 2- f(x,y)=z T B. , , . .

, 1.A( ) z=x^2+y^2 : enter image description here

:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib as mpl
X, Y = np.mgrid[-10:10:100j, -10:10:100j]
Z = (X**2+Y**2)/10 #definition of f
T = np.sin(X*Y*Z)
norm = mpl.colors.Normalize(vmin=np.amin(T), vmax=np.amax(T))
T = mpl.cm.hot(T) #change T to colors
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Z, facecolors=T, linewidth=0,
       cstride = 1, rstride = 1)
plt.show()

- :

enter image description here

:

norm = mpl.colors.Normalize(vmin=-1, vmax=1)
X, Y= np.mgrid[-10:10:101j, -10:10:101j]
fig = plt.figure()
ax = fig.gca(projection='3d')
for i in np.linspace(-1,1,5):
    Z = np.zeros(X.shape)+i
    T = np.sin(X*Y*Z)
    T = mpl.cm.hot(T)
    ax.plot_surface(X, Y, Z, facecolors=T, linewidth=0, alpha = 0.5, cstride 
        = 10, rstride = 10)

plt.show()

. T = sin(X*Y*Z), X*Y*Z , , 0.

0

. NumPy, TVTK ImageData, mlab Mayavi.

from tvtk.api import tvtk
import numpy as np
from mayavi import mlab
X, Y, Z = np.mgrid[-10:10:100j, -10:10:100j, -10:10:100j]
data = np.sin(X*Y*Z)/(X*Y*Z)
i = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
i.point_data.scalars = data.ravel()
i.point_data.scalars.name = 'scalars'
i.dimensions = data.shape
mlab.pipeline.surface(i)
mlab.colorbar(orientation='vertical')
mlab.show()

enter image description here

from numpy import random
data = random.random((20, 20, 20))

enter image description here

0

Source: https://habr.com/ru/post/1683625/


All Articles