I would like to adapt the position of my matplotlib yticklabels, so that the center of my yticklabel text is aligned with the ytick.
I.e. in the following plot
fig,ax=plt.subplots(1)
ax.plot(n.linspace(0,5,80),1+(n.linspace(0,5,80)/6.),'k',lw=2)
ax.set_ylim([0.8,2.25])
ax.set_xlim([-1.0,7])
ax.set_yticks([1.,1+(1/6.),1+(2/6.),1+(3/6.),1+(4./6),1+(5./6),2])
ax.set_yticklabels(['$f_0$','$f_1$','$f_2$','$f_3$','$f_4$','$f_5$',r'$f_{6}$'])
ax.set_ylabel(r'$f$', rotation='horizontal')
ax.yaxis.set_label_coords(-0.09,0.95)
I'd like the yticklabels "f_0" ... "f_6" to be aligned with the ytick such that the top of the superscript is at the level of the tick; thus I'd need to lower the position by perhaps two mm.
I'm aware that I can "pad" the labels, but I don't know how to change the vertical position. (At the moment, I add my labels in keynote, which is clearly not ideal.)
Edit: It was pointed out that the second answer to this question Aligning rotated xticklabels with their respective xticks and the corresponding blogpost https://gorelik.net/2017/11/23/how-to-make-a-graph-less-readable-rotate-the-text-labels/ includes a solution to this question. I agree, but when I tried to adapt my code accordingly, this did not change anything:
fig,ax=plt.subplots(1)
ax.plot(n.linspace(0,5,80),1+(n.linspace(0,5,80)/6.),'k',lw=2)
ax.set_ylim([0.8,2.25])
ax.set_xlim([-1.0,7])
ax.set_yticks([1.,1+(1/6.),1+(2/6.),1+(3/6.),1+(4./6),1+(5./6),2])
ax.set_ylabel(r'$f$', rotation='horizontal')
ax.yaxis.set_label_coords(-0.09,0.95)
labels=ax.set_yticklabels(['$f_0$','$f_1$','$f_2$','$f_3$','$f_4$','$f_5$',r'$f_{6}$'])
for i, label in enumerate(labels):
label.set_y(label.get_position()[1] - 100.)
I think that this question: How to move a tick's label in matplotlib? contains an answer as to why the set_y does not seem to work for me (though I'm unsure). Setting the vertical alignment, as proposed there, does change the yticklabel positions, but is not quite precise enough for what I want to do. I'm still playing around with the main answer of this last question, but I think that I don't understand well enough what I'm doing here, and so I think I will go back to doing this in keynote.