Get startedGet started for free

Making a scatter plot

1. Making a scatter plot

Previously, we learned how to make line plots using Matplotlib. In this chapter, we'll learn about several additional plot types, starting with scatter plots.

2. Mapping Cell Phone Signals

We now know that Freddie Frequentist is the one who kidnapped Bayes, the Golden Retriever. Now we need to learn where he is hiding. Our friends at the police station have acquired cell phone data, which gives some of Freddie's locations over the past three weeks. The locations are given as a set of x and y coordinates on a map. In order to visualize these coordinates, we'll use a scatter plot.

3. What is a scatter plot?

A scatter plot allows us to show where each data point sits on a grid. Line plots let us visualize ordered data points, but scatter plots are a great way of viewing un-ordered points.

4. What is a scatter plot?

For example, suppose we want to learn about how Golden Retrievers grow during their first year of life. We might measure the heights of many puppies of different ages. We could then plot the height of each puppy versus its age.

5. Creating a scatter plot

In matplotlib, we create scatter plots by using the command plt-dot-scatter. The first argument is the x-data (in this example, this is the age of the puppy). The second argument is the y-data (in this example, this is the height of the puppy). We can then add labels and use plt-dot-show, just as we would for a line plot.

6. Keyword arguments

Many of the same keyword arguments that we used in line plots will work in scatter plots. In this example, we use the keyword argument "color" to change the marker color to green, and the keyword argument "marker" to change the marker shapes to squares.

7. Changing marker transparency

If we try to plot many points on the same scatter plot, things can get chaotic. We can't see if we've plotted many layers of points or just a single layer. We can make it easier to read by using a new keyword: alpha. Alpha accepts a number between 0 and 1 and makes each marker transparent. The smaller the alpha parameter, the more transparent the dot. Now we know that the darker areas have many points, while the lighter areas have fewer points.

8. Let's practice!

We've learned a new function: plt-dot-scatter. We can use it to plot two-dimensional data. We can use many of the old keyword arguments that we learned for plt-dot-plot along with a new argument: alpha, which controls transparency. Let's practice!