Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib configuration for inline backend in jupyter notebook

I'd like to learn how to configure the defaults for matplotlib using the inline backend in jupyter notebook. Specifically, I'd like to set default 'figure.figsize’ to [7.5, 5.0] instead of the default [6.0, 4.0]. I’m using jupyter notebook 1.1 on a Mac with matplotlib 1.4.3.

In the notebook, using the macosx backend, my matplotlibrc file is shown to be in the standard location, and figsize is set as specified in matplotlibrc:

In [1]: %matplotlib
Using matplotlib backend: MacOSX

In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'

In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[7.5, 5.0]

However, when I use the inline backend, figsize is set differently:

In [1]: %matplotlib inline

In [2]: mpl.matplotlib_fname()
Out[2]: u'/Users/scott/.matplotlib/matplotlibrc'

In [3]: matplotlib.rcParams['figure.figsize']
Out[3]:[6.0, 4.0]

In my notebook config file, ~/.jupyter/jupyter_notebook_config.py, I also added the line

c.InlineBackend.rc = {'figure.figsize': (7.5, 5.0) }

but this had no effect either. For now I’m stuck adding this line in every notebook:

matplotlib.rcParams['figure.figsize']=[7.5, 5.0]

Is there any way to set the default for the inline backend?

like image 580
Scott Calabrese Barton Avatar asked Mar 11 '16 12:03

Scott Calabrese Barton


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 inline backend in matplotlib?

%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.


Video Answer


1 Answers

The Jupyter/IPython split is confusing. Jupyter is the front end to kernels, of which IPython is the defacto Python kernel. You are trying to change something related to matplotlib and this only makes sense within the scope of the IPython kernel. Making a change to matplotlib in ~/.jupyter/jupyter_notebook_config.py would apply to all kernels which may not make sense (in the case of running a Ruby/R/Bash/etc. kernel which doesn't use matplotlib). Therefore, your c.InlineBackend.rc setting needs to go in the settings for the IPython kernel.

Edit the file ~/.ipython/profile_default/ipython_kernel_config.py and add to the bottom: c.InlineBackend.rc = { }.

Since c.InlineBackend.rc specifies matplotlib config overrides, the blank dict tells the IPython kernel not to override any of your .matplotlibrc settings.

If the file doesn't exist, run ipython profile create to create it.

like image 125
bkanuka Avatar answered Oct 07 '22 14:10

bkanuka