【问题标题】:VTK to Matplotlib using NumpyVTK 到 Matplotlib 使用 Numpy
【发布时间】:2014-06-02 00:26:19
【问题描述】:

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

我的代码应该是这样的:

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 的情况下对一些数据进行后期处理。任何帮助表示赞赏

编辑 1

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

【问题讨论】:

  • vtk_to_numpy 的文档对此有什么要说的?

标签: python arrays numpy matplotlib vtk


【解决方案1】:

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

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() 

【讨论】:

  • 我收到NameError: name 'cm' is not defined
  • @sigvaldm 缺少导入行,我更新了答案添加import matplotlib.cm as cm
  • 啊,我明白了。在某些时候我一定已经弄清楚了,因为我前一段时间受这篇文章的启发制作了一些情节。谢谢!
【解决方案2】:

我不知道你的数据集是什么样子的,所以这里只是一些你可以获得点位置和标量值的方法:

from vtk import *
from vtk.util.numpy_support import vtk_to_numpy

# load input data
reader = vtk.vtkGenericDataObjectReader()
reader.SetFileName(r"C:\Python27\VTKData\Data\uGridEx.vtk")
reader.Update()
ug  = reader.GetOutput()
points = ug.GetPoints()
print vtk_to_numpy(points.GetData())
print vtk_to_numpy(ug.GetPointData().GetScalars())

如果你可以使用tvtk会容易一点:

from tvtk.api import tvtk
reader = tvtk.GenericDataObjectReader()
reader.file_name = r"C:\Python27\VTKData\Data\uGridEx.vtk"
reader.update()
ug = reader.output
print ug.points.data.to_array()
print ug.point_data.scalars.to_array()

如果你想在matplotib中做contour绘图,我认为你需要一个网格,你可能需要使用一些VTK类将数据集转换为网格,例如vtkProbeFilter

【讨论】:

    猜你喜欢
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多