Table of Contents

Scatter Plots Example – Matplotlib Tutorial

Generally, the idea of a scatter plot is to either show a correlation between something or sometimes a distribution but generally, correlation or some sort of relationship between two types of variables or at least in our case two types because of this will be a x and y but later on down the road when we talk about 3d scatter plots you can actually compare three variables at a time so that’s pretty cool so like with population ages or something that we could do is you know maybe age to disease or age to cancer rate or something like that.

 

Creating a Scatter Plot using matplotlib

We will simply take two lists for our x and y coordinates. The new function that we will introduce here is scatter() function along with types of markers. Rest of the functions we have already discussed such as xlabel() ylabel() title() legend() show()

The below python code is a basic example of how to draw a scatter plot using Matplotlib.

 

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [9, 10, 11, 12, 13, 14, 15, 16]

plt.scatter(x,y, label="Skatter", marker="*", s=50)
plt.xlabel("x" )
plt.ylabel("y")
plt.title("Basic Scatter Plo")

plt.legend()
plt.show()

 

In the above code, you can see we have used plt.scatter() function. We have also used markers, the default symbol to represent plot data in scatter is dot. We have used star to represent data in our scatter plot and s to set the size of the marker. To know more about the type of markers visit the Matplotlib Marker Guide

 

Given below is the code shown in Python Online code editor where you can edit the code to experiment with the data and scatter plot.