Search code examples
pythonpython-3.xnumpyscatter-plot

Create a scatter plot from an ndarray using the position in the row as the x-axis value and the value in the array for the y-axis


much like the title says I am trying to create a graph that shows 1-6 on the x-axis (the values position in the row of the array) and its value on the y-axis. A snippet from the array is shown below, with each column representing a coefficient number from 1-6.

[0.99105 0.96213 0.96864 0.96833 0.96698 0.97381]  
[0.99957 0.99709 0.9957  0.9927  0.98492 0.98864]  
[0.9967  0.98796 0.9887  0.98613 0.98592 0.99125]  
[0.9982  0.99347 0.98943 0.96873 0.91424 0.83831]  
[0.9985  0.99585 0.99209 0.98399 0.97253 0.97942]

It's already set up as a numpy array. I think it's relatively straightforward, just drawing a complete mental blank. Any ideas?


Solution

  • Do you want something like this?

    a = np.array([[0.99105, 0.96213, 0.96864, 0.96833, 0.96698, 0.97381], 
                  [0.99957, 0.99709, 0.9957,  0.9927,  0.98492, 0.98864],  
                  [0.9967,  0.98796, 0.9887,  0.98613, 0.98592, 0.99125], 
                  [0.9982,  0.99347, 0.98943, 0.96873, 0.91424, 0.83831],  
                  [0.9985,  0.99585, 0.99209, 0.98399, 0.97253, 0.97942]])
    
    import matplotlib.pyplot as plt
    plt.scatter(x=np.tile(np.arange(a.shape[1]), a.shape[0])+1, y=a)
    

    output:

    enter image description here

    Note that you can emulate the same with groups using:

    plt.plot(a.T, marker='o', ls='')
    x = np.arange(a.shape[0]+1)
    plt.xticks(x, x+1)
    

    output:

    enter image description here