Plot functions in dolfinx

I have not experienced that issue.
Here is my minimal example (using ubuntu 20.04 locally), where both interactive or non-interactive plotting works using docker:

#On your own system:
xhost + 

docker run -ti -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $(pwd):/home/shared -w /home/shared --name=name_of_container --shm-size=512m  dolfinx/dolfinx
# Inside docker container 
apt-get update
apt-get install -y --no-install-recommends libgl1-mesa-dev xvfb
pip3 install pyvista
# Change to True if you only want to save a png
echo 'export PYVISTA_OFF_SCREEN=False' >> ~/.bashrc
exec bash

# After exiting container
xhost -

# Next time you use the container
xhost + 
docker container start -i name_of_container

Minimal python code:

import dolfinx
import dolfinx.plot
import numpy as np
from mpi4py import MPI

try:
    import pyvista
except ModuleNotFoundError:
    print("pyvista is required for this demo")
    exit(0)

if pyvista.OFF_SCREEN:
    pyvista.start_xvfb(wait=0.1)


N = 2
mesh = dolfinx.UnitSquareMesh(MPI.COMM_WORLD, N, N)

# Extract mesh data from dolfin-X (only plot cells owned by the
# processor) and create a pyvista UnstructuredGrid
num_cells = mesh.topology.index_map(mesh.topology.dim).size_local
cell_entities = np.arange(num_cells, dtype=np.int32)
pyvista_cells, cell_types = dolfinx.plot.create_vtk_topology(
    mesh, mesh.topology.dim, cell_entities)
grid = pyvista.UnstructuredGrid(pyvista_cells, cell_types, mesh.geometry.x)

plotter = pyvista.Plotter()
plotter.add_mesh(grid, style="wireframe", line_width=2, color="black")
plotter.view_xy()
# Save as png if we are using a container with no rendering
if pyvista.OFF_SCREEN:
    plotter.screenshot("tmp.png")
else:
    plotter.show()

2 Likes