15
$\begingroup$

I want to place text within a plot. The text is always centered relative to the coordinates. How can I left-align or right-align the text relative to the coordinates? For example:

Show[
  Plot[hLim1[x], {x, 0.001, 0.99}, 
    AxesLabel -> {"Prob of Causality", "Prob of Causality Given Simul"}], 
  Graphics[Text[Style["X2 Function", FontSize -> 14, Red], {0.5, 0.5}]]]

centers the text at {0.5, 0.5}. How can I make the left most part of the text at {0.5, 0.5}? I have tried the TextAlignment option in Style, but it does not do it.

I have also tried the AlignmentPoint option in Graphics, but that does not change anything either.

$\endgroup$
  • $\begingroup$ Check Epilog to put some text or other graphics primitives in a plot. $\endgroup$ – Öskå Jan 15 '15 at 12:12
  • 1
    $\begingroup$ That seems very complicated. How does one control text alighnment with Epilog? I don't see it in the examples. Thank you. $\endgroup$ – Anthony Mannucci Jan 15 '15 at 12:22
  • $\begingroup$ @AnthonyMannucci This might help mathematica.stackexchange.com/questions/22839/… $\endgroup$ – Gordon Coale Jan 15 '15 at 13:28
14
$\begingroup$

The answer is in the docs yet it might be useful for others so I will just drop it here. The Details and Options section of the Text documentation says:

enter image description here

Thus you can do the following:

Plot[x, {x, 0, 1}, 
    Epilog -> {Text[Style["hello", 25], Scaled[{0.5, 0.5}], #], Red, 
      Point@{.5, .5}}, PlotLabel -> ToString@#] & /@ {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

giving you different relative placements of the Text:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Although the documentation only shows use of the values -1, 0, or 1; any numeric values can be used. For example, to make the spacing larger (e.g., to avoid any overlap with the curve) use larger numeric values. $\endgroup$ – Bob Hanlon Jan 15 '15 at 14:22
  • $\begingroup$ @BobHanlon As the doc says: "The offsets specified need not be in the range -1 to +1." :) $\endgroup$ – Öskå Jan 15 '15 at 14:35
3
$\begingroup$

The third argument to Text can be useful in situations like the one you describe. Perhaps this example will give you the info you need to proceed.

align[Right] = {1, 0};
align[Center] = {0, 0};  (* default *)
align[Left] = {-1, 0};

lText = Text["Left Aligned", {0.5, 0.6}, align[Left]];
cText = Text["Centered", {0.5, 0.5}, align[Center]];
rText = Text["Right Aligned", {0.5, 0.4}, align[Right]];

txt = Graphics[{lText, cText, rText}];
plot = Plot[Sin[π t], {t, 0, 1}];

Show[plot, txt]

plot

$\endgroup$
  • $\begingroup$ mhm, we basically have the same answer. $\endgroup$ – Öskå Jan 15 '15 at 13:33
  • 1
    $\begingroup$ @Öskå. Indeed -- because the 3rd argument of Text is designed to handle what the OP wants to do. But there are enough differences in the details that both might be useful to the OP or others with the same problem. $\endgroup$ – m_goldberg Jan 15 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.