How to fix %matplotlib widget RuntimeError: 'widget' is not a recognised GUI loop or backend name

Problem

When using %matplotlib widget in a Jupyter Notebook, you encounter the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~/.local/lib/python3.12/site-packages/matplotlib/backends/registry.py:407, in BackendRegistry.resolve_gui_or_backend(self, gui_or_backend)
    406 try:
--> 407     return self.resolve_backend(gui_or_backend)
    408 except Exception:  # KeyError ?

File ~/.local/lib/python3.12/site-packages/matplotlib/backends/registry.py:369, in BackendRegistry.resolve_backend(self, backend)
    368 if gui is None:
--> 369     raise RuntimeError(f"'{backend}' is not a recognised backend name")
    371 return backend, gui if gui != "headless" else None

RuntimeError: 'widget' is not a recognised backend name

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
Cell In[1], line 2
      1 # Enable interactive matplotlib plots
----> 2 get_ipython().run_line_magic('matplotlib', 'widget')

File ~/.local/lib/python3.12/site-packages/IPython/core/interactiveshell.py:2480, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2478     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2479 with self.builtin_trap:
-> 2480     result = fn(*args, **kwargs)
   2482 # The code below prevents the output from being displayed
   2483 # when using magics with decorator @output_can_be_silenced
   2484 # when the last Python token in the expression is a ';'.
   2485 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~/.local/lib/python3.12/site-packages/IPython/core/magics/pylab.py:103, in PylabMagics.matplotlib(self, line)
     98     print(
     99         "Available matplotlib backends: %s"
    100         % _list_matplotlib_backends_and_gui_loops()
    101     )
    102 else:
--> 103     gui, backend = self.shell.enable_matplotlib(args.gui)
    104     self._show_matplotlib_backend(args.gui, backend)

File ~/.local/lib/python3.12/site-packages/IPython/core/interactiveshell.py:3665, in InteractiveShell.enable_matplotlib(self, gui)
   3662     import matplotlib_inline.backend_inline
   3664 from IPython.core import pylabtools as pt
-> 3665 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3667 if gui != None:
   3668     # If we have our first gui selection, store it
   3669     if self.pylab_gui_select is None:

File ~/.local/lib/python3.12/site-packages/IPython/core/pylabtools.py:349, in find_gui_and_backend(gui, gui_select)
    347 else:
    348     gui = _convert_gui_to_matplotlib(gui)
--> 349     backend, gui = backend_registry.resolve_gui_or_backend(gui)
    351 gui = _convert_gui_from_matplotlib(gui)
    352 return gui, backend

File ~/.local/lib/python3.12/site-packages/matplotlib/backends/registry.py:409, in BackendRegistry.resolve_gui_or_backend(self, gui_or_backend)
    407     return self.resolve_backend(gui_or_backend)
    408 except Exception:  # KeyError ?
--> 409     raise RuntimeError(
    410         f"'{gui_or_backend}' is not a recognised GUI loop or backend name")

RuntimeError: 'widget' is not a recognised GUI loop or backend name

Solution

You need to install the ipympl package, which provides the %matplotlib widget backend for Jupyter Notebooks. You can install it using pip:

pip install -U ipympl