Pages

Tuesday, 16 April 2019

Data Visualization using Joint Plot (Seaborn Library)

Lets visualize our data with Joint Plot which is present in Seaborn library. By default, Joint Plot uses Scatter Plot and Histogram. Joint Plot can also display data using Kernel Density Estimate (KDE) and Hexagons. We can also draw a Regression Line in Scatter Plot. By using spearmanr function, we can print the correlation between two variables.

We can pass various parameters to jointplot like kind (reg, hex, kde), stat_func(spearmanr), color, size, ratio etc.

Spearmanr Parameter
  • Spearmanr parameter displays the correlation between two variables.
  • Value varies between -1 and +1 with 0 implying no correlation.
  • Correlations of -1 or +1 imply an exact monotonic relationship. 
  • Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases.
  • Spearmanr correlation does not assume that both variables are normally distributed.
For more details on spearmanr parameter, please visit documentation.

Lets explore Joint 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
from scipy.stats import spearmanr

Step 2: Load Tips dataset

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

Step 3: Explore data using Joint Plot

sns.jointplot(x='total_bill', y='tip', data=tips)

Add regression line to scatter plot and kernel density estimate to histogram

sns.jointplot(x='total_bill', y='tip', data=tips, kind='reg') 

Display kernel density estimate instead of scatter plot and histogram

sns.jointplot(x='total_bill', y='tip', data=tips, kind='kde')

Display hexagons instead of points in scatter plot

sns.jointplot(x='total_bill', y='tip', data=tips, kind='hex') 

Display correlation using spearmanr function

sns.jointplot(x='total_bill', y='tip', data=tips, stat_func=spearmanr)

Cosmetic parameters like color, size and ratio

sns.jointplot(x='total_bill', y='tip', data=tips, color='green')

sns.jointplot(x='total_bill', y='tip', data=tips, ratio=4, size=6)

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

No comments:

Post a Comment