Python scatter plot 2 dimensional array

19,860

Try using a meshgrid to define your point locations, and don't forget to index into your NumPy array properly (with [x,y] rather than [x][y]):

x, y = numpy.meshgrid(x,y)
plt.scatter(x,y,c=test_data[x,y])
plt.show()

enter image description here

Share:
19,860

Related videos on Youtube

xnx
Author by

xnx

Updated on June 22, 2022

Comments

  • xnx
    xnx almost 2 years

    I'm trying to do something that I think should be pretty straight forward but I can't seem to get it to work.

    I'm trying to plot 16 byte values measured over time to see how they change. I'm trying to use a scatter plot to do this with: x axis being the measurement index y axis being the index of the byte and the color indicating the value of the byte.

    I have the data stored in a numpy array where data[2][14] would give me the value of the 14th byte in the 2nd measurement.

    Every time I try to plot this, I'm getting either:

    ValueError: x and y must be the same size
    IndexError: index 10 is out of bounds for axis 0 with size 10
    

    Here is the sample test I'm using:

    import numpy
    import numpy.random as nprnd
    import matplotlib.pyplot as plt
    
    #generate random measurements
    # 10 measurements of 16 byte values
    x = numpy.arange(10)
    y = numpy.arange(16)
    test_data = nprnd.randint(low=0,high=65535, size=(10, 16))
    
    #scatter plot the measurements with
    # x - measurement index (0-9 in this case)
    # y - byte value index (0-15 in this case) 
    # c = test_data[x,y]
    
    plt.scatter(x,y,c=test_data[x][y])
    plt.show()
    

    I'm sure it is something stupid I'm doing wrong but I can't seem to figure out what.

    Thanks for the help.