Search code examples
matplotlibxticks

want x-ticks in the order of the values shown in array X


I have:

X = [1,5,3,4,2]

Y = [10, 14, 11, 9, 10]

plt.plot(X,Y,'o') 

plots x-axis ticks from 1 to 5.

I want the x-axis to show values 1,5,3,4,2 in this order.

Is there a way?


Solution

  • Yes, you can if you make the X variable categorical.

    Use strings:

    X = [1,5,3,4,2]
    Y = [10, 14, 11, 9, 10]
    
    plt.plot([str(x) for x in X], Y, 'o')
    

    enter image description here