Monday, 15 April 2019

Data Visualization using Box Plot (Seaborn Library)

Lets visualize our data with Box Plot which is present in Seaborn library. We will use Tips dataset. We can pass various parameters to boxplot like palette, color, hue, order, orient etc. Lets explore boxplot in detail:

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 dataset

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

Step 3: Explore data using Box Plot

Please note that I am not displaying the resulting plots in this post. Please explore it yourself in your Jupyter notebook.

#Visualizing one variable using Box Plot

sns.boxplot(x=tips['tip'])

sns.boxplot(x=tips['total_bill'])

sns.boxplot(x='total_bill', data=tips)

#Visualizing two variables using Box Plot

sns.boxplot(x='sex', y='total_bill', data=tips)

sns.boxplot(x='day', y='total_bill', data=tips)

sns.boxplot(x='day', y='total_bill', data=tips, hue='sex')

sns.boxplot(x='day', y='total_bill', data=tips, hue='sex', palette='husl')

sns.boxplot(x='day', y='total_bill', data=tips, hue='smoker', palette='coolwarm')

sns.boxplot(x='day', y='total_bill', data=tips, hue='time', palette='coolwarm')

sns.boxplot(x='day', y='total_bill', data=tips, order=['Sat', 'Sun', 'Thur', 'Fri'])

sns.boxplot(data=tips)

sns.boxplot(data=tips, orient='horizontal')
sns.boxplot(data=tips, orient='h')

sns.boxplot(data=tips, orient='vertical')
sns.boxplot(data=tips, orient='v')

#Combining Box Plot and Swarm Plot

sns.boxplot(x='day', y='total_bill', data=tips, palette='husl')
sns.swarmplot(x='day', y='total_bill', data=tips, color='black')

sns.boxplot(x='day', y='total_bill', data=tips, palette='husl')
sns.swarmplot(x='day', y='total_bill', data=tips, color='0.35')

No comments:

Post a Comment