Scatter plots

This type of plot shows us the data as a collection of points. It offers a convenient way to visualize how numeric values are related. It also helps us to understand the relationships between multiple variables. We are going to use the scatter() method to plot the data in a scatter plot. In a scatter plot, the position of points depends on its x and y axis values; that is, two-dimensional values, so each value in a dataset is a position in either the horizontal or the vertical dimension. Let's look at an example of a scatter plot. Create a script called scatterplot_example.py and write the following content in it:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-2,2,100)
y = np.random.randn(100)
colors = np.random.rand(100)
plt.scatter(x,y,c=colors)
plt.show()

Run the script and you will get the following output:

student@ubuntu:~/work$ python3 scatterplot_example.py

The output is as follows:

In the preceding example, we got values of x and y. Then we plotted those values using the plt.scatter() method to get a scatter plot for the x and y values.