# 2D slice from 3D solution

**URL:** <https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808>\
**Category:** Uncategorized\
**Created:** [March 9, 2022, 10:06am UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808 "2022-03-09T10:06:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ingineering\_DjouFEA](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ingineering_djoufea/32/3507_2.png) [@Ingineering\_DjouFEA](https://fenicsproject.discourse.group/u/Ingineering_DjouFEA)\
**Post date:** [March 9, 2022, 10:06am UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/1 "2022-03-09T10:06:10Z")

</div>

Hello everyone,  
I need your help. Indeed I solved a transient problem of soft tissue mechanics on a 3D slab. However, I would like to have the solution on an xy plane of the domain for z=0 and in the middle (z=0.5).  
I have used some solutions that I have found on the internet but, I get no result.  
I have mainly tested the solutions in the links:

[https://fenicsproject.org/qa/3257/building-2d-function-g-x-y-from-a-3d-function-f-x-y-z-constant/](https://fenicsproject.org/qa/3257/building-2d-function-g-x-y-from-a-3d-function-f-x-y-z-constant/)

> **[Interpolate nonmatching mesh · mikaem/fenicstools Wiki](https://github.com/mikaem/fenicstools/wiki/Interpolate-nonmatching-mesh)**
>
> Contribute to mikaem/fenicstools development by creating an account on GitHub.

> **[Interpolation nonmatching mesh in parallel · mikaem/fenicstools Wiki](https://github.com/mikaem/fenicstools/wiki/Interpolation-nonmatching-mesh-in-parallel)**
>
> Contribute to mikaem/fenicstools development by creating an account on GitHub.

Thank you in advance.

```auto
# Create a 2D unit square mesh from the boundary of mesh3D
mesh2 = BoundaryMesh(mesh3D, "exterior")
side = MeshFunction("size_t", mesh2, 0)
xyplane = AutoSubDomain(lambda x: x[2] < 1e-8)
xyplane.mark(side, 1)
mesh3 = SubMesh(mesh2, side, 1)
x = mesh3.coordinates()
x[:, 2] += 0.5
VH2 = FunctionSpace(mesh3, "DG", 1)
# solution obtained from mechanical problem

u,p=up.split()

# Interpolation for 2D slice from 3D

u1 = interpolate(u, VH2)

```

---

<div class="post-metadata">

**Author:** ![DGarciaLahuerta](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dgarcialahuerta/32/2673_2.png) [@DGarciaLahuerta](https://fenicsproject.discourse.group/u/DGarciaLahuerta)\
**Post date:** [March 9, 2022, 2:22pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/2 "2022-03-09T14:22:13Z")

</div>

You can try defining an expression that you can interpolate to get rid of your third axis.

Something like this:

```auto
#Dummy problem

from fenics import *
import matplotlib.pyplot as plt

# Create mesh and define function space
mesh = UnitCubeMesh(8, 8, 8)
V = FunctionSpace(mesh, 'CG', 1)

# Define boundary condition
u_D = Constant(0)

def boundary(x, on_boundary):
    return on_boundary

bc = DirichletBC(V, u_D, boundary)

# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(100)
a = dot(grad(u), grad(v))*dx
L = f*v*dx

# Compute solution
u = Function(V)
solve(a == L, u, bc)

# Plot solution and mesh
plt.colorbar(plot(u))
plt.title('u')

```

![full_sol](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/3/3eccd80556e45be306428b2f6582331929a08b53.png)

Define an expression to evaluate `u` at some value of z:

```auto
class u_slice(UserExpression):
    def eval(self, value, x):

        value[0] = u(x[0],x[1],0.5)

    def value_shape(self):
        return () 

mesh_2D = UnitSquareMesh(8, 8)
V_2D = FunctionSpace(mesh_2D, 'CG', 1)
u_sl = interpolate(u_slice(), V_2D)
plt.colorbar(plot(u_sl))
plt.title('u in the middle plane')

```

![plane_sol](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/7c29445d59f009a588f4dc73bcd2bbaffd98421d.png)

---

<div class="post-metadata">

**Author:** ![Ingineering\_DjouFEA](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/ingineering_djoufea/32/3507_2.png) [@Ingineering\_DjouFEA](https://fenicsproject.discourse.group/u/Ingineering_DjouFEA)\
**Post date:** [March 9, 2022, 3:02pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/3 "2022-03-09T15:02:21Z")

</div>

Hi  
Thank you very much for your reply.  
I managed to do it differently. And it and your code work perfectly.  
Thank you.

---

<div class="post-metadata">

**Author:** ![RemDelaporteMathurin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/remdelaportemathurin/32/4543_2.png) [@RemDelaporteMathurin](https://fenicsproject.discourse.group/u/RemDelaporteMathurin)\
**Post date:** [June 30, 2023, 7:33pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/4 "2023-06-30T19:33:48Z")

</div>

Hi!

Thanks for this!

A few questions:

1. Plotting 3D functions with fenics doesn’t seem possible anymore  
Your code yields:

```auto
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[2], line 30
     27 solve(a == L, u, bc)
     29 # Plot solution and mesh
---> 30 plt.colorbar(plot(u))
     31 plt.title('u')

File /opt/conda/lib/python3.10/site-packages/dolfin/common/plotting.py:438, in plot(object, *args, **kwargs)
    436 # Plot
    437 if backend == "matplotlib":
--> 438 return _plot_matplotlib(object, mesh, kwargs)
    439 elif backend == "x3dom":
    440 return _plot_x3dom(object, kwargs)

File /opt/conda/lib/python3.10/site-packages/dolfin/common/plotting.py:279, in _plot_matplotlib(obj, mesh, kwargs)
    277 from mpl_toolkits.mplot3d import axes3d # noqa
    278 # Enabling the 3d toolbox requires some additional arguments
--> 279 ax = plt.gca(projection='3d')
    280 else:
    281 ax = plt.gca()

TypeError: gca() got an unexpected keyword argument 'projection'

```

1. Is there a way to do what you did using a SubMesh of the 3D mesh?

---

<div class="post-metadata">

**Author:** ![Ekrem\_Ekici](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@Ekrem\_Ekici](https://fenicsproject.discourse.group/u/Ekrem_Ekici)\
**Post date:** [July 2, 2023, 10:07am UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/5 "2023-07-02T10:07:54Z")

</div>

You can also save the solution as a .pvd or .xdmf file and read it back in paraview. Then you use the slice filter and export the 2D data as a .csv file. Then you can read and manipulate it with pandas.

---

<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:** [July 2, 2023, 11:52am UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/6 "2023-07-02T11:52:21Z")

</div>

Try downgrading the matplotlib version.

---

<div class="post-metadata">

**Author:** ![RemDelaporteMathurin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/remdelaportemathurin/32/4543_2.png) [@RemDelaporteMathurin](https://fenicsproject.discourse.group/u/RemDelaporteMathurin)\
**Post date:** [July 2, 2023, 1:43pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/7 "2023-07-02T13:43:41Z")

</div>

Yep should work. However this should be downgrading to a very old version of matplotlib. Any chance this is another case where we should PR to dolfin to fix the deprecated syntax?

---

<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:** [July 2, 2023, 5:59pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/8 "2023-07-02T17:59:29Z")

</div>

Its fine to make a PR to update it, we can get that merged rather quickly. Just note that only minor patches to external packages will be made.

If you want 3D plotting I would recommend vedo or pyvista (not matplotlib). I’ve made a recent extension to interface with pyvista

> **[GitHub - jorgensd/dolfin\_pyvista\_adapter: Adapter for Pyvista for arbitrary...](https://github.com/jorgensd/dolfin_pyvista_adapter)**
>
> Adapter for Pyvista for arbitrary order (discontinuous) Lagrange with Legacy Dolfin. - GitHub - jorgensd/dolfin\_pyvista\_adapter: Adapter for Pyvista for arbitrary order (discontinuous) Lagrange wit...

[http://jsdokken.com/dolfin\_pyvista\_adapter/docs/demo.html](http://jsdokken.com/dolfin_pyvista_adapter/docs/demo.html)

---

<div class="post-metadata">

**Author:** ![RemDelaporteMathurin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/remdelaportemathurin/32/4543_2.png) [@RemDelaporteMathurin](https://fenicsproject.discourse.group/u/RemDelaporteMathurin)\
**Post date:** [July 3, 2023, 1:46pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/9 "2023-07-03T13:46:25Z")

</div>

Thanks for this Jorgen!

I know matplotlib is not ideal in this situation.  
I’m trying to have 3D visualisations in a jupyter notebook in a Github Codespace for a workshop.  
I’ve given a go at vedo but couldn’t get it to work in Codespaces.

Does dolfin\_pyvista\_adapter work in jupyter notebooks?

---

<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:** [July 3, 2023, 1:51pm UTC](https://fenicsproject.discourse.group/t/2d-slice-from-3d-solution/7808/10 "2023-07-03T13:51:25Z")

</div>

Yes, as shown in [dolfin\_pyvista\_adapter/docs/demo.ipynb at main · jorgensd/dolfin\_pyvista\_adapter · GitHub](https://github.com/jorgensd/dolfin_pyvista_adapter/blob/main/docs/demo.ipynb)
