Skip to content

Scatter Plots

Scatter Plots

Scatter plots of (x,y) point pairs are created with Matplotlib's plt.scatter() function. A scatter plot is a plot of x,y point pairs.

The required positional arguments supplied to plt.scatter() are two lists or arrays. The first positional argument specifies the x-values of each point on the scatter plot. The second positional argument specifies the y-value of each point on the scatter plot. The general form of a plt.scatter() function call is:

plt.scatter(x-points, y-points)

In the example below, 100 random, but semi-focused, x and y-values are created. These points are plotted on a scatter plot using plt.scatter(). Note the number of x-values is the same as the number of y-values. The list or array sizes passed to plt.scatter() must be equal in size.

In [1]:
import matplotlib.pyplot as plt
import numpy as np
# if using a Jupyter notebook, include:
%matplotlib inline

x1 = 1.5 np.random.randn(100) + 10 y1 = 1.5 np.random.randn(100) + 10 x2 = 1.5 np.random.randn(100) + 4 y2 = 1.5 np.random.randn(100) + 4 x = np.append(x1,x2) y = np.append(y1,y2)

plt.scatter(x,y)

plt.show()

Matplotlib scatter plots can be customized by including keyword arguments in the plt.plot() function call. Note the keyword arguments used in plt.scatter() are a little different from the keyword arguments in other Matplotlib plot types.

scatter plot feature plt.scatter() keyword Example
marker size s= plt.scatter(x, y, s=10)
marker color c= plt.scatter(x, y, c=(122, 80, 4))
marker opacity alpha= plt.scatter(x, y, alpha=0.2)

Each of these keyword arguments can be assigned an individual value which applies to the whole scatter plot. The plt.scatter() keyword arguments can also be assigned to lists or arrays. Supplying a list or array controls the properties of each marker in the scatter plot.

In [2]:
import matplotlib.pyplot as plt
import numpy as np
# if using a Jupyter notebook, include:
%matplotlib inline

x1 = 1.5 np.random.randn(100) + 10 y1 = 1.5 np.random.randn(100) + 10 x2 = 1.5 np.random.randn(100) + 4 y2 = 1.5 np.random.randn(100) + 4 x = np.append(x1,x2) y = np.append(y1,y2) colors = np.random.rand(1002) area = np.pi (8 np.random.rand(1002))**2

plt.scatter(x, y, s=area, c=colors, alpha=0.5)

plt.xlabel('x points') plt.ylabel('y points') plt.title('Scatter plot of x-y pairs semi-focused in two regions')

plt.show()