Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Scatter Plots

DS 2023 | Communicating with Data

scatter-plot.png

Overview

A scatter plot uses dots to represent the values of two numeric measurement variables on a cartesian plane.

The domains of the two variables are mapped onto the x and y axes to generate a 2D2D space.

Within this space, each dot on the plot represents an observation, i.e. a row of data.

Use Cases

Scatter plots can be used to identify relationships, trends, patterns, clusters, and outliers among the data points.

A common use case is to plot an independent variable (e.g. time) on the x-axis and a dependent variable on the y-axis.

Scatter plots allow you to visually assess how strongly two variables are correlated.

Frequently, scatter plots feature regression lines.

Like line graphs, the x and y axes of scatter plots are cropped. This is because the purpose of the plot is to show relationship, not magnitude.

Creating a scatter plot from scratch

To understand the design and purpose of a scatter plot, let’s build one from scratch.

We’ll create one out of some random numeric data.

We use NumPy’s random number generator that samples from the normal distribution to create a data frame with 1000 observations of 2 features.

Loading...

To understand the logic of a scatter plot, consider how we’d create one by hand.

<Figure size 640x480 with 1 Axes>

Pandas’ .plot.scatter() method does this for us.

<Figure size 640x480 with 1 Axes>

Because both features are drawn from the normal distribution, they should exhibit no correlation.

Indeed, the result is a random blob of points that cluster around 0, the mean.

A better example

Let’s look at a better example, one involve real data.

We import the Palmer Penguins data set from Seaborn.

This dataset represents body-size measurements for 344 penguins of three species observed on three islands of the Palmer Archipelago, Antarctica.

Loading...

Here is a scatter plot of two features.

<Figure size 640x480 with 1 Axes>

Note that the plot effectively zooms in one the data by cropping the axes.

If the axes are not cropped, you get like this:

<Figure size 640x480 with 1 Axes>

Showing a grouping variable with color

If you want to map the distribution of species onto the figure, you can associate color with the variable.

<Figure size 640x480 with 1 Axes>

The result shows that the observations cluster in the space created by the features.

This indicates that the features may be predictive of species.

Showing a third variable with size

We might also choose to change the size of each data point in proportion to a numeric variable.

Here we invent a feature size_factor to display for demonstration purposes.

We could have just used body_mass_g, but this results in very large circles.

<Figure size 640x480 with 1 Axes>

Showing variables with color and size

It’s actually pretty easy to show four variables in a scatter plot.

Here we use both color and size as channels on which to map a categorical and numeric feature respectively.

Here we bump up the size of the figure to make clearly visible all four variables.

<Figure size 1200x1000 with 1 Axes>

Viewing many scatter plots at once

Often when you have a bunch of measurement variables you will want to do a pairwise comparison of them all.

One quick way to do that is with a scatter matrix (also called a lattice).

Pandas provides this kind of plot through its plotting module.

<Figure size 1000x1000 with 25 Axes>

Note that the function returns a two-dimensional array of Axes objects.

array([[<Axes: xlabel='bill_length_mm', ylabel='bill_length_mm'>, <Axes: xlabel='bill_depth_mm', ylabel='bill_length_mm'>, <Axes: xlabel='flipper_length_mm', ylabel='bill_length_mm'>, <Axes: xlabel='body_mass_g', ylabel='bill_length_mm'>, <Axes: xlabel='size_factor', ylabel='bill_length_mm'>], [<Axes: xlabel='bill_length_mm', ylabel='bill_depth_mm'>, <Axes: xlabel='bill_depth_mm', ylabel='bill_depth_mm'>, <Axes: xlabel='flipper_length_mm', ylabel='bill_depth_mm'>, <Axes: xlabel='body_mass_g', ylabel='bill_depth_mm'>, <Axes: xlabel='size_factor', ylabel='bill_depth_mm'>], [<Axes: xlabel='bill_length_mm', ylabel='flipper_length_mm'>, <Axes: xlabel='bill_depth_mm', ylabel='flipper_length_mm'>, <Axes: xlabel='flipper_length_mm', ylabel='flipper_length_mm'>, <Axes: xlabel='body_mass_g', ylabel='flipper_length_mm'>, <Axes: xlabel='size_factor', ylabel='flipper_length_mm'>], [<Axes: xlabel='bill_length_mm', ylabel='body_mass_g'>, <Axes: xlabel='bill_depth_mm', ylabel='body_mass_g'>, <Axes: xlabel='flipper_length_mm', ylabel='body_mass_g'>, <Axes: xlabel='body_mass_g', ylabel='body_mass_g'>, <Axes: xlabel='size_factor', ylabel='body_mass_g'>], [<Axes: xlabel='bill_length_mm', ylabel='size_factor'>, <Axes: xlabel='bill_depth_mm', ylabel='size_factor'>, <Axes: xlabel='flipper_length_mm', ylabel='size_factor'>, <Axes: xlabel='body_mass_g', ylabel='size_factor'>, <Axes: xlabel='size_factor', ylabel='size_factor'>]], dtype=object)

Looking at correlation

One of the use cases of a scatter plot is that it can give us insight into the amount of correlation two variables have.

Index(['species', 'island', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g', 'sex', 'size_factor'], dtype='str')
Loading...

Another example: Regression

Scatter plots are often used to show regression — the best estimate of a linear function to account for the relationship between x and y.

Let’s look at an example using Seaborn’s tips dataset.

This dataset represents the tips recorded by one waiter over a few months at a single restaurant in New York City, with the bill total and attributes of each dining party.

Loading...

Let’s add an analytical column showing the tip as a percentage of the total bill.

Loading...

Except for a few outliers, it looks like tip as a percent remains about constant.

<Figure size 640x480 with 1 Axes>

If we want to fit a function to predict the tip xx from the total_bill yy, we can use NumPy’s polyfit function.

This returns mm and bb for the function:

y=mx+by = mx + b

where xx and yy are supplied by the two variables in question.

Slope: 0.105, Intercept: 0.92

So, to predict the tip y^\hat{y} of customer with a bill of xx (for this waiter at this restaurant), you could use this formula:

y^=0.105x+0.92\hat{y} = 0.105x + 0.92

Here’s what that function looks like, plotted over the data.

Loading...
<Figure size 640x480 with 1 Axes>

What if we want to show the difference between observation by category in the plot?

Here we color each point by its sex.

<Figure size 640x480 with 1 Axes>

If we wanted to plot independent regression lines for each group, we could compute them independently and plot them on the same axes object.

Loading...
<Figure size 640x480 with 1 Axes>

We can see that in this sample males tip slightly more than females.

This is consistent with the median values for each group:

sex Female 2.75 Male 3.00 Name: tip, dtype: float64

The colors in the above plot are mismatched.

We’d like to have the lines and dots match color for each group.

Here’s a way to achieve that goal.

<Figure size 640x480 with 1 Axes>

Anscombe’s Quartet

To conclude, let’s create scatter plots for Anscombe’s Quartet.

All four of these data sets have the same summary statistics.

Loading...

Let’s define a convenient regression function.

And now let’s plot them in a grid.

<Figure size 800x600 with 4 Axes>