Segmentation fault upon importing matplotlib into a dolfinx

Merely importing matplotlib into any file in my fenicsx conda environment on my machine was causing a segmentation fault. Here is the MWE code :

import matplotlib.pyplot as plt

import dolfinx


and here was the error:

[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see
[0]PETSC ERROR: or try on GNU/linux and Apple MacOS to find memory corruption errors
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run 
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
application called MPI_Abort(MPI_COMM_WORLD, 59) - process 0
[unset]: write_line error; fd=-1 buf=:cmd=abort exitcode=59
system msg for write_line failure : Bad file descriptor

I’m on Ubuntu 22.04.1 LTS, using dolfinx 0.5.2 (python build #: py310h0d9d84b_100 from conda-forge) and matplotlib 3.6.2 (pypi package).

It seems that I had pip installed matplotlib, so I pip uninstalled matplotlib, then installed with conda via:

conda install -c conda-forge matplotlib

This fixed my issue. I’m not understanding why this pip vs conda install seems to make so much of a difference, but felt like these types of errors are not uncommon for people to see at some point using FEniCS or FEniCSx. Any explanation of why this was happening would be appreciated!

Mixing pip and conda is not adviced, as they are based on different binaries, see: