# Paraview vs OpenFOAM vtp slice plotting with matplotlib

**URL:** https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941
**Category:** ParaView Support
**Created:** [December 7, 2022, 7:32pm UTC](https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941 "2022-12-07T19:32:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Gabriel\_Axtmann](https://discourse.paraview.org/user_avatar/discourse.paraview.org/gabriel_axtmann/32/1428_2.png) [@Gabriel\_Axtmann](https://discourse.paraview.org/u/Gabriel_Axtmann)
#### Post date: [December 7, 2022, 7:32pm UTC](https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941/1 "2022-12-07T19:32:12Z")

</div>

Hi

maybe you can help me out with a right comment or hint for my problem.

Pretty easy, I would like to plot a 2D slice vtp file (OpenFOAM) via matplotlib as tricontourf plot.

1.) Creating the vtp slice by Paraview and saving as vtp file works like a charme

2.) Using the runtime vtp file, created by cuttingPlane - libsampling OpenFOAM creates a weird triangle order.

In the attachement you can find an example, including the plotting python script and the data loader.

What am I missing?

Best,

```auto
def loadVTPFile(filename):
    import vtk
    from vtk.util.numpy_support import vtk_to_numpy
    from vtk.util import numpy_support as npvtk
    
    reader = vtk.vtkXMLPolyDataReader()
    reader.SetFileName(filename)
    reader.Update()
    data = reader.GetOutput()
       
    points = data.GetPoints()
    npts = points.GetNumberOfPoints()
    x = vtk_to_numpy(points.GetData())
    
        
    triangles= vtk_to_numpy(data.GetPolys().GetData())
    ntri = triangles.size // 4 # number of cells
    tri = np.take(triangles,[n for n in range(triangles.size) if n%4 != 0]).reshape(ntri,3)
    
    n_arrays = reader.GetNumberOfPointArrays()
    for i in range(n_arrays):
        print(reader.GetPointArrayName(i))
    
    X = vtk_to_numpy(points.GetData())
    x=X[:,0]
    y=X[:,1]
    z=X[:,2]
    
    # Define the velocity components U=(u,v,w)
    U = vtk_to_numpy(data.GetPointData().GetArray('UMean'))
    u = U[:,0]
    v = U[:,1]
    w = U[:,2]
    magU=np.sqrt(u **2+v** 2+w**2)
    
    p = vtk_to_numpy(data.GetPointData().GetArray('pMean'))
    Ma = vtk_to_numpy(data.GetPointData().GetArray('MaMean'))
    rho = vtk_to_numpy(data.GetPointData().GetArray('rhoMean'))    
   

    
    return x,y,z,u,v,w,magU,p,Ma,rho,tri

```

 ![pvCut](https://discourse.paraview.org/uploads/default/original/2X/4/484f0e1ef33c6a3ff86080832f2534a3f62136ad.png)7  
 ![OFCut](https://discourse.paraview.org/uploads/default/original/2X/e/e255e74ed2f6d2575c667265e9c7abe7c8def69b.png)

---

<div class="post-metadata">

### Author: ![Gabriel\_Axtmann](https://discourse.paraview.org/user_avatar/discourse.paraview.org/gabriel_axtmann/32/1428_2.png) [@Gabriel\_Axtmann](https://discourse.paraview.org/u/Gabriel_Axtmann)
#### Post date: [December 17, 2022, 11:34am UTC](https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941/2 "2022-12-17T11:34:39Z")

</div>

No one has an idea?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 19, 2022, 9:33am UTC](https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941/3 "2022-12-19T09:33:06Z")

</div>

This looks like a matplotlib question, not a ParaView question.

---

<div class="post-metadata">

### Author: ![foreverunfree](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/f/a3d4f5/32.png) [@foreverunfree](https://discourse.paraview.org/u/foreverunfree)
#### Post date: [November 9, 2023, 6:45am UTC](https://discourse.paraview.org/t/paraview-vs-openfoam-vtp-slice-plotting-with-matplotlib/10941/4 "2023-11-09T06:45:30Z")

</div>

Hi,

Have you solved the problem? I also want to know how to do the correct triangulation.
