4

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)

enter image description here

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.

mzzx
  • 1,964
  • 4
  • 16
  • 26
  • This question is already answered here: https://stackoverflow.com/a/47500319/2646505 – Tom de Geus Mar 23 '18 at 07:20
  • Ha yes, the second answer to this other post can be adapted. The marked answer doesn't help though, and I'm unsure how to use the second answer as an answer for this -- do you want to write an answer linking to the precise answer you posted? – mzzx Mar 23 '18 at 08:48
  • Hmm actually I can't make it work...? – mzzx Mar 23 '18 at 09:03
  • Actually I think this question contains an answer: https://stackoverflow.com/questions/28615887/how-to-move-a-ticks-label-in-matplotlib?rq=1 This way I can set the vertical alignment. – mzzx Mar 23 '18 at 09:12
  • I can reproduce your problem: I am able to change the horizontal position of the yticks, and the vertical position of the xticks, but not the other two combinations. – Tom de Geus Mar 23 '18 at 10:21
  • Ah thanks, good to know - and yes this makes sense with what I got. I think that one can tweak the answer to the question I linked (https://stackoverflow.com/a/33688795/3822090) and do something with the type, but it seems somewhat nontrivial to me (i.e. I've given up for the time being) – mzzx Mar 23 '18 at 10:57
  • The linked answer works fine, I just tried it. (You would need to find the best value for `SHIFT` yourself, as apparently you use a different font(size) than the default). It's for sure not the best solution, because it would depend on the data coordinates. – ImportanceOfBeingErnest Mar 23 '18 at 11:06
  • Ok yes I think you're right - and then change set_x to set_y also inside the lambda function that gets given to MethodType. It is a bit of pain, and I don't understand what it does, but you're right that it works. So what do I do with this question here, leave as is? Do you want to write it as an answer linking to the other answer, or can I somehow mark it as a duplicate to that question? (The one it's currently linked to is not useful.) – mzzx Mar 23 '18 at 11:17
  • Hm so I tried and I think I can't mark it as a duplicate (I can close or edit etc but not much else). I think a better alternative would be great, if it is not too much work for you, and I don't mind if you do it here or at the other question, as long as my question is linked/marked as a dupe correctly. I think perhaps having the better alternative here with a link to the other one might be slightly clearer, but you're the one with more stackoverflow experience - whatever you think will help people find the answer fast is good for me! – mzzx Mar 23 '18 at 11:41
  • (I think maybe I forfeited my right to mark this question as anything when I said no to it being a duplicate of the question with the answer that doesn't work here) – mzzx Mar 23 '18 at 11:50
  • Ok, I marked as duplicate now. I will give an answer to the other question shortly and will notify you here. This is in general desireable because then people will find all answers in one place. – ImportanceOfBeingErnest Mar 23 '18 at 11:57
  • [This is the answer](https://stackoverflow.com/a/49449590/4124317) I just provided. I guess you can easily adapt it for the case of a shift in y direction. – ImportanceOfBeingErnest Mar 23 '18 at 12:25
  • Sorry forgot to respond -- but I read your answer and it was very helpful, hadn't been aware of ScaledTranslation before at all. Thank you!! – mzzx Mar 25 '18 at 12:31

0 Answers0