Online Machine Learning Quiz

100+ Objective Machine Learning Questions. Lets see how many can you answer?

Start Quiz

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


Step 3: Explore data using Facet Grid

Facet Grid with Histogram

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

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

Facet Grid with Scatter Plot

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

Facet Grid with Regression Plot

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

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

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

Facet Grid with Box Plot

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

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

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

No comments:

Post a Comment