Loading lesson path
With Pyplot, you can use the scatter() function 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:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])plt.scatter(x, y) plt.show()
The observation in the example above is the result of 13 cars passing by.
Formula
The X - axis shows how old the car is.
The Y - axis shows the speed of the car when it passes.It seems that the newer the car, the faster it drives, but that could be a coincidence, after all we only registered 13 cars.
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?
Draw two plots on the same figure: import matplotlib.pyplot as plt import numpy as np #day one, the age and speed of 13 cars:
Formula
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])plt.scatter(x, y) #day two, the age and speed of 15 cars:
Formula
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])plt.scatter(x, y) plt.show()
The two plots are plotted with two different colors, by default blue and orange, you will learn how to change colors later in this chapter. By comparing the two plots, I think it is safe to say that they both gives us the same conclusion: the newer the car, the faster it drives.
You can set your own color for each scatter plot with the color or the c argument:
Set your own color of the markers:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])plt.scatter(x, y, color = 'hotpink')
Formula
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])plt.scatter(x, y, color = '#88c999') plt.show()
You can even set a specific color for each dot by using an array of colors as value for the c argument:
You cannot use the color argument for this, only the c argument.
Set your own color of the markers:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])colors = np.array(["red","green","blue","yellow","pink","black","orange","purple","beige","brown","gray","cyan","magenta"])
Formula
plt.scatter(x, y, c = colors)plt.show()
The Matplotlib module has a number of available colormaps. A colormap is like a list of colors, where each color has a value that ranges from 0 to 100. 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, up to 100, which is a yellow color.
You can specify the colormap with the keyword argument cmap with the value of the colormap, in this case 'viridis'
Formula
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 point in the scatter plot:
Create a color array, and specify a colormap in the scatter plot:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
Formula
plt.scatter(x, y, c = colors, cmap ='viridis')plt.show()
You can include the colormap in the drawing by including the plt.colorbar() statement:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0,10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])
Formula
plt.scatter(x, y, c = colors, cmap ='viridis')plt.colorbar() plt.show()
Formula
You can choose any of the built - in colormaps: