# How to insert graphics primitives into a plot?

I have a standard normal curve rendered with the following command:

Plot[{1/Sqrt[2*Pi] Exp[-1/2*x^2]}, {x, -3, 3}, Filling -> Axis]

It looks exactly as you think it would:

I want to be able to put an "insert" in. Like a line that would be displayed on top of the curve for specific points. Namely for the sample mean value in relation to the curve. With my (limited) editing skills in mspaint, here's an example:

Does anybody know how this could be accomplished?

-
Did you have a look at the GridLines option of Plot[]? – J. M. Apr 17 '12 at 17:03
Alternatively: look into Epilog and the graphics primitives Line[] and Text[]... – J. M. Apr 17 '12 at 17:04
And did you know that instead of MS Paint, you can also use the drawing tools provided by Mathematica: click on the graphic and select the menu item Graphics > Drawing Tools. – Jens Apr 17 '12 at 17:16
Eiplog is working by the way! thanks! the problem with the example used in the mathematica documentation concerning epilog, found here reference.wolfram.com/mathematica/ref/Line.html is that the function Line[] is for a continuous selection of data points. I want two discrete unjoined lines. Any idea how that could be accomplished? thanks! – user582 Apr 17 '12 at 17:36
@franklin You can provide a list of graphics primitives to Epilog, e.g. Epilog -> {Line[{pt1, pt2}], Line[{pt3, pt4}]} would draw two unconnected lines. Even with one Line statement you can draw unconnected lines by doing something like Line[{{pt1, pt2}, {pt3, pt4}}]. – Heike Apr 17 '12 at 17:43

You can display graphics primitives in your plot by using the Show command

Show[
Plot[{1/Sqrt[2*Pi] Exp[-1/2*x^2]}, {x, -3, 3}, Filling -> Axis],
Graphics[Line[{{1.6, 0}, {1.6, 0.4}}]],
Graphics[{Thick, Red, Line[{{-0.4, 0}, {-0.4, 0.4}}]}],
Graphics[Text["Φ", {-.6, 0.38}]],
Graphics[Text["μ", {2, 0.1}]]
]


You can actually just combine all the Graphics objects into one list but I always keep them separate for legibility and editability since otherwise you have to remember to switch back and forth between e.g. color and line weight specifications.

Show[
Plot[{1/Sqrt[2*Pi] Exp[-1/2*x^2]}, {x, -3, 3}, Filling -> Axis],
Graphics[
{Line[{{1.6, 0}, {1.6, 0.4}}],
Thick, Red,
Line[{{-0.4, 0}, {-0.4, 0.4}}],
Thin, Black,
Text["Φ", {-.6, 0.38}],
Text["μ", {2, 0.1}]}
]
]


You can also achieve the same result by using the Epilog or Prolog option for Plot

Plot[
{1/Sqrt[2*Pi] Exp[-1/2*x^2]},
{x, -3, 3},
Filling -> Axis,
Prolog ->
{Line[{{1.6, 0}, {1.6, 0.4}}],
Thick, Red,
Line[{{-0.4, 0}, {-0.4, 0.4}}],
Thin, Black,
Text["Φ", {-.6, 0.38}],
Text["μ", {2, 0.1}]}
]


The difference between Prolog and Epilog is in placing your graphics primitives behind or in front of the Plot, this may be important in certain circumstances.

Once you have composed your plot you can use the standard Export command to save it to a format of your choosing.

-
Just for reference on "behind or on top of", see this issue: Prolog is put on top of GridLines. – István Zachar Apr 17 '12 at 18:35
@IstvánZachar Perhaps you can try using the undocumented option Method -> {"GridLinesInFront" -> True} – R. M. Apr 17 '12 at 18:52
You can also use sublists to restrict graphics directives to only part of the graphics objects: Graphics[{Line[{{1.6, 0}, {1.6, 0.4}}],{Thick, Red, Line[{{-0.4, 0}, {-0.4, 0.4}}]},Text["\[CapitalPhi]", {-.6, 0.38}],Text["\[Mu]", {2, 0.1}]] – celtschk Apr 17 '12 at 18:55

I thought I would show how to do this with Presentations (which I sell). It is not necessary to use Show and Graphics level jumping (the repeated Graphics wrappers on primitives) or the Epilog option. Say what you wish, these are confusing to many users. Here everything is a primitive and one just draws one thing after another.

<< Presentations

Draw2D[
{Draw[{1/Sqrt[2*Pi] Exp[-1/2*x^2]}, {x, -3, 3}, Filling -> Axis,
FillingStyle -> Lighter@Orange],
Thick,
Line[{{1.6, 0}, {1.6, 0.4}}],
{Red, Line[{{-0.4, 0}, {-0.4, 0.4}}]},
Text[Style["\[CapitalPhi]", 14], {-0.6, 0.38}],
Text[Style["\[Mu]", 14], {1.9, 0.3}]},
AspectRatio -> 1/2,
Frame -> True,
ImageSize -> 300]
`

-