Labeling axes of a 3D mesh in jupyter notebook

Dear All,

I know that your first recommendation would be to use Paraview, PyVista or vedo for 3D geometries and meshes. But I really would like to use Jupyter Notebook interface to show a couple of very simple graphs and annotations such as mesh dimensions and some graphs for just a 2D cross-sectional plane (here I am not on this subject).

This is the simple problem. I am trying to put dimensions on a mesh. I’d really appreciate your help. Here is the code I came up with so far:

import matplotlib.pyplot as plt
from dolfin import *
%matplotlib inline

def plot_mesh(u):

    fig = plt.figure(figsize=(5,5))
    fig = plot(u)
    plt.title('Mesh Generated for the Problem Geometry')
    plt.xlabel(r'$x$ in mm')
    plt.ylabel(r'$y$ in mm')
    plt.zlabel(r'$z$ in mm')

Room_Mesh = BoxMesh(Point(0.0, 0.0, 0.0), Point(3, 4, 5), int(3), int(4), int(5))
plot_mesh(Room_Mesh)

I think I need to use:

mpl_toolkits.mplot3d.axes3d as axes3d

but then I don’t know how to tell fenics what are the dimensions of the mesh (X, Y, Z). So please do not consider only this case, my ultimate goal is to do it with a 3D mesh I import from gmsh.

All the best.

Hi,
You have the right idea. Just set the current axes and then simply annotate the current axes object as you like. A quick example that should work is something like

from dolfin import *
mesh = UnitCubeMesh(3,3,3)

# create some data to plot
V=FunctionSpace(mesh, "CG", 2)
x = SpatialCoordinate(mesh)
u = project(sin(x[0]*x[1]*x[2]),V)

# plotting in 3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure(figsize=(4,4))
ax=fig.gca(projection="3d")
plot(u)
ax.set_xlabel(r"x")
ax.set_ylabel(r"y")
ax.set_zlabel(r"z")

However, as you said the best plotting resources in 3D are those based on VTK. So paraview, vedo, pyvista are the best to use for general meshes. See the examples in Vedo for instance.

2 Likes

That solves my problem. Thank you!