title: Plotting mesh data from vtk python using matplotlib
body: <p><em>The following questions makes use of vtk python but what I am attempting to do should not require any knowledge of vtk because I have converted the data I wish to plot into numpy arrays described below. If anyone does know of an improvement to the way I go about actually processing the vtk data into numpy, please let me know!</em> </p>
  
  <p>I have some data that I have extracted using vtk python. The data consists of a 3D unstructured grid and has several 'blocks'. The block I am interested in is <code>block0</code>. The data is contained at each cell rather than at each point. I wish to plot a contourf plot of a scalar variable on this grid using matplotlib. In essence my problem comes down to the following:</p>
  
  <p>Given a set of cell faces with known vertices in space and a known scalar field variable, create a contour plot as one would get if one had created a <code>numpy.meshgrid</code> and used <code>plt.contourf/plt.pcolormesh</code> etc. Basically I post process my vtk data like so:    </p>
  
  <pre><code>numCells = block0.GetCells().GetNumberOfCells()
  
  # Array of the 8 vertices that make up a cell in 3D
  cellPtsArray = np.zeros((numCells,8,3)) 
  
  # Array of the 4 vertices that make up a cell face
  facePtsArray = np.zeros((numCells,4,3)) 
  
  #Array to store scalar field value from each cell
  valueArray = np.zeros((numCells,1)) 
  
  for i in xrange(numCells):
      cell = block0.GetCell(i)
      numCellPts = cell.GetNumberOfPoints() 
  
      for j in xrange(numCellPts):
          cellPtsArray[i,j,:] = block0.GetPoint(cell.GetPointId(j)) 
  
      valueArray[i] = block0.GetCellData().GetArray(3).GetValue(i)
      xyFacePts = cell.GetFaceArray(3) 
      facePtsArray[i,:,:] = cellPtsArray[i,xyFacePts,:]
  </code></pre>
  
  <p>Now I wish to create a contour plot of this data (fill each cell in space according to an appropriate colormap of the scalar field variable). Is there a good built in function in matplotlib to do this? <strong>Note that I cannot use any form of automatic triangulation-the connectivity of the mesh is already specified by <code>facePtsArray</code> by the fact that connections between points of a cell have been ordered correctly (see my plot below)</strong></p>
  
  <p>Here is some test data:</p>
  
  <pre><code>import numpy as np 
  import matplotlib.pyplot as plt
  
  # An example of the array containing the mesh information: In this case the 
  # dimensionality is (9,4,3) denoting 9 adjacent cells, each with 4 vertices and
  # each vertex having (x,y,z) coordinates.
  facePtsArray = np.asarray([[[0.0, 0.0, 0.0 ],
                            [1.0, 0.0, 0.0 ],
                            [1.0, 0.5, 0.0 ],
                            [0.0, 0.5, 0.0 ]],
                            [[0.0, 0.5, 0.0 ],
                            [1.0, 0.5, 0.0 ],
                            [1.0, 1.0, 0.0 ],
                            [0.0, 1.0, 0.0 ]],
                            [[0.0, 1.0, 0.0 ],
                            [1.0, 1.0, 0.0 ],
                            [1.0, 1.5, 0.0 ],
                            [0.0, 1.5, 0.0 ]],
                            [[1.0, 0.0, 0.0 ],
                            [2.0, -0.25, 0.0],
                            [2.0, 0.25, 0.0],
                            [1.0, 0.5, 0.0]],
                            [[1.0, 0.5, 0.0],
                            [2.0, 0.25, 0.0],
                            [2.0, 0.75, 0.0],
                            [1.0, 1.0, 0.0]],
                            [[1.0, 1.0, 0.0],
                            [2.0, 0.75, 0.0],
                            [2.0, 1.25, 0.0],
                            [1.0, 1.5, 0.0]],
                            [[2.0, -0.25, 0.0],
                             [2.5, -0.75, 0.0],
                             [2.5,  -0.25, 0.0 ],
                             [2.0, 0.25, 0.0]],
                            [[2.0, 0.25, 0.0],
                             [2.5, -0.25,0.0],
                             [2.5, 0.25, 0.0],
                             [2.0, 0.75, 0.0]],
                            [[2.0, 0.75, 0.0],
                             [2.5, 0.25, 0.0],
                             [2.5, 0.75, 0.0],
                             [2.0, 1.25, 0.0]]])
  
  
  
  valueArray = np.random.rand(9) # Scalar field values for each cell
  
  plt.figure()
  for i in xrange(9):
      plt.plot(facePtsArray[i,:,0], facePtsArray[i,:,1], 'ko-')
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QTErW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QTErW.png" alt="enter image description here"></a></p>