简体   繁体   English

在Seaborn中设置plot背景色

[英]Setting plot background colour in Seaborn

I am using Seaborn to plot some data in Pandas.我正在使用 Seaborn 到 plot Pandas 中的一些数据。

I am making some very large plots ( factorplot s).我正在制作一些非常大的地块( factorplot s)。

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.我正在使用由 4 x 4 显示器组成的复合屏幕,具有小(但非零)斜角 - 屏幕之间的间隙。 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.我一直在挖掘文档并四处玩耍,但我无法解决。当然这很简单。

I can get grey background using set_style('darkgrid')我可以使用set_style('darkgrid')获得灰色背景

do i need to access the plot in matplotlib directly?需要直接访问matplotlib中的plot吗?

seaborn.set takes an rc argument that accepts a dictionary of valid matplotlib rcparams . seaborn.set接受一个接受有效 matplotlib rcparams字典的rc参数。 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 axes object.所以我们需要设置两件事: 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:你得到:

在此处输入图片说明

And that'll work with your FacetGrid as well.这也适用于您的FacetGrid

I am not familiar with seaborn but the following appears to let you change the background by setting the axes background.我对 seaborn 不熟悉,但以下内容似乎可以让您通过设置轴背景来更改背景。 It can set any of the ax.set_* elements.它可以设置任何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()

Another way is to set the theme:另一种方法是设置主题:

seaborn.set_theme(style='white')

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 docsseaborn 文档中的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM