Data Visualization
Coordinate Plotting
XY Coordinates
Plotting Techniques
Graphing Data

Plotting a list of x, y coordinates

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Plotting a list of x, y coordinates is one of the most common ways to visualize raw numeric data. The job is usually simple: unpack the coordinate pairs, decide whether the data should be shown as independent points or a connected path, and then render it with a plotting library.

In Python, matplotlib is the standard tool for this task, and it handles both scatter plots and line plots cleanly.

Start With A List Of Pairs

Suppose your data looks like this:

python
points = [(1, 2), (2, 3), (3, 5), (4, 4)]

Each tuple contains one x-value and one y-value. Most plotting APIs want those values in separate sequences, so the first step is unpacking.

A readable approach is a list comprehension:

python
1import matplotlib.pyplot as plt
2
3points = [(1, 2), (2, 3), (3, 5), (4, 4)]
4
5x_values = [x for x, y in points]
6y_values = [y for x, y in points]
7
8plt.scatter(x_values, y_values)
9plt.xlabel("x")
10plt.ylabel("y")
11plt.title("Scatter plot of coordinates")
12plt.show()

This is a good starting pattern because the transformation is explicit and easy to debug.

Use zip For Cleaner Unpacking

If the input list is already well formed, zip(*points) is a compact alternative:

python
1import matplotlib.pyplot as plt
2
3points = [(1, 2), (2, 3), (3, 5), (4, 4)]
4x_values, y_values = zip(*points)
5
6plt.plot(x_values, y_values, marker="o")
7plt.xlabel("x")
8plt.ylabel("y")
9plt.title("Line plot of coordinates")
10plt.show()

This version draws a line through the points in their existing order. That is useful when the sequence itself has meaning, such as a path, a time series, or a sampled curve.

Scatter Plot Versus Line Plot

Choosing the right plot type matters as much as getting the code right.

Use a scatter plot when each point is an independent observation and the order does not matter. Use a line plot when the order of the coordinate pairs represents a path or continuous trend.

If you choose a line plot for unrelated observations, the graph can accidentally imply a relationship that is not really there.

Adding Labels And Grid Lines

Small presentation details make the chart easier to read:

python
1import matplotlib.pyplot as plt
2
3points = [(0, 0), (1, 1), (2, 4), (3, 9)]
4x_values, y_values = zip(*points)
5
6plt.scatter(x_values, y_values, color="tomato", s=80)
7plt.grid(True)
8plt.xlabel("Input")
9plt.ylabel("Output")
10plt.title("Coordinate visualization")
11plt.show()

Axis labels and a title are not just decoration. They make the plot understandable when you revisit it later or share it with someone else.

Validating The Input

A lot of plotting bugs come from malformed data rather than from the plotting library. For example, this input is inconsistent:

python
points = [(1, 2), (3,), (4, 5)]

One tuple does not contain both coordinates, so unpacking fails. If your points come from a file, an API, or user input, validate them first:

python
1points = [(1, 2), (2, 3), (3, 5)]
2
3for point in points:
4    if len(point) != 2:
5        raise ValueError("Each point must contain exactly two values")

That small guard is often enough to catch bad data early.

Common Pitfalls

The biggest mistake is trying to pass a raw list of tuples directly into a plotting function that expects separate x and y sequences. Unpack the coordinates first.

Another pitfall is using zip(*points) without checking whether the list is empty. An empty list needs special handling because there is nothing to unzip.

A third issue is choosing the wrong visualization. A line plot suggests continuity and ordering, while a scatter plot suggests independent points. Pick the one that matches the meaning of the data.

Summary

  • Unpack coordinate pairs into separate x and y sequences before plotting.
  • Use scatter for independent observations and plot for ordered paths or trends.
  • 'zip(*points) is a concise way to unpack a well-formed list of pairs.'
  • Add labels and grid lines so the result is readable.
  • Validate the coordinate shape when the input may be messy.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.