Changing the default background color for matplotlib plots

I am using ipython with matplotlib . Can I set the default background color for matplotlib ? The color tone (white) must come from somewhere. Is it possible to override it with, say, #CCCCCC ?

Note. By default, I do not mean the default value for this ipython laptop. I mean the default installation for my matplotlib installation.

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

enter image description here

How can I get rid of them?

UPDATE:

now I have the following set in my /etc/matplotlibrc , and after each change I reloaded ipython notepad;

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

The plot looks the same as in the original screenshot. that is, a white bar around the graph.

UPDATE2:

I am using ipython and I have the 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; } 
+7
matplotlib ipython
source share
2 answers

You can configure matplotlib in various ways.

If you want to configure on the whole computer, then matplotlib uses the matplotlibrc configuration file by default.

If you want to edit this to change the default axes of facecolor (the technical term for the background), you need to uncomment and edit this line:

#axes.facecolor : white # axes background color

If you want to set the background color to #CCCCCC , you must change the line to:

axes.facecolor : CCCCCC # axes background color

NB , if you reinstall matplotlib, it will be overwritten. To prevent this, you can save it in "HOME / .matplotlib / matplotlibrc", as indicated in the example comments.

If you want to temporarily change it to a different color, just add the following to the top of the script:

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

If you want to change a single matplotlib.axes object, just use ax.set_axis_bgcolor('...') .

+10
source share

You need to set the colors of the axes and the 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

In addition, there is a distinction between facecolor for an interactive plot and what is saved; you also need to pass facecolor to f.savefig if you need a uniform background in the resulting file.

You can change the default values ​​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 on an IPython laptop with a built-in backend, where the β€œsaved” version of the figure that you see under the cell is not controlled by the figure parameter, but by the savefig parameter.

+7
source share

All Articles