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]:
import numpy as np
import matplotlib.pyplot as plt

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 [2]:
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 [3]:

To switch to seaborn defaults, simply import the package.

In [4]:
import seaborn as sns

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

In [5]:
data = 1 + np.random.randn(20, 6)

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

In [6]:
f, ax = plt.subplots()

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.

In [7]:
sns.axlabel("Category", "Score")

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 [8]:
current_palette = sns.color_palette()

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

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

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

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

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

In [11]:
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 color or palette parameter.

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

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

In [13]:
sns.palplot(sns.dark_palette("skyblue", 8, reverse=True))

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

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