Pages

Monday, 15 April 2019

Data Visualization using Bar Plot (Seaborn Library)

Lets visualize our data with Bar Plot which is present in Seaborn library. 

We can pass various parameters to barplot like hue, confidence interval (ci), capsize, estimator (mean, median etc.), order, palette, color, saturation etc. 

Lets explore Bar 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 Bar Plot

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

Horizontal Bar Plot

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

Set color and saturation level

sns.barplot(x='day', y='total_bill', data=tips, color='green') 

sns.barplot(x='day', y='total_bill', data=tips, color='green', saturation=0.3)  

By default, estimator is mean, you can also set it to median or anything else

sns.barplot(x='day', y='total_bill', data=tips, estimator=np.median)

Add hue parameter

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

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

sns.barplot(x='day', y='total_bill', data=tips, hue='sex', color='green')

sns.barplot(x='day', y='total_bill', data=tips, hue='sex', palette='spring', order=['Sat', 'Sun', 'Thur', 'Fri'])

sns.barplot(x='sex', y='total_bill', data=tips, hue='sex', palette='spring', order=['Male', 'Female'])

Add confidence interval and capsize parameter

Black lines in bar plot represent error parts. We can set the capsize and confidence interval (ci) of the error parts. A confidence interval is a range of values, derived from sample statistics, that is likely to contain the value of an unknown population parameter.

sns.barplot(x='day', y='total_bill', data=tips, ci=99)
sns.barplot(x='day', y='total_bill', data=tips, ci=34)

sns.barplot(x='day', y='total_bill', data=tips, capsize=0.3)

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

No comments:

Post a Comment