To set %matplotlib widget as the default backend for all Jupyter notebooks, you need to configure the IPython profile. Here’s how you can do it:
The configuration file is usually located in:
~/.ipython/profile_default/ipython_config.py (Linux/Mac)C:\Users\<YourUsername>\.ipython\profile_default\ipython_config.py (Windows)If the file doesn’t exist, you can create it.
Open the file in a text editor and add or modify the following line:
pythonc.InlineBackend.figure_format = 'retina' c.InlineBackend.rc = {'figure.figsize': (6.0, 4.0)} c.InteractiveShell.matplotlib = 'widget'
If the file doesn’t exist, you can create it and add the above lines.
If you don’t want to edit the configuration file, you can create a startup script to set the matplotlib backend for all notebooks:
Navigate to your IPython profile directory:
bashjupyter --config-dir
This will show you the location of your Jupyter config directory.
Go to the startup folder inside the profile directory (e.g., ~/.ipython/profile_default/startup/). If the startup folder doesn’t exist, create it.
Create a new Python file, e.g., 00-matplotlib-widget.py, and add the following line:
python%matplotlib widget
Save the file. Now, every time you start a Jupyter notebook, the %matplotlib widget magic command will be executed automatically.
After making these changes, restart your Jupyter notebook server for the changes to take effect.
Note: If you’re using JupyterLab, ensure that the @jupyter-widgets/jupyterlab-manager extension is installed, as it’s required for the widget backend to work. You can install it with:
bashjupyter labextension install @jupyter-widgets/jupyterlab-manager