How to Plot List of X,y Coordinates in Matplotlib

Jinku Hu Feb 02, 2024
How to Plot List of X,y Coordinates in Matplotlib

Suppose we have a list of 2-tuple like (x, y), and we need to plot them as they are the (x, y) coordinates.

data = [
    [1, 2],
    [3, 2],
    [4, 7],
    [2, 4],
    [2, 1],
    [5, 6],
    [6, 3],
    [7, 5],
]

Complete codes to plot this list of (x, y) coordinates in Matplotlib,

import matplotlib.pyplot as plt

data = [
    [1, 2],
    [3, 2],
    [4, 7],
    [2, 4],
    [2, 1],
    [5, 6],
    [6, 3],
    [7, 5],
]

x, y = zip(*data)
plt.scatter(x, y)
plt.show()

Matplotlib Scatter Plot list of coordinate

x, y = zip(*data)

It unpacks the data from pairs to lists by using zip function.

plt.scatter(x, y)

We need to create the scatter plot, therefore scatter is the right plot type to be used in this application.

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Matplotlib Scatter Plot