Matplotlib display problems in Jupyter

If you encounter matplotlib display problems in Jupyter lab or notebooks, this may be due to environment package errors if you are using conda or vitualenv. Commonly this error shows as ‘ModuleNotFoundError: No module named ‘ipympl’’ despite ipympl being installed in your active environment.

Python kernels can be displayed and easily changed within Jupyter by installing the kernelspec; this is easily done with conda using the nb_conda_kernels package. However some care must be taken to make sure that all is playing nicely together.

Consider the following I am running through a conda Python kernel, and notice how there is no displayed plot, despite the fact I have ipympl installed in my environment:

No errors, but also no displayed plot

This problem can cause many headaches.

In fact the problem stems from the fact that I initialised my notebook from a different conda environment, in this case the base environment, and if I check the installed packages in that environment:

ipympl is not installed in the environment Jupyter was initialised from

we see that ipympl is not installed in the environment Jupyter was initialised from!

Once ipympl is correctly installed in the initial environment (conda install -n base ipympl, in this case) and the Jupyter instance is restarted, we can view our plots in any environment, here is now the working example in my Python 3.9 envionment:

After installing ipympl in the initial environment (commonly base) and restarting Jupyter, plots are correctly displayed

Researcher, coder