Change default background color for matplotlib plots

自闭症网瘾萝莉.ら 提交于 2020-01-13 08:12:29

问题


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:

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;
}

回答1:


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




回答2:


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])

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.



来源:https://stackoverflow.com/questions/25246602/change-default-background-color-for-matplotlib-plots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!