# How to align rotated tick labels?

How can I align tick labels so the tick label text ends centered under the data point? Of Course it works fine with a rotation of 90 Degrees, but not with 45 or similar angles.

I see an example of how to do this charts but not with Plot or ListPlot https://mathematica.stackexchange.com/a/2656/388

d = Table[n, {n, 10}];
xticks = Table[{n,
Rotate["The y value is " <> ToString[n], 45 Degree]}, {n, d}];
ListPlot[d, Frame -> True,
FrameTicks -> {{Automatic, None}, {xticks, None}}]


• Aug 16, 2012 at 22:53
• It actually is centered... Try for example with xticks = Table[{n, Rotate["The y value is " <> ToString[n], 45 Degree], {0, 0.2}}, {n, d}]; and you'll see that it intersects the text at its midpoint
– rm -rf
Aug 16, 2012 at 23:31
• @R.M "text ends centered" Aug 17, 2012 at 11:46
• Related: (2652) Nov 11, 2014 at 3:03

Similar to Szabolcs's and my answers here, I would suggest writing your own custom tick function, so that the labels are set into a Pane which can be both Rotated and have ImageMargins.

myTickList[min_, max_, seg_, shift_?NumericQ, phi_?NumericQ, len_: 0.01] :=
Table[{i, Rotate[Pane[Style["y =" <> ToString[i], LineSpacing -> {0, 12}],
FrameMargins -> {{shift, 0}, {0, 0}}], phi], {len, 0}},
{i, If[Head[seg] === List, Union[{min, max}, seg],
Range[min, max, seg]]}]


Examples:

fakedata = Accumulate@RandomVariate[NormalDistribution[0, 0.5], 40];

ListLinePlot[fakedata, Frame -> True, GridLines -> {None, Automatic},
FrameTicks -> {{Automatic,  Automatic}, {myTickList[0, 40, 4, 25, Pi/4], None}}]


ListLinePlot[fakedata, Frame -> True, GridLines -> {None, Automatic},
FrameTicks -> {{Automatic, Automatic}, {myTickList[0, 40, 4, 5, Pi/3], None}}]


• I'm seriously frustrated of how complicated this is ... May 18, 2015 at 13:52

Brett Champion posted a nice clean method in Labeling a bar chart, changing how rotated labels are centered that can be applied here as well:

center = Row[{#, Invisible[#]}, "\[NegativeThickSpace]"] &;

d = Range[10];

xticks = Table[{n, center @ Rotate["The y value is " <> ToString[n], 45 Degree]}, {n, d}];

ListPlot[d, Frame -> True, FrameTicks -> {{Automatic, None}, {xticks, None}}]


• I like this solution, but the placement still looked a bit off. This is easy to adjust by adding additional characters of [NegativeThickSpace] to the Row command. However, since Mathematica automatically subs out the name for a blank character that's difficult to edit, I found it easier to instead use FromCharacterCode[Table[62340, {x}]] and play with the value of x until it looks good. x=4 was the magic number for me. May 1, 2019 at 23:51