Data Visualization Using Seaborn
By: Indhumathy Chelliah
Tags: Machine Learning, Python3, Artificial Intelligence, Data Science, Data Visualization

Visualizing the distribution of the dataset.

1. Univariate distribution

histogram

kdeplot

distplot

2. Bivariate distribution

joint plot

pairplot

Univariate distribution

1. Histogram

A histogram is used for visualizing the distribution of a single variable(univariate distribution). A histogram is a bar plot where the axis representing the data variable is divided into a set of bins, and the count of observations falling under each bin is shown on the other axis.

Data variable vs count

Importing libraries and dataset

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

import seaborn as sns df=pd.read_csv("Results.csv")

df.head(3)

Now we will create histogram plots.

Creating histogram for “Marks” variable. Let’s see the distribution of marks in this dataset.

sns.histplot(x="Marks",data=df)

Inference

From the plot, we can see the range of marks. (5 to 100) This plot also clearly shows that more students get marks of more than 80.

hue

In seaborn, the hue parameter determines which column in the data frame should be used for color encoding.

We can include the “Grade” variable as a hue parameter.

sns.histplot(x="Marks",data=df,bins=10,hue="Grade")

Inference.

Now, after adding the hue parameter, we get more information like which range of marks belongs to which grade.

2. KDE plot

A kernel density estimate (KDE) plot is a method for visualizing the distribution of observations in a dataset, similar to a histogram. KDE represents the data using a continuous probability density curve in one or more dimensions.

KDE →Kernel density estimation is the way to determine the probability density function of a continuous variable.

Data variable vs density

sns.kdeplot(x="Marks",data=df)

Inference

By using the KDE plot, we can infer the probability density function of the continuous variable.

hue parameter in KDE plot

sns.kdeplot(x="Marks",data=df,hue="Grade")

3. Distplot

Distplot is a combination of a histogram with a line (density plot) on it. Distplot is also used for visualizing the distribution of a single variable(univariate distribution).

In distplot, the y-axis represents density. So the histogram height shows a density rather than a count. This is implied if a KDE or fitted density is plotted.

sns.distplot(df[“Marks”])

To visualize only a density plot, we can give hist=False .

sns.distplot(df[“Marks”],hist=False)

To visualize only the histogram, we can give kde=False .

sns.distplot(df[“Marks”],kde=False)

Bivariate distribution

1. jointplot

A Jointplot displays the relationship between two numeric variables. It is a combination of scatterplot and histogram.

sns.jointplot(x=”Marks”,y=”Study_hours”,data=df)

The joint plot also draws a regression line if we mention kind=” reg”.

sns.jointplot(x=”Marks”,y=”Study_hours”,data=df,kind=”reg”)

Using hue as a parameter

sns.jointplot(x=”Marks”,y=”Study_hours”,data=df,hue=”Grade”)

Pairplot

Pairplot is used to describe pairwise relationships in a dataset. Pairplot is used to visualize the univariate distribution of all variables in a dataset along with all of their pairwise relationships. For n variables, it produces n*n grid.

The diagonal plots are histograms and all the other plots are scatter plots.

sns.pairplot(df)

Inference

Data distribution should show some trends. In this example, Marks vs Study_hours gives a linear relationship(positive correlation).

Student_Id column is not showing any relationship with the “Marks” and also the “Study_hours” variable.

Student_Id column can be dropped from the dataset.

Using hue parameter in pairplot.