Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib figure size in Jupyter reset by inlining in Jupyter

This question is more of a curiosity.

To change the default fig size to a custom one in matplotlib, one does

from matplotlib import rcParams
from matplotlib import pyplot as plt
rcParams['figure.figsize'] = 15, 9

after that, figure appears with chosen size.

Now, I'm finding something new (never happened/noticed before just now): in a Jupyter notebook, when inlining matplotlib as

%matplotlib inline

this apparently overwrites the rcParams dictionary restoring the default value for the figure size. Hence in oder to be able to set the size, I have to inline matplotlib before changing the values of the rcParams dictionary.

I am on a Mac OS 10.11.6, matplotlib version 1.5.1, Python 2.7.10, Jupyter 4.1.

like image 203
mar tin Avatar asked Sep 04 '16 14:09

mar tin


People also ask

How do I change the default plot size in matplotlib?

We can permanently change the default size of a figure as per our needs by setting the figure. figsize.

What is the default matplotlib figure size?

If not provided, defaults to rcParams["figure. figsize"] = [6.4, 4.8] .


1 Answers

IPython's inline backend sets some rcParams when it is initialized. This is configurable, and you can override it with your own configuration:

# in ~/.ipython/ipython_config.py
c.InlineBackend.rc = {
    'figure.figsize': (15, 9)
}

The above would replace all of the rcParams that the inline backend sets, and you get total control. If you already have a matplotlib style that works nicely for inline output, you can tell the backend to leave everything alone:

c.InlineBackend.rc = {}

If you want to change just a few values, rather than overriding the whole thing, you can use the dictionary .update method:

c.InlineBackend.rc.update({'figure.figsize': (15, 9)})

In the future, the inline backend should be doing its defaults via matplotlib's nice new style mechanism, which should make it behave nicer in terms of respecting your preferences and allowing easier customization.

like image 128
minrk Avatar answered Sep 20 '22 00:09

minrk