15

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; } 
3
  • Can you say how you're plotting? That looks like IPython, but it doesn't look like either the usual notebook interface or the qtconsole. Commented Aug 11, 2014 at 19:12
  • Your issue is probably with your custom css then, not matplotlib. Commented Aug 11, 2014 at 23:33
  • @mwaskom - the white edges around the plot are still part of the plot (i.e.: they come from matplotlib - notice the numbers on the axes). The css does not interfere in how images are rendered. Commented Aug 12, 2014 at 7:11

2 Answers 2

19

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.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks. I have now set all 3 (axes.facecolor, figure.facecolor, savefig.facecolor) to F4EAEA in /etc/matplotlibrc. But I still see the white edge (as shown on the original screenshot).
Ah, there is also an edgecolor parameter you will probably want to set.
the axes.edgecolor parameter seems to affect the black frame (line) around the plot. I want to keep that black. This is the same black line that you have on your screenshot as well.
Sorry, figure.edgecolor (and savefig.edgecolor) are probably what you're looking for.
I have all the following set to F4EAEA and it still shows the white edge: axes.facecolor, figure.facecolor, figure.edgecolor, savefig.facecolor, savefig.edgecolor
|
13

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('...').

6 Comments

thanks. I have found /etc/matplotlibrc on my system. However, neither axes.facecolor : #cccccc nor axes.facecolor : '#cccccc' works.
@MartinVegter this works for me axes.facecolor: eeddcc
I've just tested it and I find the same as Diziet. If you uncomment the line and don't use the # symbol it works (testing on OS X Mavericks placing the file at ~/.matplotlib/matplotlibrc)
@Ffisegydd - thanks, it works now. But there are still white edges around (I have added a screenshot to my original question)
@MartinVegter unfortunately I'm not able to replicate it. I have a white background as standard on my IPython notebook so it looks like this with simply a grey axes face and white all around.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.