Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seaborn sns.set() changing plot background color

Tags:

python

seaborn

In seaborn using sns.set() seems to be changing the background color of the plot.

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


df = pd.DataFrame({"type":["A", "A", "A", "A", "B", "B", "B", "B"],
                   "value":[11, 14, 13, 16, 9, 8, 6, 10],
                  "date":["t1", "t2", "t3", "t4", "t1", "t2", "t3", "t4"]})

grid = sns.FacetGrid(df, size=12.5, hue="type", aspect=2)
grid.map(plt.plot, "date", "value")
plt.show()

seaborn1

Then if I run sns.set(font_scale=2) (or just sns.set()), repeating the same plot I get

grid = sns.FacetGrid(df, size=12.5, hue="type", aspect=2)
grid.map(plt.plot, "date", "value")
plt.show()

seaborn2

This seems like somewhat odd behaviour to me. I would prefer the second plotting configuration but would like to obtain this without making an arbitrary call to sns.set(), unless of course this is the recommended approach.

Relevant version info

print("matplotlib version: %s" % matplotlib.__version__)
print("seaborn version: %s" % sns.__version__)

matplotlib version: 2.1.0
seaborn version: 0.8.0
like image 309
mgilbert Avatar asked Feb 21 '18 17:02

mgilbert


1 Answers

Looking at the documentation, it seems that sns.set() was called on import in versions < 0.8.

The reason why calling sns.set without any arguments changes your plot, is that it has some default parameters. Looking at the documentation:

seaborn.set(context='notebook', style='darkgrid', palette='deep', font='sans-serif', font_scale=1, color_codes=False, rc=None)

So, using sns.set(font_scale=2) will change the fontscale, but will also change everything else to the default arguments of sns.set

like image 120
DavidG Avatar answered Oct 16 '22 12:10

DavidG