Facing problem in 3D plotting

Hi,

How to create a 3D plot for a time-dependent PDE in a 2D domain using FEniCS? Please provide some link or syntax

from dolfin import *
import numpy as np
import math
from mpl_toolkits.mplt3d import Axes3D
import matplotlib.pyplot as plt

#Plot the solution in 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = mesh.coordinates()[:,0]
y = mesh.coordinates()[:,1]
u = u_n.compute_vertex_values(mesh)
ax.plot_trisurf(x, y, u, cmap='viridis')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('u')
plt.show()    

I used this syntax for 3D plotting but it does not work.
(Run time error)

   from mpl_toolkits.mplt3d import Axes3D
ModuleNotFoundError: No module named 'mpl_toolkits.mplt3d'

that’s not a question related to Fenicsx but a question about the usage of the matplotlib library.

I suggest you reading this link

1 Like

In any case, for three-dimensional plots I suggest you to use either PyVista or Paraview, but not matplotlib.

1 Like

Thank you for your response. I have successfully plotted the approximate solution u_h in Fenics, but I am having trouble plotting the exact solution. The exact solution is given by u(x,y,t)=t^2\sin(\pi x)\sin(\pi y). Could you please provide me with the syntax or any reference that could help me plot the solution?

I would create a dolfin.Expression with the relevant solution, say

t =0.5
u_exact = Expression("t*t*sin(pi*x[0])*cos(pi*x[1])", degree=3, t=t)
V = FunctionSpace(mesh, "Lagrange", 3)
u_ex = Function(V)
u_ex.interpolate(u_exact)

and save the solution with XDMFFile.write_checkpoint.

1 Like