绘制 3D 图像形成 NumPy 数组中的数据 [英] Plotting 3D image form a data in NumPy-array

查看:40
本文介绍了绘制 3D 图像形成 NumPy 数组中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NumPy 数组中有一个数据文件,我想查看 3D 图像.我正在分享一个示例,我可以在其中查看大小为 (100, 100) 的二维图像,这是 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,然后在 Mayavi 形式的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆