Automatically run %matplotlib inline in IPython Notebook

8one6 picture 8one6 · Jan 17, 2014 · Viewed 111.5k times · Source

Every time I launch IPython Notebook, the first command I run is

%matplotlib inline

Is there some way to change my config file so that when I launch IPython, it is automatically in this mode?

Answer

Kyle Kelley picture Kyle Kelley · Jan 17, 2014

The configuration way

IPython has profiles for configuration, located at ~/.ipython/profile_*. The default profile is called profile_default. Within this folder there are two primary configuration files:

  • ipython_config.py
  • ipython_kernel_config.py

Add the inline option for matplotlib to ipython_kernel_config.py:

c = get_config()
# ... Any other configurables you want to set
c.InteractiveShellApp.matplotlib = "inline"

matplotlib vs. pylab

Usage of %pylab to get inline plotting is discouraged.

It introduces all sorts of gunk into your namespace that you just don't need.

%matplotlib on the other hand enables inline plotting without injecting your namespace. You'll need to do explicit calls to get matplotlib and numpy imported.

import matplotlib.pyplot as plt
import numpy as np

The small price of typing out your imports explicitly should be completely overcome by the fact that you now have reproducible code.