使用 Numpy 从 VTK 到 Matplotlib [英] VTK to Matplotlib using Numpy

查看:50
本文介绍了使用 Numpy 从 VTK 到 Matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 VTK 文件中提取一些数据(例如标量)及其在网格上的坐标,然后在 Matplotlib 中进行处理.问题是我不知道如何从 VTK 文件中获取点/单元数据(例如,通过给出标量的名称)并使用 vtk_to_numpy<将它们加载到 numpy 数组中/strong>

I want to extract some data (e.g. scalars) from a VTK file along with their coordinates on the grid then process it in Matplotlib. The problem is I dont know how to grab the point/cell data from the VTK file (by giving the name of the scalar, for instance) and load them into a numpy array using vtk_to_numpy

我的代码应该如下所示:

My code should look like:

import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import numpy as np
from vtk import *
from vtk.util.numpy_support import vtk_to_numpy

# load input data
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName("my_input_data.vtk")
reader.Update()

(...missing steps)

# VTK to Numpy
my_numpy_array = vtk_to_numpy(...arguments ?)

#Numpy to Matplotlib (after converting my_numpy_array to x,y and z)
CS = plt.contour(x,y,z,NbLevels)
...

PS:我知道 Paraview 可以完成这项任务,但我正在尝试对一些数据进行后期处理,而无需打开 Paraview.任何帮助表示赞赏

PS:I know that Paraview could do the task, but I am trying post process some data without having to open Paraview. Any help is appreciated

编辑 1

我发现这个 pdf 教程对于学习处理 VTK 的基础知识非常有用文件

I found this pdf tutorial to be very useful to learn the basics of handling VTK files

推荐答案

我终于找到了一种方法(可能不是最佳的)来完成这项工作.这里的例子是绘制从 vtk 文件中提取的温度场的等高线:

I finally figured a way (maybe not the optimal) that does the job. The example here is contour plotting a temperature field extracted from a vtk file:

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.interpolate import griddata
import numpy as np
import vtk
from vtk.util.numpy_support import vtk_to_numpy

# load a vtk file as input
reader = vtk.vtkXMLUnstructuredGridReader()
reader.SetFileName("my_input_data.vtk")
reader.Update()

# Get the coordinates of nodes in the mesh
nodes_vtk_array= reader.GetOutput().GetPoints().GetData()

#The "Temperature" field is the third scalar in my vtk file
temperature_vtk_array = reader.GetOutput().GetPointData().GetArray(3)

#Get the coordinates of the nodes and their temperatures
nodes_nummpy_array = vtk_to_numpy(nodes_vtk_array)
x,y,z= nodes_nummpy_array[:,0] , nodes_nummpy_array[:,1] , nodes_nummpy_array[:,2]

temperature_numpy_array = vtk_to_numpy(temperature_vtk_array)
T = temperature_numpy_array

#Draw contours
npts = 100
xmin, xmax = min(x), max(x)
ymin, ymax = min(y), max(y)

# define grid
xi = np.linspace(xmin, xmax, npts)
yi = np.linspace(ymin, ymax, npts)
# grid the data
Ti = griddata((x, y), T, (xi[None,:], yi[:,None]), method='cubic')  

## CONTOUR: draws the boundaries of the isosurfaces
CS = plt.contour(xi,yi,Ti,10,linewidths=3,cmap=cm.jet) 

## CONTOUR ANNOTATION: puts a value label
plt.clabel(CS, inline=1,inline_spacing= 3, fontsize=12, colors='k', use_clabeltext=1)

plt.colorbar() 
plt.show() 

这篇关于使用 Numpy 从 VTK 到 Matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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