Statistical Graphics with seaborn

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

Data set

In [2]:
%load_ext rpy2.ipython
In [3]:
%R data(C02)
co2 = %R CO2
In [4]:
co2.head()
Out[4]:
Plant Type Treatment conc uptake
1 1 Quebec nonchilled 95.0 16.0
2 1 Quebec nonchilled 175.0 30.4
3 1 Quebec nonchilled 250.0 34.8
4 1 Quebec nonchilled 350.0 37.2
5 1 Quebec nonchilled 500.0 35.3
In [5]:
co2.shape
Out[5]:
(84, 5)

Look and feel

In [6]:
g = sns.boxplot(x='Plant', y='uptake', data=co2)
../_images/scratch_Python10_8_0.png

Changing the style

Styles available

  • dark
  • white
  • darkgrid
  • whitegrid
  • ticks
In [7]:
sns.set_style('darkgrid')
g = sns.boxplot(x='Plant', y='uptake', data=co2)
../_images/scratch_Python10_10_0.png

Changing context

Contexts available

  • notebook
  • paper
  • talk
  • poster
In [8]:
sns.set_context('poster')
g = sns.boxplot(x='Plant', y='uptake', data=co2)
../_images/scratch_Python10_12_0.png

Changing font scale

In [9]:
sns.set_context('notebook', font_scale=1.5)
g = sns.boxplot(x='Plant', y='uptake', data=co2)
../_images/scratch_Python10_14_0.png

Changing Colors

In [10]:
pal = sns.choose_colorbrewer_palette('qualitative')
Widget Javascript not detected.  It may not be installed or enabled properly.
../_images/scratch_Python10_16_2.png
In [11]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
../_images/scratch_Python10_17_0.png
In [12]:
pal = sns.choose_dark_palette()
Widget Javascript not detected.  It may not be installed or enabled properly.
../_images/scratch_Python10_18_2.png
In [13]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
../_images/scratch_Python10_19_0.png
In [14]:
pal = sns.choose_light_palette()
Widget Javascript not detected.  It may not be installed or enabled properly.
../_images/scratch_Python10_20_2.png
In [15]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
../_images/scratch_Python10_21_0.png
In [16]:
pal = sns.choose_diverging_palette()
Widget Javascript not detected.  It may not be installed or enabled properly.
../_images/scratch_Python10_22_2.png
In [17]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
../_images/scratch_Python10_23_0.png
In [18]:
pal = sns.choose_colorbrewer_palette('divergent')
Widget Javascript not detected.  It may not be installed or enabled properly.
../_images/scratch_Python10_24_2.png
In [19]:
g = sns.boxplot(x='Plant', y='uptake', hue='Treatment',
                data=co2, palette=pal)
../_images/scratch_Python10_25_0.png

Types of plots

Showing distributions

In [20]:
g = sns.distplot(co2.uptake, rug=True)
../_images/scratch_Python10_28_0.png
In [21]:
g = sns.kdeplot(co2.uptake)
../_images/scratch_Python10_29_0.png

Note: conc is actually a categorical factor that I am abusing as a continuous value for plotting.

In [22]:
g = sns.kdeplot(co2.conc, co2.uptake)
../_images/scratch_Python10_31_0.png
In [23]:
g = sns.jointplot(x='conc', y='uptake', data=co2, kind='kde')
../_images/scratch_Python10_32_0.png

Showing relationships

In [24]:
g = sns.regplot(x='conc', y='uptake', data=co2)
../_images/scratch_Python10_34_0.png
In [25]:
g = sns.regplot(x='conc', y='uptake',
                lowess=True,
                data=co2)
../_images/scratch_Python10_35_0.png
In [26]:
sns.lmplot(x='conc', y='uptake', data=co2,
           row='Treatment', col='Type')
pass
../_images/scratch_Python10_36_0.png
In [27]:
sns.lmplot(x='conc', y='uptake', data=co2,
           order=2,
           row='Treatment', col='Type')
pass
../_images/scratch_Python10_37_0.png

Showing categories

In [28]:
g = sns.swarmplot(x='Treatment', y='uptake', data=co2)
../_images/scratch_Python10_39_0.png
In [29]:
sns.set_style('white')
g = sns.violinplot(x='Treatment', y='uptake', hue='Type',
                   split=True, data=co2)
sns.despine(offset=10, trim=True)
../_images/scratch_Python10_40_0.png
In [30]:
g = sns.factorplot(x='Treatment', y='uptake', row='conc', col='Type',
                   margin_titles=True,
                   data=co2, kind='bar')
../_images/scratch_Python10_41_0.png
In [ ]: