Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically run %matplotlib inline in IPython Notebook

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?

like image 230
8one6 Avatar asked Jan 17 '14 02:01

8one6


People also ask

What is the use of %Matplotlib inline in Jupyter notebook?

You can use the magic function %matplotlib inline to enable the inline plotting, where the plots/graphs will be displayed just below the cell where your plotting commands are written. It provides interactivity with the backend in the frontends like the jupyter notebook.

What is %Matplotlib inline in Python?

%matplotlib inline sets the backend of matplotlib to the 'inline' backend: With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.

Is %Matplotlib Inline still needed?

So %matplotlib inline is only necessary to register this function so that it displays in the output. Running import matplotlib. pyplot as plt also registers this same function, so as of now it's not necessary to even use %matplotlib inline if you use pyplot or a library that imports pyplot like pandas or seaborn.


Video Answer


1 Answers

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.

like image 193
Kyle Kelley Avatar answered Oct 15 '22 08:10

Kyle Kelley