Matplotlib Scatter
Learn all about Matplotlib Scatter in this comprehensive tutorial.
- •With Pyplot, you can use the scatter() function to draw a scatter plot.
- •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?
- •You can set your own color for each scatter plot with the color or the c argument:
- •You can even set a specific color for each dot by using an array of colors as value for the c argument:
- •The Matplotlib module has a number of available colormaps.
- •You can change the size of the dots with the s argument.
- •You can adjust the transparency of the dots with the alpha argument.
- •You can combine a colormap with different sizes of the dots.
Creating Scatter Plots
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:
The observation in the example above is the result of 13 cars passing by.
The X-axis shows how old the car is.
The Y-axis shows the speed of the car when it passes.
Are there any relationships between the observations?
It seems that the newer the car, the faster it drives, but that could be a coincidence, after all we only registered 13 cars.
Compare 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?
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.
Colors
You can set your own color for each scatter plot with the color or the c argument:
Color Each Dot
You can even set a specific color for each dot by using an array of colors as value for the c argument:
ColorMap
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' 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:
You can include the colormap in the drawing by including the plt.colorbar() statement:
You can choose any of the built-in colormaps:
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:
Alpha
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:
Combine Color Size and Alpha
You can combine a colormap with different sizes of the dots. This is best visualized if the dots are transparent:
Module quiz
2 questionsWhich of the following is true about Matplotlib Scatter?
What is the most common pitfall when working with Matplotlib Scatter?
Answer all questions to submit.