sns PAIRED VIOLIN PLOTS
Padhai Class Work
padhai_paired_violinPlots
In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes = True)
In [ ]:
pen = sns.load_dataset('penguins')
In [ ]:
pen.head(2)
Out[ ]:
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', data = pen[pen.sex=='Male'])
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb5cadf10>
In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', data = pen[pen.sex=='Female'])
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb3af47d0>

Paired Violin Plot

In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', 
               split=True, hue = 'sex', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb35561d0>
In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', 
               split=True, hue = 'island', data = pen)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-dd7e5bf91a84> in <module>()
      1 sns.violinplot(x='island', y = 'flipper_length_mm', 
----> 2                split=True, hue = 'island', data = pen)

/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py in inner_f(*args, **kwargs)
     44             )
     45         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 46         return f(**kwargs)
     47     return inner_f
     48 

/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in violinplot(x, y, hue, data, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation, ax, **kwargs)
   2401                              bw, cut, scale, scale_hue, gridsize,
   2402                              width, inner, split, dodge, orient, linewidth,
-> 2403                              color, palette, saturation)
   2404 
   2405     if ax is None:

/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py in __init__(self, x, y, hue, data, order, hue_order, bw, cut, scale, scale_hue, gridsize, width, inner, split, dodge, orient, linewidth, color, palette, saturation)
    539         if split and self.hue_names is not None and len(self.hue_names) != 2:
    540             msg = "There must be exactly two hue levels to use `split`.'"
--> 541             raise ValueError(msg)
    542         self.split = split
    543 

ValueError: There must be exactly two hue levels to use `split`.'

Inner quartile

  • the fewer dash line is the median
  • dash line is the upper and lower quartiles
In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', 
               split=True, inner = 'quartile', hue = 'sex', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb34a9210>
In [ ]:
sns.violinplot(x='island', y = 'flipper_length_mm', hue = 'species', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb34f91d0>

Split allows only two categorical variables,

  • we can have a new col, by selecting one of the given cols and
  • grouping the other two species => as follows
In [ ]:
pen['bin_species'] = pen.species.apply(lambda x: 0 if x == "Gentoo" else 1)
In [ ]:
pen.head(2)
Out[ ]:
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex bin_species
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male 1
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female 1
In [ ]:
sns.violinplot(x = 'island', y='flipper_length_mm',
               split = True, inner = 'quartile', hue='bin_species', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb14387d0>
In [ ]:
pen['bin_species'] = pen.species.apply(lambda x: "Gentoo" if x == "Gentoo" else 'Adelie | Chinstrap')
In [ ]:
pen.head(2)
Out[ ]:
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex bin_species
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male Adelie | Chinstrap
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female Adelie | Chinstrap
In [ ]:
sns.violinplot(x = 'island', y='flipper_length_mm',
               split = True, inner = 'quartile', hue='bin_species', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8eb1367750>
In [ ]: