matplotlib / ipympl

Matplotlib Jupyter Integration
https://matplotlib.org/ipympl/
BSD 3-Clause "New" or "Revised" License
1.59k stars 225 forks source link

Figure not rendering #500

Closed dzanaga closed 1 year ago

dzanaga commented 1 year ago

Describe the issue

I have a fresh miniconda installation. On the base environment I have installed jupyter and ipympl. Plotting when using the base kernel works as expected. The problem occurs when I am switching to another kernel. I created a new conda env where I installed matplotlib, ipympl, ipykernel, added the kernel to jupyter. When plotting while using this new kernel, the figure does not render.

In the base env: image

In the other kernel: image

Versions

base env:

(base) [dzanaga@dzanaga:~/Private/repos]$ python3 -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version && jupyter nbextension list && jupyter labextension list

 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 15:55:03) 
[GCC 10.4.0]
ipympl version: 0.9.2
Selected Jupyter core packages...
IPython          : 8.7.0
ipykernel        : 6.17.1
ipywidgets       : 8.0.2
jupyter_client   : 7.4.8
jupyter_core     : 5.1.0
jupyter_server   : 1.23.3
jupyterlab       : 3.5.1
nbclient         : 0.7.2
nbconvert        : 7.2.6
nbformat         : 5.7.0
notebook         : 6.5.2
qtconsole        : not installed
traitlets        : 5.6.0
Known nbextensions:
  config dir: /home/dzanaga/miniconda3/etc/jupyter/nbconfig
    notebook section
      jupyter-matplotlib/extension  enabled 
      - Validating: OK
      jupyter-js-widgets/extension  enabled 
      - Validating: OK
  config dir: /usr/local/etc/jupyter/nbconfig
    notebook section
      plotlywidget/extension  enabled 
      - Validating: OK
JupyterLab v3.5.1
/home/dzanaga/miniconda3/share/jupyter/labextensions
        jupyterlab_pygments v0.2.2 enabled OK (python, jupyterlab_pygments)
        jupyter-matplotlib v0.11.2 enabled OK
        @jupyter-widgets/jupyterlab-manager v5.0.3 enabled OK (python, jupyterlab_widgets)

second env:

(eo42) [dzanaga@dzanaga:~/Private/repos]$ python3 -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version

 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 15:55:03) 
[GCC 10.4.0]
ipympl version: 0.9.2
Selected Jupyter core packages...
IPython          : 8.7.0
ipykernel        : 6.18.3
ipywidgets       : 8.0.2
jupyter_client   : 7.4.8
jupyter_core     : 5.1.0
jupyter_server   : not installed
jupyterlab       : not installed
nbclient         : not installed
nbconvert        : not installed
nbformat         : not installed
notebook         : not installed
qtconsole        : not installed
traitlets        : 5.6.0
dzanaga commented 1 year ago

This setup always worked previously when I was working on python 3.7, here I switched to 3.9. not sure what's going on.

martinRenou commented 1 year ago

Everything looks fine on your setup. Could you try an hard refresh of your web page (ctrl + shift + R) to make sure you're not hitting some browser cache?

dzanaga commented 1 year ago

I tried, but no luck

dzanaga commented 1 year ago

Another detail, not sure it's related, but when I run for the first time %matplotlib ipympl it works, if I re-run it a second time it throws an error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[3], line 4
      2 get_ipython().run_line_magic('reload_ext', 'autoreload')
      3 get_ipython().run_line_magic('autoreload', '2')
----> 4 get_ipython().run_line_magic('matplotlib', 'ipympl')

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2364, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2362     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2363 with self.builtin_trap:
-> 2364     result = fn(*args, **kwargs)
   2365 return result

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/IPython/core/magics/pylab.py:99, in PylabMagics.matplotlib(self, line)
     97     print("Available matplotlib backends: %s" % backends_list)
     98 else:
---> 99     gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100     self._show_matplotlib_backend(args.gui, backend)

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3528, in InteractiveShell.enable_matplotlib(self, gui)
   3524         print('Warning: Cannot change to a different GUI toolkit: %s.'
   3525                 ' Using %s instead.' % (gui, self.pylab_gui_select))
   3526         gui, backend = pt.find_gui_and_backend(self.pylab_gui_select)
-> 3528 pt.activate_matplotlib(backend)
   3529 configure_inline_support(self, backend)
   3531 # Now we must activate the gui pylab wants to use, and fix %run to take
   3532 # plot updates into account

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/IPython/core/pylabtools.py:360, in activate_matplotlib(backend)
    355 # Due to circular imports, pyplot may be only partially initialised
    356 # when this function runs.
    357 # So avoid needing matplotlib attribute-lookup to access pyplot.
    358 from matplotlib import pyplot as plt
--> 360 plt.switch_backend(backend)
    362 plt.show._needmain = False
    363 # We need to detect at runtime whether show() is called by the user.
    364 # For this, we wrap it into a decorator which adds a 'called' flag.

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/matplotlib/pyplot.py:227, in switch_backend(newbackend)
    225 # make sure the init is pulled up so we can assign to it later
    226 import matplotlib.backends
--> 227 close("all")
    229 if newbackend is rcsetup._auto_backend_sentinel:
    230     current_framework = cbook._get_running_interactive_framework()

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/matplotlib/pyplot.py:912, in close(fig)
    910         _pylab_helpers.Gcf.destroy(manager)
    911 elif fig == 'all':
--> 912     _pylab_helpers.Gcf.destroy_all()
    913 elif isinstance(fig, int):
    914     _pylab_helpers.Gcf.destroy(fig)

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/matplotlib/_pylab_helpers.py:82, in Gcf.destroy_all(cls)
     80 for manager in list(cls.figs.values()):
     81     manager.canvas.mpl_disconnect(manager._cidgcf)
---> 82     manager.destroy()
     83 cls.figs.clear()

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/ipympl/backend_nbagg.py:471, in FigureManager.destroy(self)
    470 def destroy(self):
--> 471     self.canvas.close()

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/ipywidgets/widgets/widget.py:500, in Widget.close(self)
    498 if self.comm is not None:
    499     Widget._active_widgets.pop(self.model_id, None)
--> 500     self.comm.close()
    501     self.comm = None
    502     self._repr_mimebundle_ = None

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/comm/base_comm.py:109, in BaseComm.close(self, data, metadata, buffers, deleting)
    101 self.publish_msg(
    102     "comm_close",
    103     data=data,
    104     metadata=metadata,
    105     buffers=buffers,
    106 )
    107 if not deleting:
    108     # If deleting, the comm can't be registered
--> 109     comm.get_comm_manager().unregister_comm(self)

File ~/miniconda3/envs/eo42/lib/python3.9/site-packages/comm/base_comm.py:199, in CommManager.unregister_comm(self, comm)
    197 """Unregister a comm, and close its counterpart"""
    198 # unlike get_comm, this should raise a KeyError
--> 199 comm = self.comms.pop(comm.comm_id)

KeyError: '193ed29e4cc442dfbafc849398cd3615'
martinRenou commented 1 year ago

😞 Please try to downgrade ipykernel to 6.17.1...

dzanaga commented 1 year ago

yep, that fixed it, thanks for figuring it out!