Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default background color for matplotlib plots

I am using ipython with matplotlib. Is it possible to configure the default background color for matplotlib plots? The curent (white) colour must come from somewhere. Is it possible to override it to, lets say, #CCCCCC?

Note: By default, I don't mean default for a given ipython notebook. I mean default for my matplotlib installation.

The solution suggested by @Ffisegydd works. however, after setting axes.facecolor : F4EAEA, I still get white edges around the plot:

enter image description here

How can I get rid of those?

UPDATE:

now I have following set in my /etc/matplotlibrc and I have restarted ipython notebook after each change;

axes.facecolor      : F4EAEA   
figure.facecolor : F4EAEA  
figure.edgecolor : F4EAEA   
savefig.facecolor : F4EAEA    
savefig.edgecolor : F4EAEA    

The plot looks the same as on the original screenshot. i.e. there is the white stripe around the plot.

UPDATE2:

I am using ipython, and I have following custom css in my ~/.config/ipython/profile_nbserver/static/custom/custom.css

div.output_area {
  border-radius: 4px;
  background: #F4EAEA !important;
  border: thin solid #4a4a4a;
}
like image 749
Martin Vegter Avatar asked Aug 11 '14 15:08

Martin Vegter


2 Answers

You can customise matplotlib in a variety of ways.

If you're looking to customise across your entire computer then matplotlib uses the "matplotlibrc" configuration file as a default.

If you wish to edit this to change the default axes facecolor (the technical term for the background) then you'll need to uncomment and adjust this line:

#axes.facecolor : white # axes background color

If you wish to set your background colour to #CCCCCC then you should change the line to:

axes.facecolor : CCCCCC # axes background color

N.B. if you re-install matplotlib this will be overwritten. To prevent this you can save it in "HOME/.matplotlib/matplotlibrc" as the example comments state.

Should you wish to change it to a different colour temporarily then simply add the following at the top of your script:

import matplotlib as mpl

mpl.rcParams['axes.facecolor'] = '111111' # Or any suitable colour...

If you should wish to modify an individual matplotlib.axes object then just use ax.set_axis_bgcolor('...').

like image 121
Ffisegydd Avatar answered Sep 20 '22 23:09

Ffisegydd


You need to set both the axes and figure background colors:

f = plt.figure(facecolor=".6")
ax = f.add_subplot(111, axisbg=".6")
ax.plot([0, 1, 2], [1, 0, 2])

enter image description here

There is additionally a distinction between the facecolor for the interactive plot and what gets saved; you also have to pass facecolor to f.savefig if you want a uniform background on the resulting file.

You can change the defaults with the following fields in the rcParams dictionary:

import matplotlib as mpl
mpl.rcParams["figure.facecolor"]
mpl.rcParams["axes.facecolor"]
mpl.rcParams["savefig.facecolor"]

Note that this works a little unexpectedly in the IPython notebook with an inline backend, where the "saved" version of the figure you see below the cell is not controlled by the figure parameter, but by the savefig paramter.

like image 27
mwaskom Avatar answered Sep 20 '22 23:09

mwaskom