11
$\begingroup$

The text box of a FrameTick/Tick is, by default, vertically centered on the tick mark.

Illustrating alignment of FrameTicks textbox

Is there a way to adjust the vertical alignment of FrameTick text? Specifically, along the lines of Baseline, Top, etc.

FrameTicksStyle in combination with TextAlignment/Alignment does nothing.

$\endgroup$
1
  • 1
    $\begingroup$ Related question: Styling ticks in a plot $\endgroup$ Commented Apr 27, 2012 at 14:41

3 Answers 3

8
$\begingroup$

Using Pane as in Szabolcs's answer or Framed (with FrameStyle->None) with a combination of settings for BaselinePosition, ImageMargins and FrameMargins:

 Graphics[Circle[], Frame -> True, FrameTicks -> {{ {{0, Style[Pane[Style[0.4, FontSize -> 48], BaselinePosition -> (Top -> Bottom), FrameMargins -> {{0, 0}, {0, 40}}, ImageMargins -> {{0, 0}, {0, -40}}], Background -> Green]}}, {{0, Style[Pane[Style[0.4, FontSize -> 48], BaselinePosition -> (Bottom -> Top), FrameMargins -> {{0, 0}, {40, 0}}, ImageMargins -> {{0, 0}, {-40, 0}}], Background -> Green]}}}, {None, None}}] 

you get

enter image description here

$\endgroup$
10
$\begingroup$

It is also possible to use AdjustmentBox, wrapped in DisplayForm, to get very fine-grained control over placement of FrameTicks text.

The following is based on code I use for my own real-world application.

myTickList[min_, max_, seg_, shift_?NumericQ, len_: 0] := Table[{i, DisplayForm[ AdjustmentBox[Style[i, LineSpacing -> {0, 12}], BoxBaselineShift -> shift]], {len, 0}}, {i, If[Head[seg] === List, Union[{min, max}, seg], Range[min, max, seg]]}] 

An example of its use. You can see that I've shifted the left-hand frame labels up (with a negative number) and the right-hand ones down (with a positive number).

ListLinePlot[Accumulate@RandomVariate[NormalDistribution[0, 0.5], 40], Frame -> True, GridLines -> {None, Automatic}, FrameTicks -> {{myTickList[-4, 4, 1, -2], myTickList[-4, 4, 1, 2]}, {Automatic, None}}] 

enter image description here

$\endgroup$
7
$\begingroup$

If you mention your use case, we might be able to give better answers.

I believe that tick marks are places using Inset or an equivalent mechanism (Text and Inset seem to be pretty similar: Text also accepts any formatted expression including Graphics). Since I don't seem to be able to get direct access to the Inset options, I didn't manage to control the baseline. The tick symbol is always perfectly centred.

You can use a hack like this instead:

Graphics[Circle[], Frame -> True, FrameTicks -> {None, {{0, Style[Pane[Style[0, Large, Background -> Green], ImageMargins -> {{0, 0}, {10, 0}}, ContentPadding -> False], Background -> Red]}}}] 

Mathematica graphics

The idea is to add margins to the text, in effect shifting it. The margins can be added using Pane. In my sample image, the text background is green while the Pane background (the margin) is red.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.