27
$\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$
3
  • $\begingroup$ Check Epilog to put some text or other graphics primitives in a plot. $\endgroup$
    – Öskå
    Jan 15, 2015 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$ Jan 15, 2015 at 12:22
  • $\begingroup$ @AnthonyMannucci This might help mathematica.stackexchange.com/questions/22839/… $\endgroup$ Jan 15, 2015 at 13:28

2 Answers 2

12
$\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$
2
  • $\begingroup$ mhm, we basically have the same answer. $\endgroup$
    – Öskå
    Jan 15, 2015 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, 2015 at 13:40
20
$\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$
2
  • 1
    $\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, 2015 at 14:22
  • 1
    $\begingroup$ @BobHanlon As the doc says: "The offsets specified need not be in the range -1 to +1." :) $\endgroup$
    – Öskå
    Jan 15, 2015 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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