# Matplotlib | Scatter

## 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:

### Example

A simple scatter plot:

### Result:

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?

### Example

Draw two plots on the same figure:

### Result:

Note: 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.

## Colors

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

### Example

Set your own color of the markers:

## 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:

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

### Example

Set your own color of the markers:

## 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, 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

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

### Result:

You can include the colormap in the drawing by including the `plt.colorbar()` statement:

### Example

Include the actual colormap:

### Available ColorMaps

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:

### Example

Set your own size for the markers:

## 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:

### Example

Set your own size for the markers:

## 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

Create random arrays with 100 values for x-points, y-points, colors and sizes: