Pages

Wednesday, 17 April 2019

Data Visualization using Violin Plot (Seaborn Library)

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

We can pass various parameters to violinplot like hue, split, inner (quartile, stick), scale, scale_hue, bandwidth (bw), palette, order etc. 

Lets explore Violin 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 datasets

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

Step 3: Explore data using Violin Plot

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

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

Add hue and split parameter

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

sns.violinplot(x='day', y='total_bill', data=tips, hue='sex', split=True)

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

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

Add inner and scale parameter

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='quartile')

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='quartile', split='True')

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='quartile', split='True', scale='count')

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='stick', split='True', scale='count')

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='stick', split='True', scale='count', scale_hue=False)

sns.violinplot(x='day', y='total_bill', data=tips, hue='smoker', inner='stick', split='True', scale='count', scale_hue=False, bw=0.1)

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

No comments:

Post a Comment