Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlibrc rcParams modified for Jupyter inline plots

I have seen this question come up a couple of times, but I think this information changes as jupyter/ipython get updated. I am currently running python 3.5, jupyter (latest) and matplotlib 2.0.
The %matplotlib inline plots have custom properties that are set after the matplotlibrc file is imported. The most annoying of these is that the figure.facecolor property is set to be transparent which wreaks havoc when copy/pasting plots so I have to reset this property in the notebook. I cannot seem to find where this property is changed, or if it is possible to create a configuration profile somewhere to change these special inline plot settings

My question is, is it possible to change these settings, and if so, how would I do that?

like image 545
Vince W. Avatar asked Mar 07 '17 19:03

Vince W.


People also ask

What does rcParams do in Python?

Changing the Defaults: rcParams Each time Matplotlib loads, it defines a runtime configuration (rc) containing the default styles for every plot element you create. This configuration can be adjusted at any time using the plt. rc convenience routine.

How do I change the plot size in jupyter notebook?

Next, to increase the size of the plot in the jupyter notebook use plt. rcParams[“figure. figsize”] method and set width and height of the plot.

Why is %Matplotlib inline?

Why matplotlib inline is used. 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 Matplotlibrc?

Matplotlib is a cross-platform, data visualization and graphical plotting library for Python and its numerical extension NumPy. As such, it offers a viable open source alternative to MATLAB. Developers can also use matplotlib's APIs (Application Programming Interfaces) to embed plots in GUI applications.


2 Answers

Some of the rcParameters are set specifically for the inline backend. Those are

{'figure.figsize': (6.0,4.0),  
 'figure.facecolor': (1,1,1,0), # play nicely with white background in the Qt and notebook
 'figure.edgecolor': (1,1,1,0),      
 'font.size': 10, # 12pt labels get cutoff on 6x4 logplots, so use 10pt.
 'figure.dpi': 72, # 72 dpi matches SVG/qtconsole
 'figure.subplot.bottom' : .125 # 10pt still needs a little more room on the xlabel
  }

And the place where they reside is the ipykernel/pylab/config.py file. This file can be edited to obtain the desired behaviour, e.g. by changing the facecolor to 'figure.facecolor': (1,1,1,1) (no transparency).

Another option is the following:

The rcParameters are defined as part of the InlineBackend class, specifically the InlineBackend.rc attribute which is a traitlets.Dict object.

Those can be changed using the ipython configuration system as follows.

From the command line type ipython profile create which will generate the default configuration files in ~/.ipython. In the main configuration file ~/.ipython/ipython_config.py include the line:

c.InlineBackend.rc.update({"figure.facecolor": "white"})
like image 143
ImportanceOfBeingErnest Avatar answered Oct 21 '22 00:10

ImportanceOfBeingErnest


The accepted answer was correct at the time. Recent update: The inline-specific backend will no longer override Matplotlib's defaults, see this commit from 12th of May 2022.

Before this recent change, the dict with the override values had moved to matplotlib-inline/matplotlib_inline/config.py

like image 26
KishKash Avatar answered Oct 21 '22 02:10

KishKash