83
$\begingroup$

In the plot below I would like to add two vertical lines at $x = \frac{\pi}{15} \pm \frac{1}{20}$. How can I do that?

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
Plot[{f[x], f[π/15],f[π/15]/Sqrt[2]}, {x, π/15 - .01, π/15 + .01}]

Plot

$\endgroup$
2

8 Answers 8

99
$\begingroup$

An easy way to add a vertical line is by using Epilog.

Here is an example:

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
Quiet[maxy = FindMaxValue[f[x], x]*1.1]
lineStyle = {Thick, Red, Dashed};
line1 = Line[{{π/15 + 1/50, 0}, {π/15 + 1/50, maxy}}];
line2 = Line[{{π/15 - 1/50, 0}, {π/15 - 1/50, maxy}}];
Plot[{f[x], f[π/15], f[π/15]/Sqrt[2]}, {x, π/15 - 1/20, π/15 + 1/20},
    PlotStyle -> {Automatic, Directive[lineStyle], Directive[lineStyle]},
    Epilog -> {Directive[lineStyle], line1, line2}]

Vertical lines added

Caveat

While adding lines as Epilog (or Prolog) objects works most cases, the method can easily fail when automated, for example by automatically finding the minimum and maximum of the dataset. See the following examples where the red vertical line is missing at $x=5$:

data1 = Table[0, {10}];
data2 = {1., 1., 1.1*^18, 1., 6., 1.2, 1., 1., 1., 148341.};

Row@{
  ListPlot[data1, Epilog -> {Red, Line@{{5, Min@data1}, {5, Max@data1}}}],
  ListPlot[data2, Epilog -> {Red, Line@{{5, Min@data2}, {5, Max@data2}}}]
  }

enter image description here

In the left case, Min and Max of data turned out to be the same, thus the vertical line has no height. For the second case, Mathematica fails to draw the line due to automatically selected PlotRange (selecting PlotRange -> All helps). Furthermore, if the plot is part of a dynamical setup, and the vertical plot range is manipulated, the line endpoints must be updated accordingly, requiring extra attention.

Solution

Though all of these cases can be handled of course, a more convenient and easier option would be to use GridLines:

Plot[{f[x]}, {x, π/15 - 1/20, π/15 + 1/20},
    GridLines -> {{π/15 + 1/50, π/15 - 1/50}, {f[π/15], f[π/15]/Sqrt[2]}}, PlotRange -> All]

enter image description here

And for the extreme datasets:

Row@{
  ListPlot[data1, GridLines -> {{{5, Red}}, None}],
  ListPlot[data2, GridLines -> {{{5, Red}}, None}]
  }

Example image with vertical grid lines

$\endgroup$
8
  • $\begingroup$ @Istvan thanks for the edit. $\endgroup$
    – Ajasja
    Jun 4, 2013 at 12:56
  • $\begingroup$ Welcome Ajasja. A certain data2 & Epilog combo pissed me off recently triggering this edit. Sadly, I can't give a sound explanation on why Mathematica fails to draw the Line in that case. Perhaps someone else has an insight on this. $\endgroup$ Jun 4, 2013 at 16:03
  • 1
    $\begingroup$ In the Epilog version, I'd personally use Scaled[] instead of futzing around with bounds. Witness for instance ListPlot[{1., 1., 1.1*^18, 1., 6., 1.2, 1., 1., 1., 148341.}, Epilog -> {Blue, Line[{Scaled[{0, -1}, {5, 0}], Scaled[{0, 1}, {5, 0}]}]}]. $\endgroup$ Jun 5, 2013 at 11:26
  • 1
    $\begingroup$ "Who knew..." - I did. ;) $\endgroup$ Jun 5, 2013 at 12:44
  • 2
    $\begingroup$ You can also use InfiniteLine $\endgroup$
    – JEM_Mosig
    Mar 16, 2018 at 0:43
47
$\begingroup$

One way is to use GridLines:

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}

Plot[f[x], {x, π/15 - .1, π/15 + .1}, 
 GridLines -> {{Pi/15 - 1/20, Pi/15 + 1/20}, {f[Pi/15], f[Pi/15]/Sqrt[2]}}, 
 PlotRange -> All, Frame -> True, Axes -> False]

Mathematica graphics

$\endgroup$
23
$\begingroup$

I assume you mean $x = \frac{\pi}{15} \pm \frac{1}{200}$. Then you can use Prolog or Epilog with InfiniteLine, like this:

Plot[
  f[x], 
  {x, π/15 - .01, π/15 + .01},
  Epilog -> {
    (* add vertical lines *)
    InfiniteLine[{π/15 + 1/200, 0}, {0, 1}],
    InfiniteLine[{π/15 - 1/200, 0}, {0, 1}]
  }
]

output

This does not require you to know the plot range, nor any of the function values. In addition, you are still free to use GridLines for actual grid lines.

In case you don't want to cross the $x$-axis, you can use HalfLine instead of InfiniteLine, and fix the position of the axis with the AxesOrigin option:

Plot[
  f[x],
  {x, π/15 - .01, π/15 + .01},
  AxesOrigin -> {Automatic, 0},
  Epilog -> {
    (* add vertical lines *)
    HalfLine[{π/15 + 1/200, 0}, {0, 1}],
    HalfLine[{π/15 - 1/200, 0}, {0, 1}]
  }
]

output2

$\endgroup$
1
  • 1
    $\begingroup$ I use InfiniteLine all the time for this sort of thing. Works if you log plot and as you say you don't have to worry about scaling etc. To minimise typing I would write InfiniteLine[{#, 0}, {0, 1}] & /@ {\[Pi]/15 + 1/20, \[Pi]/15 - 1/20} Highly recommended approach. $\endgroup$
    – Hugh
    Oct 20, 2021 at 9:13
15
$\begingroup$

Can use Show, but Epilog is better.

f[x_] := (x^2 z)/((x^2 - y^2)^2 + 4 q^2 x^2) /. {y -> π/15, z -> 1, q -> π/600}
plot = Plot[{f[x], f[π/15], 
    f[π/15]/Sqrt[2]}, {x, π/15 - .01, π/15 + .01}, PlotRange -> {{0, 0.26}, Automatic}];

Show[plot, 
 Graphics[{Black, Line[{{Pi/15 + 1/20, 2000}, {Pi/15 + 1/20, 9000}}]}],
 Graphics[{Black, Line[{{Pi/15 - 1/20, 2000}, {Pi/15 - 1/20, 9000}}]}]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Why is Epilog better? $\endgroup$
    – Kvothe
    Mar 3, 2023 at 14:30
  • $\begingroup$ @Kvothe With Epilog the plot is self-contained as a simple Graphics object rather than a more complicated Show object which would be less manipulable for further processes. $\endgroup$ Mar 3, 2023 at 15:33
12
$\begingroup$

Another possibility is to use ParametricPlot in tandem with Show:

Show[{
  Plot[{f[x], f[Pi/15], f[Pi/15]/Sqrt[2]}, {x, 0.1, 0.3}, 
   PlotRange -> All, Frame -> True, Axes -> False],

  ParametricPlot[{{Pi/15 + 1/20, u}, {Pi/15 - 1/20, u}}, {u, 0, 9000},
    PlotStyle -> Black]
  }]

ParametricPlot

$\endgroup$
5
$\begingroup$

Another possibility is to use Ticks:

Plot[{f[x], f[π/15], f[π/15]/Sqrt[2]}, {x, π/15 - .06, π/15 + .06},
     Ticks -> {{{π/15 + 1/20, π/15 + 1/20, {0.595, 0}, Directive[Red, Dashed]},
                {π/15 - 1/20, π/15 - 1/20, {0.595, 0}, Directive[Blue, Dashed]}},
                All}, PlotRange -> {{0.12, 0.3}, All}]
$\endgroup$
1
  • $\begingroup$ Clever manipulation of the Ticks specification, +1. $\endgroup$
    – rcollyer
    Jun 4, 2013 at 12:47
3
$\begingroup$

Next possibility is to use ListPlot:

gp1 = Plot[{({f[x], f[π/15], f[π/15]/Sqrt[2]},{x, π/15 - .05, π/15 + .05}];

ymax = Max[Last /@ Level[Cases[%, _Line, Infinity], {-2}]];

gp2 = ListPlot[{{{π/15 - 1/20, 0}, {π/15 - 1/20,ymax}}, {{π/15 + 1/20, 0}, {π/15 + 1/20, ymax}}},
Joined -> True, PlotRange -> {{0.15, 0.26}, All}];
Show[{gp1, gp2}]
$\endgroup$
3
  • $\begingroup$ You might want to note how I formatted your previous answer, and apply that formatting to this answer... $\endgroup$ Jun 4, 2013 at 7:11
  • $\begingroup$ You may also find it valuable to register your account. $\endgroup$
    – rcollyer
    Jun 4, 2013 at 12:47
  • $\begingroup$ You can also use Overlay[] instead of Show[] $\endgroup$
    – lodzki
    Aug 15, 2022 at 18:34
0
$\begingroup$

You can simulate a vertical line by adding a line with a very large slope like maybe 100 or 1000. If your eyesight is so good that you can see the difference between a high slope and infinity, then increase the slope to a million or a billion. It is important to use the PlotRange parameter, otherwise the plot will distorted by the high slope of the verticle lines.

Plot[{f[x], f[\[Pi]/15], f[\[Pi]/15]/Sqrt[2], 
  1*^6 (x - (\[Pi]/15 - 1/20)), 
  1*^6 (x - (\[Pi]/15 + 1/20))}, {x,Pi/15 - .1, \ 
  [Pi]/15 + .1},PlotRange -> {0, 1000}]
$\endgroup$

Your Answer

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

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