pythonmatplotlibjupyter-notebookjupyter-labmatplotlib-widget

matplotlib widget disappears after first use


I'm trying once more to use interactive matplotlib plots in Jupyter Notebooks for my students. My plan is to use JupyterLab as the plain Notebook interface is not very well liked among students. Here is a two-cell MWE notebook:

import numpy as np
%matplotlib widget
import matplotlib.pyplot as plt

Next cell:

plt.figure(1)
x = np.arange(100)
y = x*x
plt.plot(x,y)
plt.show()

When I run these cells I do get an interactive Matplotlib plot. But when I run the second cell a second time, the plot window vanishes without a warning or error and only comes back when I rerun the first cell before the second cell. The classic notebook interface shows the same behaviour, dropping plt.show() or plt.figure() also makes no difference.

I'm running the Jupyter Server locally on a Windows 10 machine in a venv environment, the following versions are installed:

Python           : 3.8.2
ipympl           : 0.7.0

jupyter core     : 4.7.1
jupyter-notebook : 6.3.0
qtconsole        : not installed
ipython          : 7.23.1
ipykernel        : 5.5.4
jupyter client   : 6.1.12
jupyter lab      : 3.0.14
nbconvert        : 6.0.7
ipywidgets       : 7.6.3
nbformat         : 5.1.3
traitlets        : 5.0.5

To my non-expert eyes the messages during startup seem to be ok:

[I 2021-05-12 10:10:48.065 LabApp] JupyterLab extension loaded from d:\envs\pyfda_38\lib\site-packages\jupyterlab
[I 2021-05-12 10:10:48.065 LabApp] JupyterLab application directory is D:\envs\pyfda_38\share\jupyter\lab
[I 2021-05-12 10:10:48.069 ServerApp] jupyterlab | extension was successfully loaded.
[I 2021-05-12 10:10:48.488 ServerApp] nbclassic | extension was successfully loaded.
[I 2021-05-12 10:10:48.489 ServerApp] Serving notebooks from local directory: D:\Daten\xxx
[I 2021-05-12 10:10:48.489 ServerApp] Jupyter Server 1.6.4 is running at:
[I 2021-05-12 10:10:48.489 ServerApp] http://localhost:8888/lab?token=xxxx

The only (maybe) relevant warning I get is

[W 2021-05-12 10:10:55.256 LabApp] Could not determine jupyterlab build status without nodejs 

Am I doing something wrong or are interactive plots with ipympl not yet mature enough for a BYOD course?


Solution

  • It works when activate the matplotlib interactive support every time by moving the magic command into the second cell:

    %matplotlib widget
    plt.figure(1)
    x = np.arange(100)
    y = x*x
    plt.plot(x,y)
    plt.show()