Seaborn offers:

  • Beautiful themes
  • Nice color palettes
  • Plotting univariate and multivariate distributions
  • Plotting regression results with error bars
  • Time series visualizations
  • Abstractions for grid of plots

Seaborn could be used with bokeh


In [1]:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

In [2]:
np.random.seed(sum(map(ord, "aesthetics")))

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)

In [4]:
sinplot()



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


Controlling figure properties:

  • axes_style()
  • set_style()
  • plotting_context()
  • set_context()

Basic seaborn themes:

  • darkgird
  • whitegrid
  • dark
  • white
  • ticks

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



In [7]:
sns.set_style("whitegrid")
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2
sns.boxplot(data=data);



In [8]:
sns.set_style('dark')
sinplot()



In [11]:
sns.set_style("white")
sinplot()



In [12]:
sns.set_style("ticks")
sinplot()



In [13]:
sinplot()
sns.despine()


sns.violinplot()


In [14]:
f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10);



In [15]:
f, ax = plt.subplots()
sns.violinplot(data=data)
sns.despine(offset=10, trim=True);


sns.boxplot()

sns.set_style("whitegrid") sns.boxplot(data=data, palette="deep") sns.despine(left=True)

Temporarily setting figure style


In [9]:
with sns.axes_style("darkgrid"):
    plt.subplot(211)
    sinplot()
plt.subplot(212)
sinplot(-1)


Customizing Seaborn styles


In [19]:
sns.axes_style()


Out[19]:
{'axes.axisbelow': True,
 'axes.edgecolor': '.8',
 'axes.facecolor': 'white',
 'axes.grid': True,
 'axes.labelcolor': '.15',
 'axes.linewidth': 1.0,
 'figure.facecolor': 'white',
 'font.family': [u'sans-serif'],
 'font.sans-serif': [u'Arial',
  u'Liberation Sans',
  u'Bitstream Vera Sans',
  u'sans-serif'],
 'grid.color': '.8',
 'grid.linestyle': u'-',
 'image.cmap': u'Greys',
 'legend.frameon': False,
 'legend.numpoints': 1,
 'legend.scatterpoints': 1,
 'lines.solid_capstyle': u'round',
 'text.color': '.15',
 'xtick.color': '.15',
 'xtick.direction': u'out',
 'xtick.major.size': 0.0,
 'xtick.minor.size': 0.0,
 'ytick.color': '.15',
 'ytick.direction': u'out',
 'ytick.major.size': 0.0,
 'ytick.minor.size': 0.0}

In [10]:
sns.set_style("darkgrid", {"axes.facecolor": ".9"})
sinplot()



In [11]:
sns.set_style("darkgrid", {"axes.facecolor": ".96"})
sinplot()



In [12]:
# reset default parameters
sns.set()
sinplot()


Use set_context() to change context

The following context are available

  • 'paper'
  • 'notebook'
  • 'talk'
  • 'poster'

In [13]:
sns.set_context("paper")
plt.figure(figsize=(8, 6))
sinplot()



In [14]:
sns.set_context("talk")
plt.figure(figsize=(8, 6))
sinplot()



In [28]:
sns.set_context("talk")
plt.figure(figsize=(8, 6))
sinplot()



In [32]:
sns.set_context("notebook", font_scale=1.5, rc={"lines.linewidth": 2.5})
sinplot()



In [35]:
sns.set(rc={"figure.figsize": (6, 6)})

np.random.seed(sum(map(ord, "palettes")))


<built-in function ord>

In [39]:
print np.random.seed()


None

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


Available standard color palettes deep, muted, pastel, bright, dark, and colorblind.

hls is good


In [41]:
sns.palplot(sns.color_palette("hls", 8))



In [49]:
sns.palplot(sns.hls_palette(8, l=.3, s=.8))
sns.palplot(sns.hls_palette(8, l=.7, s=.8))
sns.palplot(sns.hls_palette(8, l=.3, s=.3))
sns.palplot(sns.hls_palette(8, l=.7, s=.3))
sns.palplot(sns.hls_palette(8, l=.6, s=.9))


Even perception husl


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



In [53]:
sns.palplot(sns.color_palette("Paired"))



In [54]:
sns.palplot(sns.color_palette("Set2", 10))


choose_colorbrewer_palette()

This is great!


In [58]:
# data_type : {‘sequential’, ‘diverging’, ‘qualitative’}
sns.choose_colorbrewer_palette('qualitative', 0)


Out[58]:
[(0.8941176470588235, 0.10196078431372557, 0.10980392156862737),
 (0.21568627450980393, 0.4941176470588236, 0.7215686274509804),
 (0.3019607843137256, 0.6862745098039216, 0.29019607843137263),
 (0.5960784313725492, 0.3058823529411765, 0.6392156862745098),
 (1.0, 0.4980392156862745, 0.0),
 (0.9999999999999998, 1.0, 0.19999999999999996),
 (0.6509803921568629, 0.33725490196078434, 0.1568627450980391),
 (0.9686274509803922, 0.5058823529411766, 0.7490196078431374),
 (0.6, 0.6, 0.6)]

In [59]:
flatui = ["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71"]
sns.palplot(sns.color_palette(flatui))


xkcd ran a crowdsourced effort to name random
This produced a set of 954 named colors, which you can now reference in seaborn using the xkcd_rgb dictionary:


In [60]:
plt.plot([0, 1], [0, 1], sns.xkcd_rgb["pale red"], lw=3)
plt.plot([0, 1], [0, 2], sns.xkcd_rgb["medium green"], lw=3)
plt.plot([0, 1], [0, 3], sns.xkcd_rgb["denim blue"], lw=3);



In [61]:
colors = ["windows blue", "amber", "greyish", "faded green", "dusty purple"]
sns.palplot(sns.xkcd_palette(colors))


sequential color palattes


In [65]:
sns.palplot(sns.color_palette("Blues"))
sns.palplot(sns.color_palette("BuGn_r"))
sns.palplot(sns.color_palette("GnBu_r"))
sns.palplot(sns.color_palette("GnBu_d"))


Coilors appropriate for black and white printing

cubehelix


In [66]:
sns.palplot(sns.color_palette("cubehelix", 8))



In [67]:
sns.palplot(sns.cubehelix_palette(8))
sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75))
sns.palplot(sns.cubehelix_palette(8, start=2, rot=0, dark=0, light=.95, reverse=True))



In [69]:
x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
sns.kdeplot(x, y, cmap=cmap, shade=True)


Out[69]:
<matplotlib.axes._subplots.AxesSubplot at 0x1068f4210>

In [72]:
x, y = np.random.multivariate_normal([0, 0], [[1, -.5], [-.5, 1]], size=300).T
cmap = sns.cubehelix_palette(light=1, as_cmap=True)
sns.kdeplot(x, y, cmap=cmap)


Out[72]:
<matplotlib.axes._subplots.AxesSubplot at 0x103adba50>

In [73]:
sns.palplot(sns.light_palette("green"))
sns.palplot(sns.dark_palette("purple"))
sns.palplot(sns.light_palette("navy", reverse=True))



In [74]:
pal = sns.dark_palette("palegreen", as_cmap=True)
sns.kdeplot(x, y, cmap=pal);



In [75]:
sns.palplot(sns.light_palette((210, 90, 60), input="husl"))
sns.palplot(sns.dark_palette("muted purple", input="xkcd"))


diverging when high and low values are important


In [77]:
sns.palplot(sns.color_palette("BrBG", 7))
sns.palplot(sns.color_palette("RdBu_r", 7))
sns.palplot(sns.color_palette("coolwarm", 7))



In [78]:
sns.palplot(sns.diverging_palette(220, 20, n=7))
sns.palplot(sns.diverging_palette(145, 280, s=85, l=25, n=7))
sns.palplot(sns.diverging_palette(10, 220, sep=80, n=7))
sns.palplot(sns.diverging_palette(255, 133, l=60, n=7, center="dark"))



In [79]:
sns.set_palette("husl")
sinplot()



In [81]:
with sns.color_palette("PuBuGn_d"):
    sinplot()



In [82]:
sinplot()



In [83]:
print np.random.seed(sum(map(ord, "distributions")))


None

In [87]:
print sum(map(ord, "distributions"))


1427

In [88]:
print map(ord, "distributions")


[100, 105, 115, 116, 114, 105, 98, 117, 116, 105, 111, 110, 115]

In [91]:
print ord('d')


100

In [92]:
help(ord)


Help on built-in function ord in module __builtin__:

ord(...)
    ord(c) -> integer
    
    Return the integer ordinal of a one-character string.


In [ ]: