Plot List of X,y Coordinates in Matplotlib

Jinku Hu Dec 10, 2020 Jan 28, 2020 Matplotlib Matplotlib Scatter Plot
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

Related Article - Matplotlib Scatter Plot

  • Matplotlib Label Scatter Points
  • Plot Points in Matplotlib
  • Set Color for Scatterplot in Matplotlib
  • Make the Legend of the Scatter Plot in Matplotlib