Python plot a list of points

I have a list of pairs (a, b) that I would like to plot with matplotlib in python as actual x-y coordinates. Currently, it is making two plots, where the index of the list gives the x-coordinate, and the first plot's y values are the as in the pairs and the second plot's y values are the bs in the pairs.

To clarify, my data looks like this: li = [(a,b), (c,d), ... , (t, u)] I want to do a one-liner that just calls plt.plot() incorrect. If I didn't require a one-liner I could trivially do:

xs = [x[0] for x in li]
ys = [x[1] for x in li]
plt.plot(xs, ys)

How can I get matplotlib to plot these pairs as x-y coordinates?


Sample data

# sample data
li = list(zip(range(1, 14), range(14, 27)))

li → [(1, 14), (2, 15), (3, 16), (4, 17), (5, 18), (6, 19), (7, 20), (8, 21), (9, 22), (10, 23), (11, 24), (12, 25), (13, 26)]

Incorrect Plot

plt.plot(li)
plt.title('Incorrect Plot:\nEach index of the tuple plotted as separate lines')

Python plot a list of points

Desired Plot

  • This produces the correct plot, but to many lines of code are used to unpack li. I need to unpack and plot with a single line of code, not multiple list-comprehensions.
xs = [x[0] for x in li]
ys = [x[1] for x in li]
plt.plot(xs, ys)
plt.title('Correct Plot:\nBut uses to many lines to unpack li')

Python plot a list of points

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: 

    • Matplotlib 
    • numpy 

    Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. To plot any data the most basic step starts with creating or loading it, this article discusses all the ways of passing the data to be plotted as list. While passing data as list one important thing to be kept in mind is to keep X,Y Lists of the same size, otherwise it will raise a ValueError. 

    Examples shown in this article use scatter plot, the same approaches can be applied to any other type of graph.

    Method 1: Naive method

    In this method, the coordinates are simply passed as two lists.

    Approach

    • Import module
    • Create a list for X coordinates
    • Create a list for Y coordinate
    • Pass these two lists to plot the function

    Example: 

    Python3

    import matplotlib.pyplot as plt

    x = [1, 2, 3, 4, 5, 6, 7, 8]

    y = [2, 3, 1, 3, 1, 4, 2, 3]

    plt.scatter(x, y)

    plt.show()

    Output:

    Python plot a list of points

    Method 2: Using numpy array

    Function used: arrange()

    Syntax: np.arange(start,end,step)

    Parameters:

    • Start: starting value
    • End: ending value
    • Step: step size, by default it is 1.

    Approach

    • Import module
    • Create numpy array for coordinates
    • Pass these arrays to plot

    Example:

    Python3

    import numpy as np

    import matplotlib.pyplot as plt

    x = np.arange(1, 11, 1)  

    y = np.arange(10, 0, -1)  

    plt.scatter(x, y)

    plt.show()

    Output:

    Python plot a list of points

    Method 3: List of lists

    Creating lists of list of all points to be plotted can also one of the ways of achieving our requirement. After creating such a list it cannot be directly passed to plot, first a transpose needs to taken to get x and y coordinates.

    Approach

    • Import module
    • Create list of lists for coordinates
    • Take transpose
    • Pass the coordinates to the plot

    Example

    Python3

    import numpy as np

    import matplotlib.pyplot as plt

    data = np.array([

        [1, 4],

        [2, 2],

        [3, 7],

        [4, 6],

        [5, 0],

        [6, 3]

    ])

    x, y = data.T

    plt.scatter(x, y)

    plt.show()

    Output: 

    Python plot a list of points


    How do you plot a list of points in Python?

    Practical Data Science using Python.
    Set the figure size and adjust the padding between and around the subplots..
    Create lists of x and y data points..
    Plot x and y data points with red color and starred marker..
    Set some axis properties..
    Iterate x and y to show the coordinates on the plot..

    How do I plot a series of points in matplotlib?

    Approach.
    Import module..
    Create a list for X coordinates..
    Create a list for Y coordinate..
    Pass these two lists to plot the function..

    Can you plot a list in Python?

    Today, we will see how to plot a list in Python. We will use the matplotlib library. It is a popular Python library for data visualization. Using this, we can easily plot a list in a few lines of code.

    How do you plot multiple data in Python?

    You can plot multiple lines from the data provided by an array in python using matplotlib. You can do it by specifying different columns of the array as the x and y-axis parameters in the matplotlib. pyplot. plot() function.