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)
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)
Changing context¶
Contexts available
- notebook
- paper
- talk
- poster
In [8]:
sns.set_context('poster')
g = sns.boxplot(x='Plant', y='uptake', data=co2)
Changing font scale¶
In [9]:
sns.set_context('notebook', font_scale=1.5)
g = sns.boxplot(x='Plant', y='uptake', data=co2)
Changing Colors¶
- Seaborn tutorial
- See more examples
In [10]:
pal = sns.choose_colorbrewer_palette('qualitative')
Widget Javascript not detected. It may not be installed or enabled properly.
In [11]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
In [12]:
pal = sns.choose_dark_palette()
Widget Javascript not detected. It may not be installed or enabled properly.
In [13]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
In [14]:
pal = sns.choose_light_palette()
Widget Javascript not detected. It may not be installed or enabled properly.
In [15]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
In [16]:
pal = sns.choose_diverging_palette()
Widget Javascript not detected. It may not be installed or enabled properly.
In [17]:
g = sns.boxplot(x='Plant', y='uptake', data=co2, palette=pal)
In [18]:
pal = sns.choose_colorbrewer_palette('divergent')
Widget Javascript not detected. It may not be installed or enabled properly.
In [19]:
g = sns.boxplot(x='Plant', y='uptake', hue='Treatment',
data=co2, palette=pal)
Types of plots¶
Showing distributions¶
In [20]:
g = sns.distplot(co2.uptake, rug=True)
In [21]:
g = sns.kdeplot(co2.uptake)
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)
In [23]:
g = sns.jointplot(x='conc', y='uptake', data=co2, kind='kde')
Showing relationships¶
In [24]:
g = sns.regplot(x='conc', y='uptake', data=co2)
In [25]:
g = sns.regplot(x='conc', y='uptake',
lowess=True,
data=co2)
In [26]:
sns.lmplot(x='conc', y='uptake', data=co2,
row='Treatment', col='Type')
pass
In [27]:
sns.lmplot(x='conc', y='uptake', data=co2,
order=2,
row='Treatment', col='Type')
pass
Showing categories¶
In [28]:
g = sns.swarmplot(x='Treatment', y='uptake', data=co2)
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)
In [30]:
g = sns.factorplot(x='Treatment', y='uptake', row='conc', col='Type',
margin_titles=True,
data=co2, kind='bar')
In [ ]: