Intro to Data Visualization using Seaborn and Matplotlib
By: Sanket Kangle
Tags: Seaborn, Eda, Machine Learning, Data Visualization, Data Science

Photo by Carlos Muza on Unsplash

We live in an online era, we create 2.5 quintillion bytes of data each day. By the time you are reading this article, the number has increased already! That is a huge, really huge. Data visualization is the tool to harness its power. In the world of data science, one of the widely used libraries in python for the same is seaborn — an easy-to-use yet powerful tool.

Seaborn is an advanced data visualization library built on top of the matplotlib library — a plotting library for the Python programming language and its numerical mathematics extension NumPy. For this tutorial, you need a jupyter notebook/google colab or any other such .ipynb supporting the application. Without further due, let’s get started by importing all the necessary libraries.

Setting up the environment

Run the following code in your notebook to import pandas, matplotlib, and seaborn libraries.

If you do not have these libraries already installed, then use the following command to install them from your jupyter notebook and then use the above command to import them.

Importing and understanding data

We will be using the pokemon dataset for this exercise, you can get it from here or just run the following code

You should be able to see this combat stats data for the original 151 Pokémon.

Line plot using Seaborn

In the line chart, the measurement points are ordered (typically by their x-axis value) and joined with straight line segments. A line chart is often used to visualize a trend in data over intervals of time — a time series — thus the line is often drawn chronologically. In these cases, they are known as run charts.

Using Seaborn, in one line of code, we can plot a line plot.

Line 1: sns is allies generally used for seaborn . sns.seaborn tells the notebook that we want to create a line chart. Every command using the seaborn library will be starting with this sns prefix. For arguments, x and y takes the name of the column to be plotted on the x-axis and y-axis respectively. In data we give the name of the dataframe from which the column names are assigned to x and y . Typically this format is followed in every sns plot. the second

Line 2: To give the title to the plot, we used matplotlib.pyplot's allies plt and command line plt.title with a string as an argument. you should get a result like following

We can make multiple line plots in one graph as well, just need to add one more line with similar syntax.

Also, you can add labels to your line plots just by adding the argument label as above. Try it out!

Bar chart using Seaborn

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. A bar graph shows comparisons among discrete categories.

Line 2: we can use sns.countplot to plot the total count of the categorical variable

Line 5: Using plt.xticks we personalize the graph with a 45⁰ rotation. You should get output like following

When we have categories as columns and numerical value as data then we can use normal barplot as well like following

Heatmap using Seaborn

Heatmaps help you visualize matrix-like data. Each cell is color-coded according to its corresponding value. We will check what is the correlation between different columns of the pokemon dataset.

Line 1: df.corr() gives the matrix output of pairwise correlation of columns except for NA values and categorical columns, which is saved in a variable corr

Line 2: We set the size of the graph using this plt.figure(figsize=(x,y)) command

Line 3: we plot the heatplot using this sns.heatplot command. setting annot=True gives the numerical amplitude of correlation along with color-coding.

Scatter plots using Seaborn

A scatter plot is a type of plot or mathematical diagram using Cartesian coordinates to display values for typically two variables for a set of data. If the points are color-coded, one additional variable can be displayed.

Line 1: sns.scatterplot tells the notebook that we want to plot a scatter plot. x-axis, y-axis, and data is given as usual. hue gives color-coding to all the individual data points according to the provided categorical variable Stage

This data does show some linear regression right? but how can we sure of it? only if there was a way to put fit linear regression lines to the data. And you are in luck my friend, with Seaborn its just one line of code!

If you want individual regression lines for each value of Stage , then just add hue back in.

A positive slope suggests a positive correlation which can be verified by heatmap as well.

Usually, we use scatter plots to highlight the relationship between two continuous variables (like "heights" and "weights" ). However, we can adapt the design of the scatter plot to feature a categorical variable (like "gender" ) on one of the main axes. and we build it with the sns.swarmplot command.

Histograms using Seaborn

A histogram is an approximate representation of the distribution of numerical data. The higher the height of the bar, the more data falls into the range covered by that bar.

Line 1:We use sns.distplot to plot histograms. In the histogram, we dot need to provide x and y as y is by default frequency, so the only input we have to give is a .Note that we do not need to provide an argument data in this command. kde=False removes the kernel density estimation plot on top of the histogram.

kde = False

kde = True

We can also print multiple histograms in the same plot just like we did with line plots. Use the same command multiple times with proper input columns.

Line 1 and Line 2 are the same as above

Line 3: Forcing the legends to come on a plot with plt.legend() method

Density plots using Seaborn

Density plots display the distribution between two variables. One can think of them as a smoothened histogram. They are useful for plotting probability density functions and the probability mass functions of random variables.

Line 1: sns.kdeplot is the command used to plot KDE graph. data is assigned the dataset for plotting and shade=True fills the area under the curve with color.

We can also plot multiple KDE plot in the same graph as follows

We can also plot 2 column’s KDE plot using sns.jointplot