sns-FACETTED PLOTS
Padhai ClassWork
padhai_FacetedPlotting

Facetted Plotting with Seaborn's built-in 'Penguins' dataset

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', 
               split=True, inner = 'quartile', hue = 'sex', data = pen)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe586bc2d90>
In [ ]:
pen['bin_species'] = pen.species.apply(lambda x: "Gentoo" if x == "Gentoo" else '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 0x7fe5845caa50>
In [ ]:
pen.head(1)
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
In [ ]:
sns.kdeplot(pen.flipper_length_mm, shade = True)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe5845652d0>
In [ ]:
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe5844e84d0>
In [ ]:
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True)
sns.kdeplot(pen[pen.species == 'Adelie'].flipper_length_mm, shade = True)
sns.kdeplot(pen[pen.species == 'Chinstrap'].flipper_length_mm, shade = True)
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe5844e8610>
In [ ]:
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True)
sns.kdeplot(pen[pen.species == 'Adelie'].flipper_length_mm, shade = True)
sns.kdeplot(pen[pen.species == 'Chinstrap'].flipper_length_mm, shade = True)
plt.legend(title="Species", labels = ['Gentoo', "Adelie", "Chinstrap"])
Out[ ]:
<matplotlib.legend.Legend at 0x7fe58447c210>
In [ ]:
sns.boxplot(pen[pen.species == 'Adelie'].flipper_length_mm)
sns.boxplot(pen[pen.species == 'Chinstrap'].flipper_length_mm)
sns.boxplot(pen[pen.species == 'Gentoo'].flipper_length_mm)
plt.legend(title="Species", labels = ['Gentoo', "Adelie", "Chinstrap"])
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
Out[ ]:
<matplotlib.legend.Legend at 0x7fe584368710>

Observation:

  • not as in the case of KDE plot, we can have a clear view of the box plots, even when, they over-lap over the other
In [ ]:
fig, axs = plt.subplots(nrows = 3)
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True, ax=axs[0])
sns.kdeplot(pen[pen.species == 'Adelie'].flipper_length_mm, shade = True, ax=axs[1])
sns.kdeplot(pen[pen.species == 'Chinstrap'].flipper_length_mm, shade = True, ax=axs[2])
plt.legend(title="Species", labels = ['Gentoo', "Adelie", "Chinstrap"])
Out[ ]:
<matplotlib.legend.Legend at 0x7fe5841baf90>
In [ ]:
fig, axs = plt.subplots(nrows = 3)
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True, ax=axs[0])
sns.kdeplot(pen[pen.species == 'Adelie'].flipper_length_mm, shade = True, ax=axs[1])
sns.kdeplot(pen[pen.species == 'Chinstrap'].flipper_length_mm, shade = True, ax=axs[2])
# plt.legend(title="Species", labels = ['Gentoo', "Adelie", "Chinstrap"])
plt.tight_layout()

Using for loop to draw plots

In [ ]:
fig, axs = plt.subplots(nrows = 3)
fig.set_size_inches(8, 6)
sns.kdeplot(pen[pen.species == 'Gentoo'].flipper_length_mm, shade = True, ax=axs[0])
sns.kdeplot(pen[pen.species == 'Adelie'].flipper_length_mm, shade = True, ax=axs[1])
sns.kdeplot(pen[pen.species == 'Chinstrap'].flipper_length_mm, shade = True, ax=axs[2])
# plt.legend(title="Species", labels = ['Gentoo', "Adelie", "Chinstrap"])
plt.tight_layout()
In [ ]:
sns.boxplot(pen[pen.species == "Gentoo"].flipper_length_mm)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe584483950>
In [ ]:
sns.boxplot(pen[pen.species == "Gentoo"].flipper_length_mm)
sns.boxplot(pen[pen.species == "Adelie"].flipper_length_mm)
sns.boxplot(pen[pen.species == "Chinstrap"].flipper_length_mm)
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
Out[ ]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fe5816fee50>

to see that the box plots doesn't overlap over the other, fig.set_size_inches() can be used

In [ ]:
fig, axs = plt.subplots(nrows = 3)
fig.set_size_inches(6, 6)
sns.boxplot(pen[pen.species == "Gentoo"].flipper_length_mm, ax = axs[0])
sns.boxplot(pen[pen.species == "Adelie"].flipper_length_mm, ax = axs[1])
sns.boxplot(pen[pen.species == "Chinstrap"].flipper_length_mm, ax = axs[2])
plt.tight_layout()
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning

it would be cumbersome, to plot, say, 10 plots in the same region, as the code needs to be replicated required number of times.

as the work is repetitive in nature, we can use FOR loop to duplicate the same plot with various other parameters

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 [ ]:
col_name = 'species'
nrows = len(pen[col_name].unique())
fig, axs = plt.subplots(nrows=nrows)
fig.set_size_inches(6, 6)
j = 0
for i in pen[col_name].unique():
    sns.boxplot(pen[pen.species == i].flipper_length_mm, ax = axs[j])
    j += 1
plt.tight_layout()
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
/usr/local/lib/python3.7/dist-packages/seaborn/_decorators.py:43: FutureWarning: Pass the following variable as a keyword arg: x. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  FutureWarning
In [ ]:
col_name = 'species'
n = len(pen[col_name].unique())
print(n)
3

more efficient way doing the above is through FACET GRID

Link for further reference: https://seaborn.pydata.org/generated/seaborn.FacetGrid.html

In [ ]:
g = sns.FacetGrid(pen, row = 'species')
g.map(sns.kdeplot, 'flipper_length_mm', shade=True)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe5811acf10>
In [ ]:
g = sns.FacetGrid(pen, col = 'species')
g.map(sns.kdeplot, 'flipper_length_mm', shade=True)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe5810384d0>
In [ ]:
g = sns.FacetGrid(pen, col = 'island')
g.map(sns.kdeplot, 'flipper_length_mm', shade=True)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe5814d4250>
In [ ]:
g = sns.FacetGrid(pen, col = 'island')
g.map(sns.boxplot, 'flipper_length_mm')
/usr/local/lib/python3.7/dist-packages/seaborn/axisgrid.py:670: UserWarning: Using the boxplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe58186ee50>
In [ ]:
g = sns.FacetGrid(pen, col = 'island')
g.map(sns.histplot, 'flipper_length_mm')
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe580f29890>
In [ ]:
g = sns.FacetGrid(pen, col = 'island')
g.map(sns.distplot, 'flipper_length_mm')
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).
  warnings.warn(msg, FutureWarning)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe580d8efd0>
In [ ]:
g = sns.FacetGrid(pen, col = 'island', row='sex')
g.map(sns.histplot, 'flipper_length_mm')
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe580c49a90>
In [ ]:
g = sns.FacetGrid(pen, col = 'island', row='sex')
g.map(sns.violinplot, 'flipper_length_mm')
/usr/local/lib/python3.7/dist-packages/seaborn/axisgrid.py:670: UserWarning: Using the violinplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe580ab3810>
In [ ]:
g = sns.FacetGrid(pen, col = 'island', row='sex', hue='species')
g.map(sns.violinplot, 'flipper_length_mm')
/usr/local/lib/python3.7/dist-packages/seaborn/axisgrid.py:670: UserWarning: Using the violinplot function without specifying `order` is likely to produce an incorrect plot.
  warnings.warn(warning)
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe580484bd0>
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 [ ]:
g = sns.FacetGrid(pen, col = 'island', row='sex', hue='species')
g.map(sns.scatterplot, 'flipper_length_mm', 'bill_length_mm')
g.add_legend()
Out[ ]:
<seaborn.axisgrid.FacetGrid at 0x7fe57f98ecd0>
In [ ]:
g = sns.FacetGrid(pen, col = 'island', row='sex', hue='species')
g.map(sns.scatterplot, 'flipper_length_mm', 'bill_length_mm')
g.add_legend()
g.refline(y=pen['bill_length_mm'].median())
In [ ]: