tags:

views:

375

answers:

3

In Mathematica, how do you plot a horizontal line at a given number? How do you plot a vertical line at a given number?

+3  A: 

One approach would be to add Line graphic primitives to your graphics:

p1 = Plot[Sin[x], {x, -2*Pi,2*Pi}];
l1 = Graphics@Line[{{-2Pi,.75},{2Pi,.75}}]; (* horizontal line at y==.75 *)
Show[p1,l1]

Another approach would be to fiddle around with GridLines.

High Performance Mark
+3  A: 

If you're actually using Plot (or ListPlot, et c.), the easiest solution is to use the GridLines option, which lets you specify the x- and y-values where you want the lines drawn. For instance:

Plot[Sin[x], {x, 0, 2 \[Pi]},
  GridLines -> {{0, \[Pi]/2, \[Pi], 3 \[Pi]/2, 2 \[Pi]},
                {-1, -Sqrt[3]/2, -1/2, 0, 1/2, Sqrt[3]/2, 1}}]
Pillsy
+2  A: 

For the case of horizontal lines when using Plot the easiest trick is to just include additional constant functions:

Plot[{Sin[x], .75}, {x, 0, 2Pi}]

For vertical lines, there's the Epilog option for Plot and ListPlot:

Plot[Sin[x], {x, 0, 2Pi}, Epilog->Line[{{4,-100}, {4,100}}]]

But probably the best is the GridLines option given in Pillsy's answer.

dreeves
+1: I'd never thought of or come across the first suggestion you make @dreeves.
High Performance Mark