绘制3D图像以NumPy数组形式形成数据 [英] Plotting 3D image form a data in NumPy-array
问题描述
我在NumPy数组中有一个数据文件,我想查看3D图像。我正在分享一个示例,其中我可以查看大小为(100,100)的2D图像,这是xy平面中z = 0时的一个切片。
I have a data file in NumPy array, I would like to view the 3D-image. I am sharing an example, where I can view 2D image of size (100, 100), this is a slice in 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()
如何查看形状为(100,100,100)的数据T的3D图像?
How can I view a 3D image of the data T of shape (100, 100, 100)?
推荐答案
我已经解决了我的问题。如果我们有NumPy数据,则可以将其转换为TVTK ImageData,然后可以借助Maya形式的mlab进行可视化。该代码及其3D可视化如下
I have got a solution to my question. If we have the NumPy data, then we can convert them into TVTK ImageData and then visualization is possible with the help of mlab form Mayavi. The code and its 3D visualization are the following
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()
对于另一个随机生成的数据
For another randomly generated data
from numpy import random
data = random.random((20, 20, 20))
可视化将是
这篇关于绘制3D图像以NumPy数组形式形成数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!