更改Matplotlib图的默认背景颜色
我正在将ipython
与matplotlib
一起使用.是否可以为matplotlib
图配置默认的背景颜色?当前(白色)颜色必须来自某个地方.可以将其覆盖为#CCCCCC
吗?
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
?
注意:默认情况下,我并不是说给定的ipython笔记本为默认设置.我的意思是matplotlib
安装的默认设置.
Note: By default, I don't mean default for a given ipython notebook. I mean default for my matplotlib
installation.
@Ffisegydd建议的解决方案有效.但是,设置axes.facecolor : F4EAEA
后,在绘图周围仍然出现白色边缘:
The solution suggested by @Ffisegydd works. however, after setting axes.facecolor : F4EAEA
, I still get white edges around the plot:
我该如何摆脱呢?
现在我在/etc/matplotlibrc
中设置了以下设置,并且每次更改后都重新启动了ipython笔记本;
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.
我正在使用ipython,并且在我的~/.config/ipython/profile_nbserver/static/custom/custom.css
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;
}
您需要同时设置轴和图形背景颜色:
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])
交互式绘图的面色与保存的内容之间还有区别;如果要在生成的文件上使用统一的背景,还必须将facecolor
传递给f.savefig
.
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.
您可以在rcParams
词典中的以下字段中更改默认值:
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"]
请注意,这在带有内联后端的IPython笔记本中会有些意外,其中您在单元格下面看到的图形的保存"版本不受figure
参数的控制,而是受savefig
参数的控制
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.