Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Plot VTK with matplotlib
#!/usr/bin/env python
import os
import numpy as np
import vtk
import matplotlib.pyplot as plt
def load_velocity(filename):
if not os.path.exists(filename):
return None
reader = vtk.vtkPolyDataReader()
data = reader.GetOutput()
# Extracting triangulation information
triangles = data.GetPolys().GetData()
points = data.GetPoints()
# Mapping data: cell -> point
mapper = vtk.vtkCellDataToPointData()
mapped_data = mapper.GetOutput()
# Extracting interpolate point data
udata = mapped_data.GetPointData().GetArray(0)
ntri = triangles.GetNumberOfTuples()/4
npts = points.GetNumberOfPoints()
nvls = udata.GetNumberOfTuples()
tri = np.zeros((ntri, 3))
x = np.zeros(npts)
y = np.zeros(npts)
ux = np.zeros(nvls)
uy = np.zeros(nvls)
for i in xrange(0, ntri):
tri[i, 0] = triangles.GetTuple(4*i + 1)[0]
tri[i, 1] = triangles.GetTuple(4*i + 2)[0]
tri[i, 2] = triangles.GetTuple(4*i + 3)[0]
for i in xrange(npts):
pt = points.GetPoint(i)
x[i] = pt[0]
y[i] = pt[1]
for i in xrange(0, nvls):
U = udata.GetTuple(i)
ux[i] = U[0]
uy[i] = U[1]
return (x, y, tri, ux, uy)
x, y, tri, ux, uy = load_velocity('U_test-plane.vtk')
plt.tricontour(x, y, tri, ux, 16, linestyles='-',
colors='black', linewidths=0.5)
plt.tricontourf(x, y, tri, ux, 16)
plt.rc('text', usetex=True)
plt.xlim([0, 0.1])
plt.ylim([0, 0.1])
plt.gca().tick_params(direction='out', which='both')
plt.title('$\mathsf{Cavity\ tutorial,\ u_x}$')
plt.savefig("cavity-ux.png", dpi=300, bbox_inches='tight')
plt.savefig("cavity-ux.pdf", bbox_inches='tight')

This comment has been minimized.

Copy link
Owner Author

@mrklein mrklein commented Sep 22, 2015


And this is the result


This comment has been minimized.

Copy link

@ponzer007 ponzer007 commented Nov 13, 2015

Helpful, thanks


This comment has been minimized.

Copy link

@tacio-bicudo tacio-bicudo commented Mar 2, 2018

Very nice! Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.