20

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?

5 Answers5

31

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.

bkanuka
  • 907
  • 7
  • 18
  • 1
    ipython is gone; the user is talking about jupyter. The config files and their locations are different. – Emre Mar 16 '16 at 01:29
  • 7
    I'm aware. These are the settings for the ipython kernel which still exists as a kernel for jupyter. Settings for the notebook ui and jupyter specific things are in ~/.jupyter (or whatever jupyter config dir) and settings for the ipython kernel are in the ipython config dir. – bkanuka Mar 16 '16 at 15:38
  • 1
    I apologize; you were right. Somebody really should edit those docs. – Emre Mar 16 '16 at 17:36
  • @bkanuka How did you know this? How is one supposed to figure this out? Thank you so much for knowing this! This killed many many hours I only randomly came across this as I was about to add my own question relating to this. This seems like a terrible issue. – Kyle Swanson Jan 26 '18 at 01:27
3

Using Jupyter on windows at least, I was able to do it using something very much like venkat's answer, i.e.:

%matplotlib inline
import matplotlib
matplotlib.rcParams['figure.figsize'] = (8, 8)

I did this to square the circle, which had been rather eliptical up to that point. See, squaring the circle is not that hard. :)

John Lockwood
  • 3,787
  • 29
  • 27
2

Note that the path of ipython_kernel_config.py differs if you run ipython from a virtual environment. In that case, dig in the path where the environment is stored.

dopexxx
  • 2,298
  • 19
  • 30
1

Use figsize(width,height) in the top cell and it changes width of following plots

vkt
  • 439
  • 5
  • 6
1

For jupyter 5.x and above with IPython kernels, you can just override particular keys and leave the rest by putting things like this, with your desired figsize in your ~/.ipython/profile_default/ipython_kernel_config.py:

c = get_config()
c.InlineBackend.rc.update({"figure.figsize": (12, 10)})
mtd
  • 2,224
  • 19
  • 21