Search code examples
pythonmatplotlibscatter-plot

How to scatter plot a two dimensional list


How can I scatter plot a list of pairs with each axis of the plot representing one of the value in the pair in python? My list looks like this

[(62725984, 63548262), (64797631, 64619047), (65069350, 65398449), (58960696, 57416785), (58760119, 58666604), (60470606, 61338129), (60728760, 59001882)]


Solution

  • This should be easy. You can extract the pair into two variables as follows:

    x,y = zip(*<name_of_your_2d_list>)
    

    Also, you can pass the same to scatter function as

    matplotlib.pyplot.scatter(*zip(*<name_of_your_2d_list>).

    Try the following. It should work:

    import matplotlib.pyplot, pylab
    
    data = [(62725984, 63548262), (64797631, 64619047), (65069350, 65398449), (58960696, 57416785), (58760119, 58666604), (60470606, 61338129), (60728760, 59001882)]
    
    matplotlib.pyplot.scatter(*zip(*data)) 
    matplotlib.pyplot.show()