Python 在 Seaborn 中设置绘图背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25238442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:54:49  来源:igfitidea点击:

Setting plot background colour in Seaborn

pythonmatplotlibplotpandasseaborn

提问by Lyndon White

I am using Seaborn to plot some data in Pandas.

我正在使用 Seaborn 在 Pandas 中绘制一些数据。

I am making some very large plots (factorplots).

我正在制作一些非常大的图(factorplots)。

To see them, I am using some visualisation facilities at my university. I am using a Compound screen made up of 4 by 4 monitors with small (but nonzero) bevel -- the gap between the screens. This gap is black. To minimise the disconnect between the screen i want the graph backgound to be black. I have been digging around the documentation and playing around and I can't work it out.. Surely this is simple.

为了看到它们,我在我的大学使用了一些可视化设施。我正在使用由 4 x 4 显示器组成的复合屏幕,带有小(但非零)斜角 - 屏幕之间的间隙。这个缝隙是黑色的。为了尽量减少屏幕之间的断开,我希望图形背景为黑色。我一直在挖掘文档并四处玩耍,但我无法解决。这当然很简单。

I can get grey background using set_style('darkgrid')

我可以使用获得灰色背景 set_style('darkgrid')

do i need to access the plot in matplotlib directly?

我需要直接访问 matplotlib 中的绘图吗?

采纳答案by Paul H

seaborn.settakes and rcargument that accepts a dictionary of valid matplotlib rcparams. So we need to set two things: the axes.facecolor, which is the color of the area where the data are drawn, and the figure.facecolor, which is the everything a part of the figure outside of the axesobject.

seaborn.setrc接受有效 matplotlib 字典的参数和参数rcparams。所以我们需要设置两件事情:axes.facecolor,它是绘制数据的区域的颜色,以及figure.facecolor,它是axes对象之外图形的一部分。

(edited with advice from @mwaskom)

(根据@mwaskom 的建议进行编辑)

So if you do:

所以如果你这样做:

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn
seaborn.set(rc={'axes.facecolor':'cornflowerblue', 'figure.facecolor':'cornflowerblue'})

fig, ax = plt.subplots()

You get:

你得到:

enter image description here

enter image description here

And that'll work with your FacetGridas well.

这也适用于您FacetGrid

回答by Greg

I am not familiar with seaborn but the following appears to let you change the background by setting the axes background. It can set any of the ax.set_*elements.

我对 seaborn 不熟悉,但以下内容似乎可以让您通过设置轴背景来更改背景。它可以设置任何ax.set_*元素。

import seaborn as sns
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

m=pd.DataFrame({'x':['1','1','2','2','13','13'],
                'y':np.random.randn(6)})

facet = sns.factorplot('x','y',data=m)

facet.set(axis_bgcolor='k')

plt.show()

回答by Simas Joneliunas

In new versions of seaborn you can also use axes_style()and set_style()to quickly set the plot style to one of the predefined styles: darkgrid, whitegrid, dark, white, ticks

在新版本的 seaborn 中,您还可以使用 axes_style()set_style()快速将绘图样式设置为预定义样式之一:darkgrid、whitegrid、dark、white、ticks

st = axes_style("whitegrid")
set_style("ticks", {"xtick.major.size": 8, "ytick.major.size": 8})

More info in seaborn docs

seaborn 文档中的更多信息