-1

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?

Raz
  • 49
  • 11
  • Nowhere in your code are you attempting to set the tick locations, so why should they be? – Julien Aug 18 '21 at 08:40
  • how about here ax.set_xticklabels(currentNamesList)? – Raz Aug 18 '21 at 08:44
  • if not what is the solution? – Raz Aug 18 '21 at 08:44
  • 1
    You need `ax.set_xticks(listOfSteps1)` to set the locations. And then `ax.set_xticklabels(currentNamesList)`. This also works when there are multiple subplots. `plt.xticks(listOfSteps1, currentNamesList)` does the same, but only for the "current ax". – JohanC Aug 18 '21 at 12:25

1 Answers1

1

You need to replace:

ax.set_xticklabels(currentNamesList)

by

plt.xticks(listOfSteps1, currentNamesList)

to match names to values

djangoliv
  • 1,698
  • 14
  • 26