更改Matplotlib图的默认背景颜色 [英] Change default background color for matplotlib plots

查看:918
本文介绍了更改Matplotlib图的默认背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ipythonmatplotlib一起使用.是否可以为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.

这篇关于更改Matplotlib图的默认背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆