Jupyter-lab pyplot cell re-evaluation breaks interactive widget display

164 views Asked by At

This code works as expected on a cleanly started kernel, but on re-executing the second cell, instead of an interactive widget (ipypml per https://matplotlib.org/3.3.0/users/interactive.html ), I get just the text output as in the image.

How are jupyter, jupyter-lab, widgets, pyplot, and matplotlib interacting to cause this problem?

And how do I properly do a plot so that I can re-execute the cell without re-starting the kernel?

Cell 0:

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

Cell 1:

fig,ax = plt.subplots(num=0)
ax.plot(np.arange(4))

Cell 1 output:

[<matplotlib.lines.Line2D at 0x161b913a0>]

enter image description here

1

There are 1 answers

0
Dave X On

I found that I need to make sure the figure gets cleanly opened (or closed and re-opened) in each update. I had used the plt.subplots(num=0) option to keep repeated updates from creating new, non-displayed figures. (note that the 2nd cell in the screenshot has a been re-executed 5 times and has a [6] prefix)

plt.close(0)
fig,ax = plt.subplots(num=0)
ax.plot(np.arange(4))

enter image description here