August 15, 2024
Python

# Data Visualization using Seaborn

Seaborn is a Python library built on top of matplotlib. Seaborn is basically a Data Visualization library with a wide variety of wonderful styles and features for statistical plotting. In this article, we will focus on Data Visualization using Seaborn.

## Installation and environment setup

Seaborn library has some mandatory dependencies which need to be installed first. They are:

• NumPy (>= 1.9.3)
• SciPy (>= 0.14.0)
• Pandas (>= 0.15.2)
• Matplotlib (>= 1.4.3)

Seaborn can be installed using any of the two methods given below:

1. Using Anaconda
1. Using Pip

To get started, we import the dependencies and the seaborn package, as:

Consider the following data:

The DataFrame is:

This sales_record dataset consists of information such as the sales of three companies over the years. Let’s plot some graphs to visualize this data more clearly.

### Line Plot

A line plot is useful to visualize the frequency of data along the number line. We can visualize the sales trend of the companies over the given 10 years using a line graph. The code for doing so in Seaborn is:

Output:

### Bar Graph

Data can also be visualized using horizontal or vertical straight lines. In Seaborn, barplot() function works on a full dataset and plots the mean of the categories, as:

Output:

## Plotting Distribution of the Data

Seaborn can also be used to plot the distribution of data using Histograms, KDE plots, or joint distributions. The function for plotting the univariate distribution of data using Seaborn is distplot(). This function depicts both the histogram and the KDE function for the data. For example, using distplot() for visualizing the distribution of sales for Company3:

Output:

To view only the histogram, we can set “kde=False”, as:

Output:

The use of these plots can be visualized more properly in a distributed data like:

Output:

We can also view KDE plots separately using the kdeplot() function. Let’s visualize this for the following multivariate data:

Output:

We can also plot the joint distribution using seaborn. Consider the following examples:

Output:

Hexplot can also be plotted using the jointplot() function, by changing kind=‘hex’, as:

Output:

### Scatter Plot

To view data in form of Scatter plots, can be done in using the relplot() function of Seaborn. Consider it on the sales_record data:

Output:

We can do the same using the scatterplot() function of Seaborn. Let’s view it on some random multivariate data:

Output:

## Plotting Categorical Data in Seaborn

The sales_records data is a good example to visualize categorical data, where we are viewing data of three different companies. With seaborn, this is done using the catplot() function. For example:

Output:

Using seaborn, we can view categorical data in different ways such as Jitter plot, Hue plot, Point plot, Box plot, etc. Jitter is the deviation from the true value. In the above plot, we can set jitter as False, which will give vales in a straight line rather than scattered:

Output:

Using a point plot, we can point out the estimate value and confidence interval. We can do so for the original sales_records data for three companies, as:

Output:

Box plot shows three quartile values of the distribution. For the sales_record data:

Output:

The data can also be represented using a violin plot, as:

Output:

If there we want to introduce another dimension such that its categories are visualized in different colors, then we set that feature in the hue parameter. Consider the following code:

Here, Type is of A and B, which can be represented using hue as:

## Summary

In this article, we looked at Data Visualization using Seaborn. We have seen various plots such as line plot, bar plot, histogram, distribution plot, joint plot, scatter plot, cat plot and box plot.