Search code examples
pythonmatplotlibplotxticks

I have a problem with x tick labels in plotting in python


Why xticklabels do not coincide with the real x_ticks?

 A=[0.4533333333333333,
 0.6033333333333334,
 0.7033333333333335,
 0.59,
 0.49666666666666665,
 0.49666666666666665,
 0.4166666666666667,
 0.7600000000000001]

EB_A =[0.04800000000000001,
 0.06497800193279869,
 0.06003509868180174,
 0.05379449471770337,
 0.055139672522411476,
 0.06977298499632904,
 0.06328550572939434,
 0.04762818959864468]

currentNamesList = ['0','4h','6h','16h','24h','48h','4d','6d']
listOfSteps1=[0,4,6,16,24,48,96,144]
fig, ax = plt.subplots()
ax.plot(listOfSteps1, A,label= 'ATRA')
ax.errorbar(listOfSteps1, A, yerr = EB_A)
plt.xlabel('Time',fontsize=12)
plt.ylabel('Average Correlation' , fontsize=12)
ax.set_xticklabels(currentNamesList)
plt.legend(loc= 'upper left')
plt.tight_layout()
plt.show()

enter image description here

One can see that the xticklables are not exactly on listOfSteps1? Do you know why?


Solution

  • You need to replace:

    ax.set_xticklabels(currentNamesList)
    

    by

    plt.xticks(listOfSteps1, currentNamesList)
    

    to match names to values