Plot cross-section of 3D mesh in pyvista

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

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!

See: Clipping with Planes & Boxes — PyVista 0.42.3 documentation

Thank you, this was helpful.

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

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.