Data Visualization in Python – Scatter plots in Matplotlib

Data Visualization in Python

In last post I talked about plotting histograms, in this post we are going to learn how to use scatter plots with data and why it could be useful.

What is Scatter Plot?

From Wikipedia:

A scatter plot (also called a scatterplot, scatter graph, scatter chart, scattergram, or scatter diagram)[3] 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. The data are displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.[4]


Scatter Plots are usually used to represent the correlation between two or more variables. It also helps it identify Outliers, if any.


Enough talk and let’s code. First come up with an arbitrary but interesting example. Suppose the result was announced for a class. In this class both guys and girls appeared in the exam. The goal is to find out who performed better and how to get rid of shortcomings. We are going to make a scatter plot for that.

We have grades available in two different lists and we are going to call scatter twice to plot different data sets. When it runs it produces a graph like below:

Scatter plot for grades


Boys are in green while girls in red. The graph is clearly telling that girls performed way better than guys but.. it also tells another interesting story. There are two outliers, one in guys and other in girls. The guy performed pretty well while a single girl did pretty bad. Data tells you story, it helps you to investigate unknowns. Graphs help you to find the fact and then investigate the causes this result got produced. Imagine if a school head hire a statistician, he would present this graph and then will ask the head to call these two buddies for their exceptional results. There are chances that the guy who performed well was being strictly monitored by parents and he was asked to work well or guided well. The girl did not perform well could have some domestic issue, or ill.. whatever. The point is data helps you to find facts.

Alright so after this fake data let’s deal with real data. We have to find sea level rise in past 100 years. I grabbed the data from here.


The graph it produced:

Sea Level Rise Graph

O boy! the situation is not well, climate change or whatever the reason is, it’s increasing sea level almost every year. There are no clear outlier here, at least in this graph. There might be a few if we zoom in.

Anyways, so that’s it for now.


If you like this post then you should subscribe to my blog for future updates.

* indicates required

If you like my work then you can donate me in bitcoins