Setting plot background colour in Seaborn

55,517

Solution 1

seaborn.set takes an rc argument 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 axes object.

(edited with advice from @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

And that'll work with your FacetGrid as well.

Solution 2

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.

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

Solution 3

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

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

More info in seaborn docs

Share:
55,517
Frames Catherine White
Author by

Frames Catherine White

(aka oxinabox) she/her/hers. Research Software Engineer, Invenia Labs. PhD, in Natural Language Processing / Machine Learning. Writes a lot of JuliaLang code.

Updated on July 25, 2022

Comments

  • Frames Catherine White
    Frames Catherine White almost 2 years

    I am using Seaborn to plot some data in Pandas.

    I am making some very large plots (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.

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

    do i need to access the plot in matplotlib directly?

  • Frames Catherine White
    Frames Catherine White over 9 years
    Well that works. I hope it isn't the best way. (I suspect it is though). I will wait to see if a seaborn expert has a better way before accepting this.
  • Paul H
    Paul H over 3 years
    @SumNeuron something else is killing your kernel