SEABORN - VIOLIN PLOT Padhai ClassWork
padhai_ViolinPlot

Violin Plot from Seaborn with Penguin datasaet

In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(color_codes = True)
In [ ]:
df = sns.load_dataset('diamonds')
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.swarmplot(x='body_mass_g', data=pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f238993f110>

Description:

  • in simple words, Violin plots is a combination of BOX PLOT and KDE PLOT and SWARM PLOT
  • apart from outliers from Box plot and data points from Swarm Plot, everything else is depicted here
  • the think line in the middle represents the Inter Quartile Range (IQR)
  • the white dot (as observed in the figure) indicates the median value
  • the thin line indicates the whiskers as in box plot
  • a continuous KDE plot on the top indicating the distribution of data
  • the same KDE Plot is drawn in the opposite direction - symetrically opposite to the actual KDE Plot.
  • SWARM plot looks closer to VIOLIN plot
In [ ]:
sns.violinplot(x='body_mass_g', data=pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f238bc26ed0>
In [ ]:
sns.boxplot(x='body_mass_g', data=pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f2386c0a5d0>
In [ ]:
sns.kdeplot(x='body_mass_g', data=pen, shade=True)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f23869f3a90>
In [ ]:
fig, axs = plt.subplots(nrows=4)
sns.kdeplot(x='body_mass_g', data=pen, shade=True, ax=axs[0])
sns.boxplot(x='body_mass_g', data=pen,  ax=axs[1])
sns.swarmplot(x='body_mass_g', data=pen, ax=axs[2])
sns.violinplot(x='body_mass_g', data=pen, ax=axs[3])
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 30.2% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f23844a7850>
In [ ]:
fig, axs = plt.subplots(nrows=4)
fig.set_size_inches(6, 8)
sns.kdeplot(x='body_mass_g', data=pen, shade=True, ax=axs[0])
sns.boxplot(x='body_mass_g', data=pen,  ax=axs[1])
sns.swarmplot(x='body_mass_g', data=pen, ax=axs[2])
sns.violinplot(x='body_mass_g', data=pen, ax=axs[3])
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 5.5% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f238294f050>

How to align the axes for the above plots is as follows:

In [ ]:
fig, axs = plt.subplots(nrows=4)
fig.set_size_inches(6, 8)
p1 = sns.kdeplot(x='body_mass_g', data=pen, shade=True, ax=axs[0])
p1.set(xlim=(2000, 7500))
p2 = sns.boxplot(x='body_mass_g', data=pen,  ax=axs[1])
p2.set(xlim=(2000, 7500))
p3 = sns.swarmplot(x='body_mass_g', data=pen, ax=axs[2])
p3.set(xlim=(2000, 7500))
p4 = sns.violinplot(x='body_mass_g', data=pen, ax=axs[3])
p4.set(xlim=(2000, 7500))
/usr/local/lib/python3.7/dist-packages/seaborn/categorical.py:1296: UserWarning: 5.5% of the points cannot be placed; you may want to decrease the size of the markers or use stripplot.
  warnings.warn(msg, UserWarning)
Out[ ]:
[(2000.0, 7500.0)]
In [ ]:
fig, axs = plt.subplots(nrows=2)
p1 = sns.swarmplot(x='body_mass_g', data=pen, ax = axs[0])
p1.set(xlim = (2000, 7500))
p2 = sns.violinplot(x='body_mass_g', data=pen, ax = axs[1])
p2.set(xlim = (2000, 7500))
Out[ ]:
[(2000.0, 7500.0)]
In [ ]: