# Scatterplots and Relationships

Plotting two variables against each other to reveal association, nonlinearity, clustering and outliers before any correlation coefficient is computed.

**Rests on:** Descriptive Statistics

**Leads to:** DBSCAN and OPTICS, Q-Q Plots and Normality

*Mathematical Foundations · Statistical Inference*


## Why learn this

Before you can make any prediction about how two things in the real world relate, you must see if they move together. A scatterplot is your first tool for this job. It shows you clearly whether larger houses cost more, whether a sensor reading predicts a machine failure, or whether customers who buy one item tend to buy another. If you skip this step, you risk fitting a straight line to data that actually curves, or declaring a relationship exists when it is just caused by one unusual data point. Seeing the shape of the data visually prevents you from trusting numbers that hide complex patterns.


## The intuition

Imagine you have two lists of numbers, each number paired with its partner: for each house, its size in square feet and its price in lakhs. You want to know if bigger houses cost more. You could look at the two lists side by side, but with fifty pairs your eyes blur. Instead, you mark a dot on a page for each pair: the left-right position is size, and the up-down position is price. With all fifty dots down, patterns jump out. If the dots slant upward from left to right, bigger houses do tend to cost more — that is an association. If the dots form a loose cloud with no slant, size tells you little about price. If the dots curve, like a smile, then a straight-line summary would mislead you. A scatterplot is your naked-eye view of a relationship before any number tries to summarise it. The key habit: always plot first, even when you plan to compute a single number later to describe the strength of that link. The plot shows you what that number hides.


## An analogy

Picture yourself as a detective entering a room where the floor is a giant grid: the left edge is labelled in years of experience, and the bottom edge in salary. Each person in the company places a coin on the grid at the point matching their experience and salary. You stand back and look at the pattern of coins. Do they form a rising diagonal (more experience, more salary)? Do they cluster into two separate blobs (one group well paid, another poorly)? Is there a single coin far from everyone else (an unusual value, maybe the CEO, maybe an error)? That is exactly what a scatterplot does — it turns a table of numbers into a pattern of dots you can read with your eyes. The analogy works until you try to scale it: a real dataset has hundreds or thousands of points, and dots overlap. You need to add transparency or use small random jitter so you can see the density underneath. Also, the grid has no units — you must label axes carefully, or a squashed or stretched scale will make a slope look steeper or shallower than it is. The detective's eye is the point, not the coordinates.


## Definition

A scatterplot is a graph that shows each observation as a dot positioned by two variables — one on the horizontal axis, one on the vertical — so that patterns of association, curvature, clustering, and unusual values are visible at a glance.


## How it connects

You have not yet studied statistics formally, but this topic connects directly to two neighbours in your library. A scatterplot is the visual companion to Summary Numbers: while an average and a measure of spread summarise one variable, a scatterplot reveals how two variables move together. When you later study the strength of links between variables, you will see they turn the scatterplot's shape into a single number — and you will learn why that number can be misleading (see Anscombe's quartet, a famous set of datasets with identical summary numbers but different visual shapes). When you move to fitting lines, the scatterplot is the first check you draw to see whether a straight line is even appropriate. In short, every branch of Statistical Inference — from testing ideas to complex forecasting methods — assumes you have looked at the data first.


## The mathematics

A scatterplot is a way to draw a picture of how two numbers move together. To make this picture, you need pairs of numbers. Each pair comes from one observation, such as one person or one day. Let us call the first number in the pair x and the second number y.

The math here is just coordinate geometry. This is the same system used to find a spot on a map using latitude and longitude. You draw two lines that cross at right angles. The horizontal line is the x-axis. The vertical line is the y-axis. For each observation, you take its x value and move along the horizontal line to that number. Then you take its y value and move up or down to that height. You place a dot there. That dot represents one pair of (x, y).

Once all the dots are on the page, you look at their shape. The shape tells you about the relationship between x and y.

If the dots go up from left to right, x and y move in the same direction. When x gets larger, y also tends to get larger. This is called a positive association. Passage 1 confirms this: "Positive correlation ... means that when x grows, then y is also expected to grow."

If the dots go down from left to right, x and y move in opposite directions. When x gets larger, y tends to get smaller. This is called a negative association. Passage 1 states: "Negative correlation means that as x increases, then y decreases."

If the dots do not form a straight line but curve, the relationship is nonlinear. If the dots cluster into separate groups, the data has distinct subgroups. A single dot far away from the main group of dots is an outlier.

These visual patterns are the foundation for numerical summaries. Later, you will calculate a number called the correlation coefficient. This number compresses the scatterplot's shape into a single value between -1 and 1. Passage 1 defines this as:
corr[x,y] = Cov[x,y] / sqrt(V[x] * V[y])

However, the single number cannot show you if the shape is curved or if one outlier is driving the result. Only the plot can show that. The plot shows the actual data points; the correlation hides them.


## A worked example

Let us draw a scatterplot for five employees. We want to see how years of experience (x) relates to monthly salary in thousands of rupees (y).

We have five pairs of numbers:
Employee A: x = 2, y = 25
Employee B: x = 5, y = 40
Employee C: x = 3, y = 30
Employee D: x = 8, y = 55
Employee E: x = 1, y = 45

Step 1: Draw the axes. Label the horizontal axis "Years of Experience" and the vertical axis "Salary (thousands)".

Step 2: Plot Employee A. Find x = 2 on the horizontal axis. Move up to y = 25 on the vertical axis. Place a dot at this spot. This dot is low on the left side.

Step 3: Plot Employee B. Find x = 5 on the horizontal axis. Move up to y = 40. Place a dot here. This dot is higher and further right than A.

Step 4: Plot Employee C. Find x = 3 on the horizontal axis. Move up to y = 30. Place a dot here. This dot sits between A and B in both height and width.

Step 5: Plot Employee D. Find x = 8 on the horizontal axis. Move up to y = 55. Place a dot here. This is the highest and rightmost dot.

Step 6: Plot Employee E. Find x = 1 on the horizontal axis. Move up to y = 45. Place a dot here. This dot is very high up but far to the left.

Result: Look at dots A, B, C, and D. They form a line going up from left to right. This shows that more experience is associated with higher salary. This is a positive association.

Now look at dot E. It has low experience (1 year) but high salary (45 thousand). It sits far above the imaginary line formed by the other four dots. This dot is an outlier. The scatterplot reveals this unusual case immediately. A list of numbers would not show this pattern as clearly.


## From scratch, in Python

```python
Now let's see how to create a scatterplot in Python, using only the standard library. First, we need two things: a list of x-values and a list of y-values. A list in Python is an ordered collection of items, written with square brackets: [1, 2, 3]. We'll use the matplotlib library to draw the plot; it's the most common library for this task. We'll also use the random module to add a little jitter to the points so we can see overlaps, but first, let's just plot the raw points.
```


## What the code does

We import matplotlib.pyplot as plt. We create two lists: x_values and y_values. We call plt.scatter(x_values, y_values) which places a dot for each pair. Then we label the axes and show the plot. The scatter function loops over each index and draws a dot at (x_values[i], y_values[i]). That is the entire operation.


## Common misconceptions

**A correlation coefficient tells you everything about the relationship** — If I compute the correlation coefficient and it's 0.8, I know the relationship is strong and linear.

Anscombe's quartet is the famous demonstration: four scatterplots with identical correlation coefficients (0.816) but vastly different shapes — one is a straight line, one is a curve, one has an outlier, one is a vertical line with a single point. The number hides all of that. In fact: Always plot the data first. Use the correlation coefficient only as a summary of the linear association after you have confirmed the scatterplot looks roughly elliptical and straight.

**Scatterplots are optional if you have the data** — I have the data in a table, so I can just compute the statistics directly without drawing a picture.

Summary statistics like means and correlations are pure numbers; they cannot show you curvature, clusters, or outliers. A table of numbers can hide the fact that the relationship changes direction halfway through. In fact: A scatterplot is a required step in any exploratory data analysis. It is cheap to make and it protects you from making false assumptions.

**An upward-sloping scatterplot always means one variable causes the other** — If ice cream sales and drowning deaths both go up in summer, a scatterplot of the two will slope upward, so ice cream causes drownings.

Correlation does not imply causation. A third variable (hot weather) drives both. The scatterplot only shows association, not a causal mechanism. In fact: Use the scatterplot to see association, but never claim a causal link from it alone. Causal inference needs additional methods like experimentation or careful control.

**Outliers are always bad and should be removed** — Any dot far from the rest is an error and I should delete it before analysis.

An outlier might be the most interesting point — a fraud case, a sensor malfunction, or a genuine rare event. Removing it silently can hide the truth. In fact: Investigate outliers before removing anything. Ask whether the data was recorded correctly, and consider analysing with and without the outlier to see how conclusions change.

**A scatterplot's shape is unaffected by how you scale the axes** — Whether I draw the axes from 0 to 100 or from 40 to 60, the scatterplot looks the same.

Changing axis limits or stretching one axis can make a slope look steeper or flatter, or exaggerate the appearance of clustering. A scatterplot is not invariant to scale. In fact: Always label axes with units and ranges. Compare plots with consistent scales when comparing groups, and be aware that two plots of the same data can look very different if axes are scaled differently.


## In short

A scatterplot is your first and best look at how two variables relate. Always plot the data before computing any summary statistic. Look for direction (slope), shape (straight vs curved), clusters, and outliers. A correlation coefficient compresses the plot into a number, but it cannot show you what the plot reveals — so the plot comes first. This habit will serve you through every later topic in statistical inference and every AI application you build.


## Where this is used

Scatterplots are not just classroom tools — they are used everywhere in building and debugging AI systems. In large language models, when engineers want to understand whether a model's internal representation captures a concept (like sentiment), they plot the model's embedding values for many sentences on a scatterplot (often after reducing dimensions with PCA) and look for clusters. If positive sentences cluster together, the model has learnt something useful. In small models running on devices, such as a voice assistant that spots a keyword on a phone, engineers plot the model's predicted probability of 'wake word' against the sensor input across many test recordings. They look for scattered points that indicate the model is uncertain or misclassifying. In retrieval-augmented generation, the system searches for relevant documents by comparing embeddings — a scatterplot of query embeddings versus document embeddings reveals whether the retrieval is grouping related topics or sending the search astray. In agentic systems that plan and call tools, a developer might plot the reward or success rate of the agent across different environments; a scatterplot of performance vs time step can show when an agent plateaus or diverges. In everyday applied machine learning — recommendation, vision, forecasting, fraud detection — scatterplots are the first diagnostic: plot predicted values against actual values. Perfect predictions lie on a straight diagonal; deviation from that line shows bias or noise. When building such systems, a developer uses a plotting library (like Matplotlib or Seaborn in Python) to generate these plots in notebooks. What breaks when this is done badly is everything: if you skip the scatterplot and go straight to a correlation coefficient, you might fit a linear model to a curved relationship. If you ignore an outlier, your model's loss function will be distorted. The plot is the canary in the coal mine.


---

Read this on the web: https://basavapurushottam.com/learn-ai/scatterplots-and-relationships
