In Jupyter how to set `%matplotlib widget` for all notebooks?
Feb 22, 11:59am

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:

1. Locate or Create the IPython Profile Configuration File

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.

2. Edit the Configuration File

Open the file in a text editor and add or modify the following line:

python
c.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.

3. Alternative: Use a Startup Script

If you don’t want to edit the configuration file, you can create a startup script to set the matplotlib backend for all notebooks:

  1. Navigate to your IPython profile directory:

    bash
    jupyter --config-dir

    This will show you the location of your Jupyter config directory.

  2. Go to the startup folder inside the profile directory (e.g., ~/.ipython/profile_default/startup/). If the startup folder doesn’t exist, create it.

  3. Create a new Python file, e.g., 00-matplotlib-widget.py, and add the following line:

    python
    %matplotlib widget
  4. Save the file. Now, every time you start a Jupyter notebook, the %matplotlib widget magic command will be executed automatically.

4. Restart Jupyter

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:

bash
jupyter labextension install @jupyter-widgets/jupyterlab-manager
Feb 22, 11:59am

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.