ipympl icon indicating copy to clipboard operation
ipympl copied to clipboard

Blank figures in Jupyter notebook

Open btlorch opened this issue 1 year ago • 22 comments

I have happily used interactive plots in jupyter notebooks for years with %matplotlib notebook. Unfortunately, since version 7 was introduced, I have experienced troubles with getting interactive plotting to work properly. I read that version 7 requires installing ipympl and %matplotlib ipympl.

Today, I set up a fresh virtual environment and installed jupyter and ipympl. Figures are opening, the controls appear, a figure title appears, but the figure contains no content. There is also no error message on the browser's developer console.

Steps to reproduce:

# Create virtual environment
python3 -m venv debug
source debug/bin/activate

# Install jupyter and ipympl
pip install jupyter ipympl matplotlib numpy

# Open notebook
jupyter notebook

Then I use the code from the basic example

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

fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(3*x)
ax.plot(x, y)

A white area is opening, the controls appear, but there is no figure content.

I have seen several similar issues on Stackoverflow, where the solution is usually to upgrade or downgrade one of the packages. However, these answers have aged as newer versions came out in the meantime. Is there a general recipe to identify the issue when interactive plotting fails?

Versions

Python: 3.12.3
ipympl version: 0.9.4
IPython          : 8.25.0
ipykernel        : 6.29.4
ipywidgets       : 8.1.3
jupyter_client   : 8.6.2
jupyter_core     : 5.7.2
jupyter_server   : 2.14.1
jupyterlab       : 4.2.1
nbclient         : 0.10.0
nbconvert        : 7.16.4
nbformat         : 5.10.4
notebook         : 7.2.0
qtconsole        : 5.5.2
traitlets        : 5.14.3
jupyter labextension list 
JupyterLab v4.2.1
/home/bene/local/venvs/debug/share/jupyter/labextensions
        jupyterlab_pygments v0.3.0 enabled OK (python, jupyterlab_pygments)
        jupyter-matplotlib v0.11.4 enabled OK
        @jupyter-widgets/jupyterlab-manager v5.0.11 enabled OK (python, jupyterlab_widgets)
        @jupyter-notebook/lab-extension v7.2.0 enabled OK

btlorch avatar Jun 05 '24 08:06 btlorch

Thanks for opening an issue.

A white area is opening, the controls appear, but there is no figure content.

So it looks like ipympl was properly installed and is working with Notebook 7, otherwise you wouldn't see the figure controls.

It's weird though that you are getting a white area. I will try to reproduce.

martinRenou avatar Jun 05 '24 09:06 martinRenou

Thanks, I updated my initial post with the list of extensions. As far as I can see, the versions should be compatible according to the version compatibility table.

btlorch avatar Jun 05 '24 09:06 btlorch

Yes, your installation looks perfectly fine.

What is your matplotlib version?

martinRenou avatar Jun 05 '24 09:06 martinRenou

My matplotlib version is 3.9.0.

btlorch avatar Jun 05 '24 09:06 btlorch

Thanks, I can reproduce your issue.

~~I wonder if this has to do with ipympl not supporting plt.subplot~~ (it is). Friendly ping @ianhi, I suspect you're more familiar than me on this matter.

martinRenou avatar Jun 05 '24 09:06 martinRenou

Wait, I see something really weird (same code, same environment, same server, different notebooks, on the left I can see the plot 100% of the time when executing, on the right I never see it):

Screenshot from 2024-06-05 11-20-29

martinRenou avatar Jun 05 '24 09:06 martinRenou

Hm, I shut down and close the notebook, then reopen it, I can see the figure. When I create another new notebook, there the figure does not show up.

btlorch avatar Jun 05 '24 09:06 btlorch

It seems we have some kind of race condition in the initialization of the plot in the case of Notebook 7. It was never reported in the case of JupyterLab AFAIK.

martinRenou avatar Jun 05 '24 09:06 martinRenou

For reference this seems to be the case in JupyterLab 4.2.2 too:

image

jtpio avatar Jun 12 '24 13:06 jtpio

Friendly ping @ianhi, I suspect you're more familiar than me on this matter.

Sorry all, i've been off github and literally in the lab basement getting the final datasets I need in order finsih grad school.

Unfortunately I've been unable to replicate this with jupyterlab 4.2.3 (or the notebook command). What browser are you using? I'm on firefox

ianhi avatar Jul 08 '24 03:07 ianhi

Hi! I'm also running into this.

ipympl==0.9.4
jupyter-events==0.10.0
jupyter-lsp==2.2.5
jupyter_client==8.6.2
jupyter_core==5.7.2
jupyter_server==2.14.2
jupyter_server_terminals==0.5.3
jupyterlab==4.2.3
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.3
jupyterlab_widgets==3.0.11
matplotlib==3.9.2
matplotlib-inline==0.1.7

I downgraded to jupyterlab 4.2.3 to see if that helped based on @ianhi's comment, but it's still present. It is worth noting that in chromium, I do see the plots, but the javascript console has errors on both browsers. It is also worth noting that it seems to reproduce without subplots being involved at all?

Firefox: screenshot_210824_084726

chromium: screenshot_210824_085102

toygardarm avatar Aug 21 '24 06:08 toygardarm

Unfortunately I've been unable to replicate this with jupyterlab 4.2.3 (or the notebook command). What browser are you using? I'm on firefox

It seems to me that it's random, some kind of race condition. I wonder if simulating a slow connection using the dev tools would help us replicate the issue more easily.

martinRenou avatar Aug 21 '24 07:08 martinRenou

For what it's worth - it does not seem random on my machine. It reproduces reliably in Firefox, both with a local and remote jupyterlab environment. As in, I haven't yet seen it not reproduce! Or - if it /is/ random, my luck is clearly terrible!

toygardarm avatar Aug 21 '24 09:08 toygardarm

For what it's worth, I am having exactly the same problem.

Python 3.12.6
ipympl==0.9.4
IPython          : 8.26.0
ipykernel        : 6.29.5
ipywidgets       : 8.1.5
jupyter_client   : 8.6.2
jupyter_core     : 5.7.2
jupyter_server   : 2.14.2
jupyterlab       : 4.2.5
nbclient         : 0.10.0
nbconvert        : 7.16.1
nbformat         : 5.10.4
notebook         : 7.2.1
qtconsole        : 5.5.2
traitlets        : 5.14.1
  • Creating a new notebook and then trying to use ipympl results in a blank plot, but the widgets display.
  • If I close the browser tab and then reopen, the plot now displays but is inactive: it won't interact with the mouse.
  • Closing the tab and killing the server, then restarting, results in interactive plots working. I can now add new plots etc. and everything works as I expect.
  • Re-created consistently in Firefox and Edge, on Windows 10.

MatthewDaws avatar Feb 06 '25 16:02 MatthewDaws

I wonder if it's related to this issue?

juhaszp95 avatar Apr 17 '25 09:04 juhaszp95

I tried to look into this today, I also don't think it's a race condition as it's reliably reproducible as described in https://github.com/matplotlib/ipympl/issues/556#issuecomment-2640369468.

As mentioned, it happens when a new notebook is created, but if the notebook is later opened again after restarting the server, it doesn't happen anymore. So it has something to do with how the plots are initialized from a previously saved notebook.

I don't know much about Jupyter extension development but I found that the refresh message below is sent (or the handle_resize function called) only in the working case. So when creating a new notebook, either handle_resize is not properly registered or the resize message is not sent.

https://github.com/matplotlib/ipympl/blob/5e068ed222427919cc20c406e34d02ba9be55cc3/src/mpl_widget.ts#L170

mgunyho avatar Jun 27 '25 14:06 mgunyho

The problem seems to be related specifically to the behavior of the "New -> Python 3 (ipykernel)" button in the Jupyter UI. If I create a new notebook via that button, the issue appears. But if I create a new notebook by going to my file browser and copy-pasting a blank notebook with identical contents to the one created from the UI (while the server is running), and open that, it works OK.

mgunyho avatar Jun 27 '25 14:06 mgunyho

Just a quick note to say that I see this issue in VS Code as well - if I create and run a notebook, save it, close it, then open it again (potentially after a VS Code restart as well), the plots don't show but show an error message.

juhaszp95 avatar Jun 27 '25 15:06 juhaszp95