python使用vtk与mayavi三维可视化绘图

VTK(Visualization Toolkit)是3D计算机图形学、图像处理和可视化的强大工具。它可以通过Python绑定使用,适合于科学数据的复杂可视化。Mayavi 依赖于 VTK (Visualization Toolkit),一个用于 3D 计算机图形、图像处理和可视化的强大库。

安装

pip install mayavi
pip install vtk

示例代码

  • vtk画圆锥
import vtk

# 创建一个锥体
cone = vtk.vtkConeSource()
cone.SetHeight(3.0)
cone.SetRadius(1.0)
cone.SetResolution(10)

# 创建映射器 Mapper
coneMapper = vtk.vtkPolyDataMapper()
coneMapper.SetInputConnection(cone.GetOutputPort())

# 创建演员 Actor
coneActor = vtk.vtkActor()
coneActor.SetMapper(coneMapper)
coneActor.GetProperty().SetColor(0.2, 0.63, 0.79)
coneActor.GetProperty().SetSpecular(0.5)
coneActor.GetProperty().SetSpecularPower(30)

# 创建渲染器 Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(coneActor)
renderer.SetBackground(0.1, 0.2, 0.4)

# 创建渲染窗口 RenderWindow
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetSize(800, 600)

# 创建渲染窗口交互器 RenderWindowInteractor
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# 初始化过程并开始渲染和交互
renderWindow.Render()
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()

在这里插入图片描述

  • mayavi画3D曲线
from mayavi import mlab
import numpy as np

# Creating data for a 3D parametric curve (a helix)
t = np.linspace(0, 4 * np.pi, 100)
x = np.sin(t)
y = np.cos(t)
z = t

# Plotting the parametric curve
mlab.figure(bgcolor=(1, 1, 1), size=(400, 300))
mlab.plot3d(x, y, z, t, tube_radius=0.1, colormap='Spectral')

# Display the figure
mlab.show()

在这里插入图片描述

  • mayavi可视化.mat数据
from mayavi import mlab
import scipy.io
import numpy as np

# 加载.mat文件
mat_data = scipy.io.loadmat('a.mat')  # 替换为您的.mat文件的路径
data = mat_data['a']

data_magnitude = np.abs(data )

# 创建一个新的Mayavi场景
mlab.figure(bgcolor=(1, 1, 1), size=(800, 600))

# 绘制声场的幅度
src = mlab.pipeline.scalar_field(data )

# 使用 volume 渲染来显示声场数据的内部结构
volume = mlab.pipeline.volume(src)
volume.module_manager.scalar_lut_manager.lut_mode = 'jet'  # 设置颜色映射 - 'jet'

# 调整颜色映射范围
volume.module_manager.scalar_lut_manager.use_default_range = False
volume.module_manager.scalar_lut_manager.data_range = [data.min(), data.max()]

# 获取颜色查找表
lut = volume.module_manager.scalar_lut_manager.lut.table.to_array()

# 修改查找表中的透明度通道,增加最大值的透明度
lut[:, -1] = np.linspace(0, 255, lut.shape[0])  # 调整透明度渐变

# 更新查找表
volume.module_manager.scalar_lut_manager.lut.table = lut

# 显示场景
mlab.show()
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mayavi 的参考手册,适合初学者和expert。 User guide: full table of contents An overview of Mayavi Introduction What is Mayavi2? Technical details Using Mayavi as an application, or a library? Scenes, data sources, and visualization modules: the pipeline model Loading data into Mayavi Installation Installing ready-made distributions Requirements for manual installs Doing it yourself: Python packages: Eggs Installing with easy_install Step-by-step instructions to install with eggs under Windows Under Mac OSX Snow Leopard The bleeding edge: Git Testing your installation Troubleshooting Using the Mayavi application Tutorial examples to learn Mayavi Parametric surfaces: a simple introduction to visualization Loading scalar data: the heart.vtk example Visualizing rich datasets: the fire_ug.vtu example Using Mayavi with scipy Exploring a vector field General layout of UI Visualizing data Modules Filters Interaction with the scene Mouse interaction Keyboard interaction From interactive usage to scripting The embedded Python interpreter Recording Mayavi actions to a script Command line arguments mlab: Python scripting for 3D plotting A demo 3D Plotting functions for numpy arrays 0D and 1D data 2D data 3D data Changing the looks of the visual objects created Adding color or size variations Changing the scale and position of objects Changing object properties interactively Figures, legends, camera and decorations Handling several figures Figure decorations Moving the camera Running mlab scripts Using mlab interactively Using together with Matplotlib’s pylab In scripts Animating the data Assembling pipelines with mlab Data sources Modules and filters Case studies of some visualizations Visualizing volumetric scalar data Visualizing a vector field Advanced use of Mayavi Organisation of Mayavi visualizations: the pipeline Anatomy of a Mayavi pipeline The link between different Mayavi entry points A pipeline example examined Data representation in Mayavi Introduction to TVTK datasets The flow of data Retrieving the data from Mayavi pipelines Dissection of the different TVTK datasets Inserting TVTK datasets in the Mayavi pipeline Objects populating the Mayavi pipeline Scene Source Filter ModuleManager: Colors and legends node Module Engine Base class: PipelineBase Class hierarchy Advanced Scripting with Mayavi Design Overview: Mayavi as a visualization framework Scripting the mayavi2 application Using the Mayavi envisage plugins Building applications using Mayavi Custom interactive dialogs Embedding a Mayavi scene in a Traits dialog A scene, with mlab embedded Making the visualization live Integrating in a WxPython application Integrating in a Qt application Tips and Tricks Off screen rendering Avoiding the rendering window Platform Summary Rendering using the virtual framebuffer Using VTK with Mesa for pure software rendering Extending Mayavi with customizations Customizing Mayavi2 Scripting Mayavi without using Envisage Computing in a thread Polling a file and auto-updating Mayavi Serving Mayavi on the network TCP server: the serve_tcp function UDP server: the serve_udp function Animating a visualization Animating a series of images Making movies from a stack of images Scripting from the command line Texture mapping actors Shifting data and plotting Using the UserDefined filter Sharing the same data between scenes Using mlab Using the core Mayavi API Changing the interaction with a scene Accelerating a Mayavi script Miscellaneous Citing Mayavi Getting help Tests for Mayavi Helping out Development quick start Improving the documentation Example gallery Mlab functions gallery Advanced mlab examples Interactive examples Advanced visualization examples Data interaction examples Misc examples MLab reference Plotting functions barchart contour3d contour_surf flow imshow mesh plot3d points3d quiver3d surf triangular_mesh Figure handling functions clf close draw figure gcf savefig screenshot sync_camera Figure decoration functions colorbar scalarbar vectorbar xlabel ylabel zlabel Camera handling functions move pitch roll view yaw Other functions animate axes get_engine orientation_axes outline set_engine show show_engine show_pipeline start_recording stop_recording text text3d title Mlab pipeline-control reference Sources Tools Data Modules and Filters Mayavi API reference Pipeline base objects Scene Source Filter ModuleManager Module PipelineBase Engine Main view and UI objects Scene UIs: DecoratedScene and MayaviScene SceneEditor MlabSceneModel EngineView and EngineRichView Known bugs and issues Changelog Mayavi 3.4.0 (Oct 15, 2010) Enhancements Fixes Mayavi 3.3.2 (May 25, 2010) Enhancements Fixes Mayavi 3.3.1 (Feb 24, 2010) Enhancements Fixes Mayavi 3.3.0 (July 15, 2009) Enhancements Fixes Mayavi 3.2.0 (March 23, 2009) Mayavi 3.1.0 Mayavi 3.0.3 Mayavi 3.0.1 and 3.0.2 Mayavi 3.0.0

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞华1993

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值