TExcelTAD
Home › Machine Learning › ML Scatter Plot

ML Scatter Plot

Histograms show one variable at a time. Machine learning is mostly about relationships between variables: does studying longer raise test scores? Do bigger flats cost more rent? The quickest way to see a relationship between two numeric variables is a scatter plot: one dot per example, with one variable on the x-axis and the other on the y-axis.

A first scatter plot

Here are 12 students: how many hours they studied for an exam and the score they got.

python · try it yourself
import matplotlib.pyplot as plt

hours = [1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7, 8, 9]
score = [52, 55, 61, 58, 67, 70, 68, 76, 79, 77, 85, 90]

plt.scatter(hours, score)
plt.xlabel("Hours studied")
plt.ylabel("Exam score")
plt.title("Study time vs exam score")
plt.show()
print(f"Plotted {len(hours)} students")
Plotted 12 students

The dots rise from bottom-left to top-right: more study, higher score. That is a positive relationship. If the dots fell from top-left to bottom-right (say, hours of phone use vs score) it would be a negative relationship. A shapeless cloud means no relationship.

Measuring the relationship: correlation

The Pearson correlation coefficient r puts a number on how close the dots are to a straight line:

r Meaning
close to +1 strong positive straight-line relationship
close to 0 no straight-line relationship
close to −1 strong negative straight-line relationship
python · try it yourself
import numpy as np

hours = np.array([1, 2, 2.5, 3, 4, 4.5, 5, 6, 6.5, 7, 8, 9])
score = np.array([52, 55, 61, 58, 67, 70, 68, 76, 79, 77, 85, 90])
phone = np.array([6, 5.5, 5, 5, 4, 4.5, 3, 3, 2.5, 2, 1.5, 1])

print("hours vs score:", np.corrcoef(hours, score)[0, 1].round(3))
print("phone vs score:", np.corrcoef(phone, score)[0, 1].round(3))
hours vs score: 0.987
phone vs score: -0.964

np.corrcoef returns a 2×2 matrix; the value at [0, 1] is the correlation between the two inputs.

Note: Correlation is not causation. Phone time and score are strongly (negatively) related here, but the data alone cannot tell you that phones cause lower scores.

Larger random data sets

Scatter plots get more interesting with more points. Let's simulate 500 flats where rent grows with size, plus random noise:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(5)
size_m2 = rng.normal(70, 20, size=500)
rent = 300 + 12 * size_m2 + rng.normal(0, 120, size=500)

plt.scatter(size_m2, rent, s=10, alpha=0.5)
plt.xlabel("Flat size (m²)")
plt.ylabel("Monthly rent")
plt.title("500 simulated flats")
plt.show()
print("correlation:", np.corrcoef(size_m2, rent)[0, 1].round(3))
correlation: 0.883

s=10 makes the dots small and alpha=0.5 makes them half transparent, so you can see where many dots overlap.

Colouring by a category

A third variable can be shown with colour. Here we colour every flat by whether it has a balcony:

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(8)
size_m2 = rng.uniform(30, 110, size=120)
balcony = rng.integers(0, 2, size=120)          # 0 = no, 1 = yes
rent = 250 + 11 * size_m2 + 150 * balcony + rng.normal(0, 60, size=120)

for value, colour, label in [(0, "tab:blue", "no balcony"), (1, "tab:orange", "balcony")]:
    mask = balcony == value
    plt.scatter(size_m2[mask], rent[mask], c=colour, label=label, s=20)
plt.xlabel("Flat size (m²)")
plt.ylabel("Monthly rent")
plt.legend()
plt.show()
print("flats with a balcony:", int(balcony.sum()), "of", len(balcony))
flats with a balcony: 65 of 120

The orange dots sit above the blue ones: at the same size, flats with a balcony cost more. Spotting a pattern like this in a plot is often the first hint about which features a model should use.

When there is no relationship

python · try it yourself
import numpy as np

rng = np.random.default_rng(2)
shoe_size = rng.normal(42, 2, size=300)
iq = rng.normal(100, 15, size=300)
print("shoe size vs IQ:", np.corrcoef(shoe_size, iq)[0, 1].round(3))
shoe size vs IQ: 0.007

Independent random values give a correlation close to zero: their scatter plot would be a round, featureless cloud.

Test yourself

On a scatter plot, dots that fall from the top-left to the bottom-right show…

  1. a positive relationship
  2. a negative relationship
  3. no relationship
  4. a normal distribution

What does a Pearson correlation of about 0 tell you?

  1. The variables are identical
  2. There is no straight-line relationship
  3. One variable causes the other
  4. The data contains errors

Which Matplotlib function draws a scatter plot?

  1. plt.plot_points()
  2. plt.hist()
  3. plt.scatter()
  4. plt.dots()