Data Visualization using Box Plot (Seaborn Library)

Home » News » Data Visualization using Box Plot (Seaborn Library)
Lets visualize our data with Box Plot which is present in Seaborn library. Box Plots are very useful in finding outliers in a variable. We can also combine Box Plot with Swarm Plot.


We can pass various parameters to boxplot like hue, order, orient, palette, color etc. 


Lets explore Box Plot using 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 dataset

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

Step 3: Explore data using Box Plot


Box Plot is both univariate and bivariate. Lets analyze it first by using one variable and then we will use two variables. 


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)


Add hue parameter

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') 


Note: If you run the above line, you will find that there is no hue corresponding to “Sat” and “Sun” as there is no data for “Lunch” for “Sat” and “Sun”.

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


Change orientation of box plot

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′)


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


Related:
What is Boxplot? How is it used to find outliers in a dataset?
Boxplot Grouping: Visualizing one variable based on another variable using boxplot

Leave a Reply

Your email address will not be published. Required fields are marked *

New Providers
Quotex

1000+ Trading Instruments
The best trading broker in the world.

Stockity

1000+ Trading Instruments
The best trading broker in the world.

New Games
Lies of P

$59.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

COCOON

$24.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

New Offers
Commission up to $1850 for active user of affiliate program By Exness

Top Points © Copyright 2023 | By Topoin.com Media LLC.
Topoin.info is a site for reviewing the best and most trusted products, bonus, offers, business service providers and companies of all time.

Discover more from Topoin

Subscribe now to keep reading and get access to the full archive.

Continue reading