لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

Python Data Science Matplotlib Scatter Plot the scatter() function

How to Create Scatter Plots?

scatter() function is used with Pyplot in order to draw a scatter plot. The scatter() function plots one dot for each observation.
It needs two arrays of the same length, one for the values of the x-axis, and one for values on the y-axis.

Example 1: A simple scatter plot.

code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,94,
78,77,85,86,99])

plt.scatter(x, y)
plt.show()

the output will be


Observations

The observation in the example above is the result of 13 Bikes passing by.

The X-axis shows how old the Bikes is.

The Y-axis shows the speed of the Bikes when it passes.

Are there any relationships between the observations?

It seems that the newer the Bikes, the faster it drives, but that could be a coincidence, given the fact is that only 13 Bikes are registered.

Comparing Plots:

In the example above, there seems to be a relationship between speed and age, but what if we plot the observations from another day as well? Will the scatter plot tell us something else?

Example 2: Draw two plots on the same figure

Code

import matplotlib.pyplot as plt
import numpy as np

#day 1, the age and speed of 13 Bikes:
x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
plt.scatter(x, y)

#day 2, the age and speed of 15 Bikes:

x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])
y = np.array([88,87,88,111,85,103,87,
94,78,77,85,86,99])
plt.scatter(x, y)

plt.show()

the output will be


Note: The two plots are plotted with two different colors, by default blue and orange.

By comparing the 2 plots we can safely say that they both gives us the same conclusion. The newer the Bike, the faster it drives.


Setting the Colors

You can set your own color for each scatter plot with the color or the c argument.

Example 3: Set your own color of the markers.

Code

import matplotlib.pyplot as plt
import numpy as np

#day 1, the age and speed of 13 Bikes:

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
plt.scatter(x, y, color = 'hotpink')

#day 2, the age and speed of 15 Bikes:
x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])
y = np.array([88,87,88,111,85,103,87,
94,78,77,85,86,99])
plt.scatter(x, y, color = '#88c999')

plt.show()

the output will be


Setting the Colors for Each Dot You can even set a specific color for each dot by using an array of colors as value for the c argument.

Note: You cannot use the color argument for this, only the c argument.

Example 4: Set your own color of the markers.

Code

import matplotlib.pyplot as plt
import numpy as np

#day 1, the age and speed of 13 Bikes:

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
colors = np.array(["magenta","green","blue",
"yellow","pink","black","orange","purple",
"beige","brown","gray","cyan","red"])
plt.scatter(x, y, c=colors)

#day 2, the age and speed of 15 Bikes

x = np.array([8,8,7,2,17,2,9,5,11,12,9,6,5])
y = np.array([88,87,88,111,85,103,87,
94,78,77,85,86,99])
colors = np.array(["magenta","green","blue",
"yellow","pink","black","orange","purple",
"beige","brown","gray","cyan","red"])
plt.scatter(x, y, c=colors)

plt.show()

the output will be


ColorMap

There are a number of options available for colormaps in Matplotlib module.

A colormap is like a list of colors, where each color has a value that ranges from 0 to 100.

Example 5: Colormap

Code

Here is an example of a colormap.


This colormap is called 'viridis' and as you can see it ranges from 0, which is a purple color, and up to 100, which is a yellow color.

How to Use the ColorMap?

You can specify the colormap with the keyword argument cmap with the value of the colormap, in this case 'viridis' which is one of the built-in colormaps available in Matplotlib.

In addition you have to create an array with values (from 0 to 100), one value for each of the point in the scatter plot.

Example 6: Create a color array, and specify a colormap in the scatter plot.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.show()

the output will be


How to include the colormap in the drawing?

Tthe Colormap can be includedin the drawing by including the plt.colorbar() statement.

Example 7: Include the actual colormap.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.colorbar()
plt.show()

the output will be


Available Options For ColorMaps

You can choose any of the built-in colormaps.

Click Here to See the Options Available For ColorMaps
How to Change the Size?

You can change the size of the dots with the s argument.

Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis.

Example 8: Set your own size for the markers.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
sizes = np.array([20,50,100,200,500,1000,
60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes)

plt.show()

the output will be


Alpha Argument

You can adjust the transparency of the dots with the alpha argument.

Just like colors, make sure the array for sizes has the same length as the arrays for the x- and y-axis.

Example 9: Set your own size for the markers.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.array([7,8,7,2,17,2,9,4,11,12,9,6,5])
y = np.array([86,87,88,111,86,103,87,
94,78,77,85,86,99])
sizes = np.array([20,50,100,200,500,1000,
60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes, alpha=0.5)

plt.show()

the output will be


Combine Color Size and Alpha

You can combine a colormap with different sizes on the dots.

This is best visualized if the dots are transparent.

Example 10: Create random arrays with 200 values for x-points, y-points, colors and sizes.

Code

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randint(200, size=(200))
y = np.random.randint(200, size=(200))
colors = np.random.randint(200, size=(200))
sizes = 10 * np.random.randint(200, size=(200))

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()

plt.show()

the output will be


لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.