Kernel dead in jupyter notebook, matplotlib seems not to work

Solution 1:

If you don't want to mess up the matplotlib that you already have installed in Anaconda, you can install second instance of matplotlib inside a Python virtual environment (virtualenv) alongside the matplotlib that is installed in Anaconda. Installing Jupyter, matplotlib and whatever else you need with pip is easy and straightforward in virtualenv.

virtualenv allows you to create a sandboxed and isolated environment where Python packages can be installed without interfering with other packages on the same machine.

  1. Install Python virtual environment creator (virtualenv):

    sudo apt install python-virtualenv virtualenv # still works in 22.04 
    
  2. Make a new directory (I'll call it PythonVirtualEnv in this example) for the Python virtual environment and setup the Python virtual environment with Python and pip in it.

    cd ~  
    mkdir PythonVirtualEnv
    virtualenv PythonVirtualEnv 
    
  3. Install some packages.

    cd ~/PythonVirtualEnv  
    source bin/activate
    python -m pip install jupyter matplotlib 
    
  4. Deactivate the Python virtual environment before leaving it.

    deactivate  
    

Creating an environment with a custom Python interpreter

sudo apt install python3-virtualenv 
cd ~  
mkdir Python3VirtualEnv
virtualenv --python=/usr/bin/python3 Python3VirtualEnv # /usr/bin/python3 is the default location of the python3 executable
cd ~/Python3VirtualEnv  
source bin/activate
python3 -m pip install jupyter matplotlib