Module 4 · Beginner · Functions and graphs

Coordinates and the Cartesian plane

Coordinates explained for beginners: the x and y axes, plotting points, the four quadrants, distance and midpoint, and why every row of data is a point.

By theprogrammers team · · 6 min read

How do you tell a friend exactly where you are in a cinema? "Row F, seat 12." Two pieces of information, and they can walk straight to you. Maps do the same with latitude and longitude, chess with "e4", spreadsheets with "B7".

Mathematics uses the same trick to describe any position on a flat surface: two numbers, called coordinates. The grid they live on is the Cartesian plane, named after the French mathematician René Descartes. It's the canvas for every graph you'll ever draw, and in data science, it's how you see data: every row of a table with two numbers is a point.

This lesson explains the axes, plotting points, the four quadrants, and how to measure the distance between two points, which is the idea behind recommendation systems and the KNN algorithm.

What you need first: negative numbers and squares and square roots.

Key terms

  • Axis (plural axes): a number line. The x-axis runs left to right; the y-axis runs bottom to top.
  • Origin: the point where the axes cross, (0, 0).
  • Coordinates: a pair of numbers (x, y) giving a point's position.
  • Quadrant: one of the four regions the axes divide the plane into.
  • Distance: the straight-line length between two points.

The two axes

Take a horizontal number line, the x-axis. Now add a vertical one through its zero, the y-axis. Where they cross is the origin.

  • Along the x-axis, positive numbers go right, negatives go left.
  • Along the y-axis, positive numbers go up, negatives go down.

Plotting a point

A point is written (x, y): the x-coordinate always comes first.

To plot (3, 2): start at the origin, go 3 right, then 2 up. To plot (−2, 4): go 2 left, then 4 up.

A memory trick: "along the corridor, then up the stairs". Always across first, then up or down.

import matplotlib.pyplot as plt

points = {"A": (3, 2), "B": (-2, 4), "C": (-3, -1), "D": (2, -3)}
fig, ax = plt.subplots(figsize=(6, 6))
ax.axhline(0, color="black", lw=1)
ax.axvline(0, color="black", lw=1)
for name, (x, y) in points.items():
    ax.plot(x, y, "o", color="tab:red", ms=8)
    ax.annotate(f"{name} ({x}, {y})", (x, y), xytext=(6, 6), textcoords="offset points")
ax.plot(0, 0, "o", color="black")
ax.annotate("origin (0, 0)", (0, 0), xytext=(6, -14), textcoords="offset points")
for label, (x, y) in {"Quadrant I": (3, 4.3), "Quadrant II": (-4.5, 4.3),
                      "Quadrant III": (-4.5, -4.5), "Quadrant IV": (3, -4.5)}.items():
    ax.text(x, y, label, color="grey")
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.set_xticks(range(-5, 6))
ax.set_yticks(range(-5, 6))
ax.grid(alpha=0.3)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_title("Points on the Cartesian plane")
plt.show()
A grid from −5 to 5 on both axes with the axes drawn in black. Four red points are labelled A (3, 2) top right, B (−2, 4) top left, C (−3, −1) bottom left and D (2, −3) bottom right, with the origin marked and the four regions labelled Quadrant I to IV anticlockwise from the top right.

The order really matters: (3, 2) and (2, 3) are different points.

The four quadrants

The axes split the plane into four quadrants, numbered anticlockwise from the top right:

QuadrantxyExample
I (top right)positivepositiveA (3, 2)
II (top left)negativepositiveB (−2, 4)
III (bottom left)negativenegativeC (−3, −1)
IV (bottom right)positivenegativeD (2, −3)

Points on an axis belong to no quadrant: (0, 5) is on the y-axis, (−4, 0) is on the x-axis.

Distance between two points

How far is A (1, 2) from B (4, 6)?

Draw a right-angled triangle: go across from A to below B, then up to B. The horizontal side is 4 − 1 = 3, the vertical side is 6 − 2 = 4. The distance is the slanted side, the hypotenuse. By Pythagoras:

distance = √(3² + 4²) = √(9 + 16) = √25 = 5

The general distance formula:

d = √((x₂ − x₁)² + (y₂ − y₁)²)

The squares mean the order of subtraction doesn't matter: (−3)² is 9, just like 3².

The midpoint

The point exactly halfway between two points is found by averaging the coordinates:

midpoint = ((x₁ + x₂)/2, (y₁ + y₂)/2)

Between (1, 2) and (4, 6): ((1 + 4)/2, (2 + 6)/2) = (2.5, 4).

Coordinates in Python

import math

a = (1, 2)
b = (4, 6)
print(math.dist(a, b))
print(((a[0] + b[0]) / 2, (a[1] + b[1]) / 2))
5.0
(2.5, 4.0)

math.dist applies the distance formula for you.

Every row of data is a point

Here's why coordinates matter so much in data science. Take a table of customers:

CustomerAgeMonthly spend (₹ thousand)
Asha254
Ben275
Chen5212

Treat age as x and spend as y, and each customer becomes a point: Asha is (25, 4). A scatter plot is exactly this: one point per row. Customers who are similar sit close together.

Distance then measures similarity:

asha, ben, chen = (25, 4), (27, 5), (52, 12)
print(round(math.dist(asha, ben), 2))
print(round(math.dist(asha, chen), 2))
2.24
28.16

Asha is much more similar to Ben than to Chen. That's the entire idea behind the KNN algorithm: to predict something about a new customer, find the customers closest to them.

More than two dimensions

With three columns, each row is a point in 3D space: (x, y, z). With 10 columns, a point in 10-dimensional space. You can't picture 10 dimensions, but the distance formula works the same way: add up all the squared differences, then take the square root.

print(math.dist((1, 2, 3), (4, 6, 3)))
5.0

A row of numbers treated as a point like this is called a vector, the central idea of the Linear algebra course.

One caution: in the customer example, age differences (in years) are much bigger numbers than spend differences, so they dominate the distance. That's why feature scaling puts all columns on a similar scale first.

Practice

  1. In which quadrant is each point: (−5, 2), (3, −7), (−1, −1), (4, 4)?
Show the answer

II, IV, III and I.

  1. Starting at the origin, what are the moves to plot (−4, 3)?
Show the answer

4 left, then 3 up.

  1. Find the distance between (2, 1) and (8, 9).
Show the answer

Across 6, up 8. √(36 + 64) = √100 = 10.

  1. Find the midpoint of (−2, 5) and (6, −1).
Show the answer

((−2 + 6)/2, (5 + (−1))/2) = (2, 2).

  1. Which customer is closer to (30, 6): P (32, 7) or Q (29, 11)?
Show the answer

P: √(4 + 1) ≈ 2.24. Q: √(1 + 25) ≈ 5.10. P is closer.

Key takeaways

  • A point is (x, y): across first, then up or down.
  • The axes divide the plane into four quadrants, numbered anticlockwise from the top right.
  • Distance = √((x₂ − x₁)² + (y₂ − y₁)²), which is Pythagoras; the midpoint averages the coordinates.
  • Every row of numeric data is a point; distance between points measures similarity.
  • The same formula works in any number of dimensions.

Frequently asked questions

Which comes first, x or y?

x always comes first. The point (3, 5) is 3 along the horizontal axis and 5 up the vertical one.

What is the Cartesian plane?

A flat grid formed by a horizontal x-axis and a vertical y-axis crossing at the origin, on which every point is described by a pair of coordinates (x, y).

How do you find the distance between two points?

Subtract the x-coordinates and the y-coordinates, square both differences, add them, and take the square root. In Python, use math.dist(point1, point2).

What to learn next

  1. Learn to read what a graph is telling you in how to read any graph.
  2. Revisit what is a function?.