Pages

Thursday, 18 April 2019

Data Visualization using FacetGrid (Seaborn Library)

Lets visualize our data with Facet Grid which is present in Seaborn library. Facet Grid can be used with Histogram, Scatter Plot, Regression Plot, Box Plot etc. 

We can pass various parameters to FacetGrid like row, col, col_order, hue, palette, height, aspect etc. To add legend to Facet Grid, you can use add_legend() function. 

Lets explore Facet Grid with Tips dataset. 

Step 1: Import required libraries

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline

Step 2: Load Tips datasets

tips=sns.load_dataset('tips')
tips.head()

Step 3: Explore data using Facet Grid

Facet Grid with Histogram

x = sns.FacetGrid(tips, row='smoker', col='time')
x = x.map(plt.hist, 'total_bill')

x = sns.FacetGrid(tips, row='smoker', col='time')
x = x.map(plt.hist, 'total_bill', bins=15, color='green')

Facet Grid with Scatter Plot

x = sns.FacetGrid(tips, row='smoker', col='time')
x = x.map(plt.scatter, 'total_bill', 'tip')

Facet Grid with Regression Plot

x = sns.FacetGrid(tips, row='smoker', col='time', height=6, aspect=0.7)
x = x.map(sns.regplot, 'total_bill', 'tip')

x = sns.FacetGrid(tips, col='time', hue='smoker', palette='husl')
x = x.map(sns.regplot, 'total_bill', 'tip')

x = sns.FacetGrid(tips, col='time', hue='smoker')
x = x.map(sns.regplot, 'total_bill', 'tip').add_legend()

Facet Grid with Box Plot

x = sns.FacetGrid(tips, col='day', height=10, aspect=0.2)
x = x.map(sns.boxplot, 'time', 'total_bill')

x = sns.FacetGrid(tips, col='day', height=10, aspect=0.2, col_order=['Sat', 'Sun', 'Thur', 'Fri'])
x = x.map(sns.boxplot, 'time', 'total_bill', color='red')

You can download my Jupyter notebook from here. I recommend to also try above code with Iris dataset.

1 comment:

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.