Controlling figure aesthetics in seaborn

This notebook provides a brief introduction to the seaborn functions that let you manipulate the look of matplotlib figures and choose color palettes that will reveal interesting patterns in your data.

For much more information, you can check out the detailed tutorials on figure style and color palettes at the seaborn documentation.


In [1]:
%matplotlib inline

In [2]:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(9221999)

Setting the figure style

Let's define a simple function to plot some offset sine waves to help us see the different stylistic parameters we can tweak.


In [3]:
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

This is what the plot looks like with matplotlib defaults:


In [4]:
sinplot()


To switch to seaborn defaults, simply import the package.


In [5]:
import seaborn as sns
sinplot()


By default seaborn plots with the "darkgrid" theme, but there are a number of other options:


In [6]:
sns.set_style("whitegrid")
data = 1 + np.random.randn(20, 6)
sns.boxplot(data=data);


Seaborn also has some utility functions to easily manipulate the figure outside of what can be done through the matplotlib rc parameters.


In [7]:
sns.set_style("ticks")
f, ax = plt.subplots()
sns.boxplot(data=data)
sns.despine(offset=10, trim=True)


Scaling figures for different contexts

The seaborn defaults are tailored to make plots that are well-proportioned for vieweing on your own computer screen. There are several other plotting contexts that let you quickly change the scale of plot elements for use in contexts where larger or smaller figures are used. you can also independently scale the fonts within each context.


In [8]:
sns.set_context("talk", font_scale=1.25)
sns.boxplot(data=data)
sns.axlabel("Category", "Score")
sns.despine(offset=10, trim=True);


Using color palettes

Seaborn also tries to make it easy to choose color palettes that are attractive and informative. The default palette is based on the default matplotlib cycle, but with flatter colors:


In [9]:
sns.set()
current_palette = sns.color_palette()
sns.palplot(current_palette)


It's also easy to get evenly spaced hues in the husl or hls color spaces.


In [10]:
sns.palplot(sns.color_palette("husl", 8))


You can use the color_palette function to get discretized colors from any matplotlib colormap.


In [11]:
sns.palplot(sns.color_palette("coolwarm", 7))


When working with matplotlib palettes that are naturally qualitative, seaborn handles them properly.


In [12]:
sns.palplot(sns.color_palette("Paired", 8))


Many seaborn functions use the color_palette function behind the scenes, and thus accept any of the valid arguments for their palette parameter.


In [13]:
sns.violinplot(data=data, inner="points", palette="Set3");


There is also a flexible interface to the cubehelix system.


In [14]:
sns.palplot(sns.cubehelix_palette(8, start=2.5, rot=.75))


Two other functions allow you to create custom palettes. The first takes a color and creates a blend with a very dark gray.


In [15]:
sns.palplot(sns.dark_palette("#5178C7", 8))


A more general function for making custom palettes interpolates between an arbitrary number of seed points.


In [16]:
sns.palplot(sns.blend_palette(["mediumseagreen", "ghostwhite", "#4168B7"], 9))