Skip to content
Research Atlas

Exploring Patterns · Lesson 4 of 5

Correlations & Scatter Plots

~15 min

The Concept

You plot nitrate at well GW-14 against cumulative rainfall from the nearest rain gauge (RG-03). There's a positive relationship, higher rainfall corresponds to higher nitrate, but it's messy. Then you plot the same data split by season: the spring relationship is strong; the summer relationship is almost flat. The correlation that appeared to be about total rainfall is actually about when the rainfall falls.

'This is why we look at scatter plots,' your supervisor says. 'Correlation coefficients tell you there's a relationship. Scatter plots tell you what kind.'

Pearson's correlation coefficient r (a single number from -1 to +1 that says how tightly two things move together in a straight-line way) measures the strength and direction of a linear relationship between two variables, ranging from −1 (perfect inverse linear relationship) to +1 (perfect positive linear relationship). Zero means no linear relationship, but it does not mean no relationship at all. Two variables can have r = 0 and still be strongly related in a non-linear way.

Always plot your data before computing a correlation. Anscombe's Quartet, four datasets with nearly identical means, variances, and correlation coefficients, but completely different scatter-plot shapes, is the classic demonstration that summary statistics can be misleading without the visual.

The Analogy

Think of two kids on a seesaw. When one goes up, the other reliably goes down, that is a strong negative correlation. If they bounce around with no connection to each other at all, that is close to zero correlation. Correlation just tells you whether two things tend to move together, not why, and not whether one is causing the other.

The Math (optional)

Pearson's correlation coefficient r:

Pearson correlation

r=i=1n(xixˉ)(yiyˉ)i=1n(xixˉ)2i=1n(yiyˉ)2r = \frac{\sum_{i=1}^{n}(x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum_{i=1}^{n}(x_i - \bar{x})^2 \cdot \sum_{i=1}^{n}(y_i - \bar{y})^2}}

r is the covariance of x and y divided by the product of their standard deviations. It ranges from −1 to +1. Values near ±1 indicate strong linear relationships; near 0 indicates weak or no linear relationship.

Code (optional)

correlation_matrix.pypython
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("bluewater_monthly_summary.csv")

# Select water quality and climate variables
cols = ["nitrate_mgL", "rainfall_mm", "temp_C", "water_level_m", "DO_mgL"]
corr = df[cols].corr()

print("Correlation matrix:")
print(corr.round(2))

# Strongest correlation pairs (absolute r > 0.4)
corr_long = corr.stack().reset_index()
corr_long.columns = ["var1", "var2", "r"]
strong = corr_long[(corr_long["r"].abs() > 0.4) & (corr_long["var1"] != corr_long["var2"])]
print("\nStrong correlations (|r| > 0.4):")
print(strong.sort_values("r", ascending=False))

Line by line

  • df[cols].corr() computes Pearson r for every pair of variables at once, the result is a matrix where each cell is one pairwise correlation.
  • Values on the diagonal are always 1.0 (a variable is perfectly correlated with itself).
  • Stacking the matrix and filtering for |r| > 0.4 extracts only the meaningful correlations, making the matrix easier to scan.
  • This is a screening step, strong correlations get plotted as scatter plots next, to confirm the relationship is genuinely linear.

Why Real Researchers Care

Correlation matrices are the standard first step in multivariate environmental analysis. They tell you which variables move together (and might therefore be measuring the same underlying process) and which to include as independent predictors in a regression model. Including two highly correlated predictors in the same model (multicollinearity) causes instability, the correlation matrix warns you in advance.

Quick Check

Q1. Rainfall and nitrate at GW-14 have r = 0.62. What does this tell you, and what does it not tell you?

Your Goal

Given these five paired values of monthly rainfall (mm) and nitrate (mg/L): (45,6.1), (80,9.3), (120,14.8), (30,5.2), (95,11.4), compute the mean of each variable, then by hand (or by code) compute r. Is it positive or negative?

Hint: Compute mean_x and mean_y first. Then for each point compute (xᵢ − x̄)(yᵢ − ȳ), square each deviation separately, sum them, and apply the formula.

Teach It Back

Explain to a colleague why r = 0 does not mean there is no relationship between two variables. Use a sketch or an example.

Previous lesson