5 Best Ways to Automatically Run Matplotlib Inline in IPython Notebook

Rate this post

πŸ’‘ Problem Formulation: If you’ve used Jupyter Notebooks, you’ve likely come across the need to display your Matplotlib charts inline, directly below your code cells. The goal is to configure your IPython kernel such that it automatically renders Matplotlib figures inline without repeatedly typing %matplotlib inline for every new session. This article provides several methods to achieve this seamless integration, from simple IPython magic commands to permanent configuration changes.

Method 1: Use the %matplotlib inline Magic Command

The %matplotlib inline magic command is used within an IPython environment to render Matplotlib figures directly in the browser. It’s part of the IPython kernel and enables graphical outputs from Matplotlib to be displayed inline within Jupyter Notebooks. It’s straightforward and requires no additional setup.

Here’s an example:

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

The output is a matplotlib figure presented inline in the Jupyter Notebook.

This magic command makes figures appear inline immediately after the cell that produced them. The drawback: it must be run every time you start a new notebook session.

Method 2: Set Inline Backend with IPython Configuration

For users seeking a more permanent solution, IPython’s configuration options enable setting the default backend to ‘inline’ that gets run at the start of every IPython session. This needs to be set up once, and changes persist across sessions.

Here’s an example:

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

# This can be added to your ipython_config.py file:
c = get_config()
c.InteractiveShellApp.matplotlib = 'inline'

With this configuration, the IPython kernel will default to display Matplotlib figures inline.

This method configures IPython at a system level, meaning you don’t need to run %matplotlib inline explicitly. However, altering system-wide configuration might not be preferable for all users.

Method 3: Embed Inline Backend in the Notebook Metadata

Another approach is to embed configuration settings into the metadata of your Jupyter Notebook, dictating that Matplotlib inline backend be used for any session with that specific notebook.

Here’s an example:

{
 "cells": [
  ...
 ],
 "metadata": {
  "kernelspec": {
   ...
  },
  "language_info": {
   ...
  },
  "matplotlib": {
   "inline": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

With this metadata set, Jupyter will automatically use the inline backend for Matplotlib when the notebook is opened.

This method provides the convenience of not running additional commands at the start of each session, but only applies to individual notebooks rather than globally.

Method 4: Startup Files for IPython Profiles

IPython profiles can contain startup files that execute when a new IPython session is started. Creating a startup file dedicated to setting the Matplotlib backend to inline will eliminate the need for manual input each time.

Here’s an example:

# File: ~/.ipython/profile_default/startup/00-inline-matplotlib.py

import matplotlib
matplotlib.use('inline')

Upon initialization of the IPython session, Matplotlib is configured to display figures inline.

This method offers a balance, applying settings at start time without modifying system-wide settings, but is not as granular as configuring individual notebooks.

Bonus One-Liner Method 5: Executing Inline Magic inside a Python Script

For an in-between solution, you may include the inline magic command directly into your Python scripts which are run in a Jupyter Notebook environment, allowing for more dynamic control.

Here’s an example:

get_ipython().run_line_magic('matplotlib', 'inline')

This line of code, when included in a Python script, triggers the IPython environment to render Matplotlib output inline.

This one-liner is a good hack for script-based workflows, but it couples your code to the IPython environment, which may affect portability.

Summary/Discussion

  • Method 1: IPython Magic Command. Easy to use. Non-persistent.
  • Method 2: IPython Configuration. Persistent across sessions. Requires initial setup.
  • Method 3: Notebook Metadata. Configuration is notebook-specific. Transparent to the user.
  • Method 4: IPython Startup Files. Semi-global setting. It does not affect the system-wide configuration.
  • Method 5: Python Script Inline Magic. Dynamic and script-based. Tightly couples code with the Jupyter environment.