Signup/Sign In

Matplotlib Scatter Plot - scatter() Function

In this tutorial, we will cover what is a scatter plot? and how to create a scatter plot to present your data using Matplotlib library.

The Scatter plot is a type of plot that is used to show the data as a collection of points.

  • This plot is mainly used to observe the relationship between the two variables.

  • Scatter plots make use of dots to represent the relationship between two variables.

  • These plots are mainly used to plot data points on the horizontal and vertical axis in order to show how much one variable is affected by another.

  • In 2-Dimensions it is used to compare two variables while in 3-Dimensions it is used to make comparisons in three variables.

Matplotlib scatter() Function

The method scatter() in the pyplot module in matplotlib library of Python is mainly used to draw a scatter plot.

The syntax to use this method is given below:

matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s, c, marker, cmap, vmin, vmax,alpha,linewidths, edgecolors)

Function Parameters:

Let us discuss the parameters of scatter() method:

  • x_axis_data

    This parameter indicates an array containing x-axis data.

  • y_axis_data

    This parameter indicates an array containing y-axis data.

  • s<

    This parameter indicates the marker size (it can be scalar or array of size equal to the size of x or y). It is an optional parameter and the default value is None.

  • c

    This parameter indicates the color of sequence and it is an optional parameter with default value equals to None.

  • marker

    This parameter is used to indicate the marker style. The default value of this parameter is None and it is also an optional parameter.

  • cmap

    This optional parameter indicates cmap name with default value equals to None.

  • linewidths

    This parameter indicates the width of the marker border and having None as default value.

  • edgecolors

    This parameter is used to indicate the marker border-color and also it's default value is None.

  • alpha

    This option indicates the blending value, between 0 (transparent) and 1 (opaque).

Let us dive into some examples and create some scatter plots.

Simple Scatter Plot Example:

Below we have a code snippet to create a simple scatter plot. Let us go through the code snippet:

import matplotlib.pyplot as plt 

x =[5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6] 
y =[99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86] 
plt.scatter(x, y, c ="red") 
plt.show() 

When you run the above code on your machine you will see the output as shown below:

scatter plot example matplotlib

Scatter Plot with large Dataset:

Let us create another scatter plot with different random numbers and the code snippet is given below:

import numpy as np
import matplotlib.pyplot as plt

# Creating the data
N = 1000
x = np.random.rand(N)
y = np.random.rand(N)
colors = (0,0,0)
area = np.pi*3

plt.scatter(x, y, s=area, c=colors, alpha=0.5)
plt.title('Scatter plot studytonight.com')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

The output for the same is as follows:

scatter plot example matplotlib
It is important to note here that the data can be classified into several groups. Let us understand how to create scatter plots with the group with the help of code snippet given below:

Customized Scatter Plot Example:

Now we will cover the code snippet of scatter plot with the groups of heights and weights in matplotlib:



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.