# Plot cross-section of 3D mesh in pyvista

**URL:** <https://fenicsproject.discourse.group/t/plot-cross-section-of-3d-mesh-in-pyvista/12958>\
**Category:** I/O\
**Created:** [December 6, 2023, 10:38am UTC](https://fenicsproject.discourse.group/t/plot-cross-section-of-3d-mesh-in-pyvista/12958 "2023-12-06T10:38:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [December 6, 2023, 10:38am UTC](https://fenicsproject.discourse.group/t/plot-cross-section-of-3d-mesh-in-pyvista/12958/1 "2023-12-06T10:38:09Z")

</div>

Dear community, i have the following poisson problem defined on a 3D cubic mesh:

```auto
import numpy as np
from mpi4py import MPI
from dolfinx.mesh import *
from dolfinx import fem, mesh, plot
from dolfinx.fem.petsc import LinearProblem
import ufl
from ufl import dx, grad, inner

# mesh, space
msh = create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [1, 1, 1]], [20,20,20], CellType.hexahedron)
V = fem.functionspace(msh, ("Lagrange", 1))

# BC
facets = mesh.locate_entities_boundary(msh, dim=1,
                                       marker=lambda x: np.logical_or.reduce((
                                           np.isclose(x[0], 0.0),
                                           np.isclose(x[0], 1.0),
                                           np.isclose(x[1], 0.0),
                                           np.isclose(x[1], 1.0),
                                           np.isclose(x[2], 0.0),
                                           np.isclose(x[2], 1.0))))
dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)
bc = fem.dirichletbc(0.0, dofs=dofs, V=V)

# Next, the variational problem is defined:
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = 10
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx

# linear problem brings together var probl, BC and solver (here: LU) 
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()

# plots
import pyvista
cells, types, x = plot.vtk_mesh(V)
grid = pyvista.UnstructuredGrid(cells, types, x)
grid.point_data["u"] = uh.x.array.real
grid.set_active_scalars("u")
plotter = pyvista.Plotter()
plotter.add_mesh(grid, show_edges=True)
plotter.show()

```

How can I plot a cross section through a defined plane instead of the outside of the cube (which obviously shows nothing but the boundary values)?

Thank you!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 6, 2023, 10:44am UTC](https://fenicsproject.discourse.group/t/plot-cross-section-of-3d-mesh-in-pyvista/12958/2 "2023-12-06T10:44:36Z")

</div>

See: [Clipping with Planes & Boxes — PyVista 0.42.3 documentation](https://docs.pyvista.org/version/stable/examples/01-filter/clipping.html)

---

<div class="post-metadata">

**Author:** ![renol-kth](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@renol-kth](https://fenicsproject.discourse.group/u/renol-kth)\
**Post date:** [December 6, 2023, 11:55am UTC](https://fenicsproject.discourse.group/t/plot-cross-section-of-3d-mesh-in-pyvista/12958/3 "2023-12-06T11:55:31Z")

</div>

Thank you, this was helpful.

For reference, this is the code that shows the cross section:

```auto
import numpy as np
from mpi4py import MPI
from dolfinx.mesh import *
from dolfinx import fem, mesh, plot
from dolfinx.fem.petsc import LinearProblem
import ufl
from ufl import dx, grad, inner

# mesh, space
msh = create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [1, 1, 1]], [20,20,20], CellType.hexahedron)
V = fem.functionspace(msh, ("Lagrange", 1))

# BC
facets = mesh.locate_entities_boundary(msh, dim=1,
                                       marker=lambda x: np.logical_or.reduce((
                                           np.isclose(x[0], 0.0),
                                           np.isclose(x[0], 1.0),
                                           np.isclose(x[1], 0.0),
                                           np.isclose(x[1], 1.0),
                                           np.isclose(x[2], 0.0),
                                           np.isclose(x[2], 1.0))))
dofs = fem.locate_dofs_topological(V=V, entity_dim=1, entities=facets)
bc = fem.dirichletbc(1.0, dofs=dofs, V=V)

# Next, the variational problem is defined:
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = 10
a = inner(grad(u), grad(v)) * dx
L = inner(f, v) * dx

# linear problem brings together var probl, BC and solver (here: LU) 
problem = LinearProblem(a, L, bcs=[bc], petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()

# plots
import pyvista
import matplotlib as mpl
cells, types, x = plot.vtk_mesh(V)
grid = pyvista.UnstructuredGrid(cells, types, x)
grid.point_data["u"] = uh.x.array.real
grid.set_active_scalars("u")
plotter = pyvista.Plotter()
clipped = grid.clip('y', invert=True)
plotter.add_mesh(grid, style='wireframe')
plotter.add_mesh(clipped)
plotter.show()

```

Calling `add_mesh()` with wireframe style makes the interior visible.
