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.

Related Article - Matplotlib Scatter

  • How to Connect Scatterplot Points With Line in Matplotlib
  • comments powered by Disqus