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 will use Tips dataset. We can pass various parameters to barplot like palette, color, saturation, estimator, hue, order, ci, capsize etc. Lets explore barplot 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 Bar Plot

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

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

sns.barplot(x='day', y='total_bill', data=tips, color='green')  #pass color parameter if you want to display all the bars in same color

sns.barplot(x='day', y='total_bill', data=tips, color='green', saturation=0.3)  #you can also set saturation level of the color

sns.barplot(x='day', y='total_bill', data=tips, estimator=np.median)  #by default, estimator is mean, you can also set it to median or anything else

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

Note: Black lines in bar plot represent error parts. We can set the cap size 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)

No comments:

Post a Comment