我有一个pvtu文件和相关的vtu文件,我想要显示其中的一些数据。如果我在Paraview (5.6+)中加载pvtu,当我选择纯色(白色)和带边缘的表面时,我会得到以下图像:

网格明显是各向异性的,接近顶部边界,几乎是扁平的三角形;这是预期的行为。
如果我现在在Python中加载相同的pvtu并以以下方式显示网格,
import numpy
import matplotlib.pyplot as plt
import vtk
gridreader = vtk.vtkXMLPUnstructuredGridReader()
gridreader.SetFileName('whatever.pvtu')
gridreader.Update()
vtkOut = gridreader.GetOutput()
vtkData = vtkOut.GetPoints().GetData()
coords = numpy.array([vtkData.GetTuple3(x)
for x in range(vtkData.GetNumberOfTuples())])
plt.triplot(coords[:, 0], coords[:, 1])
plt.gcf().set_size_inches(16, 8)
plt.gca().set_aspect('equal')
plt.savefig('meshPython1.png', bbox_inches='tight')
plt.gca().set_xlim((5e5, 3e6))
plt.gca().set_ylim((6e5, 1e6))
plt.savefig('meshPython2.png', bbox_inches='tight')我明白了:


你可以很容易地看到不存在各向异性。因此,我天真的问题是:如何用Python重现Paraview中显示的网格?然而,可能有一个更准确的问题。我完全知道matplotlib的三角化库接受三角形作为参数,但我找不到从pvtu中提取三角形的命令。因此,也许更好的问题是如何从pvtu文件中获取三角形?
感谢您的帮助。
发布于 2019-11-10 00:47:53
你的问题是你没有使用matplotlib.tri的triangles选项。事实上,如果不在matplotlib中指定,ParaView中存在的网格的连接性就会丢失。事实上,你给了matplotlib一个自由来呈现它想要的任何单元格,当你知道你的三角形网格的连接性时,这显然是不正确的。可以使用以下命令提取三角形网格的连接性:
cell_connecitivty_matrix = []
for i in range(vtOut.GetNumberOfCells()):
assert vtkOut.GetCell(i).GetNumberOfPoints() == 3
cell_connecitivty_matrix.append(vtkOut.GetCell(i).GetPointIds())
cell_connecitivty_matrix = np.array(cell_connecitivty_matrix, dtype=np.float).reshape((vtOut.GetNumberOfCells(),3))
#plot triangles with their connectivity matrix
plt.triplot(coords[:, 0], coords[:, 1], triangles=cell_connectivity_matrix)发布于 2019-11-10 17:42:25
根据程序员的回答,下面的代码允许我实现与Paraview相同的网格:
import numpy
import matplotlib.pyplot as plt
import vtk
gridreader = vtk.vtkXMLPUnstructuredGridReader()
gridreader.SetFileName('whatever.pvtu')
gridreader.Update()
vtkOut = gridreader.GetOutput()
vtkData = vtkOut.GetPoints().GetData()
coords = numpy.array([vtkData.GetTuple3(x)
for x in range(vtkData.GetNumberOfTuples())])
cell_connectivity_matrix = []
for i in range(vtkOut.GetNumberOfCells()):
assert vtkOut.GetCell(i).GetNumberOfPoints() == 3
cell_connectivity_matrix.append(
[vtkOut.GetCell(i).GetPointIds().GetId(j)
for j in range(vtkOut.GetCell(i).GetPointIds().GetNumberOfIds())])
cell_connectivity_matrix = numpy.array(cell_connectivity_matrix,
dtype=numpy.float)
plt.triplot(coords[:, 0], coords[:, 1], triangles=cell_connectivity_matrix)
plt.gcf().set_size_inches(16, 8)
plt.gca().set_aspect('equal')
plt.show()将显示以下内容

https://stackoverflow.com/questions/58617015
复制相似问题