Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to insert guide lines in Graphics - for example a horizontal line through y=0 coordinate without specifying the range of the line?

Originally I assumed Infinity might do the trick:

Graphics[{Point[{0, 1}], Line[{{-Infinity, 0}, {Infinity, 0}}]}, 
 Frame -> True]

But this results in the error message:

Coordinate {DirectedInfinity[-1], 0} should be a pair of numbers, or a Scaled or Offset form.

So Instead of Infinity, it's possible to insert such a line using Epilog with large magnitude parameters, eg -50 to 50. The reason for Epilog is so that PlotRange->All can be used without having to specify a narrower range (see following example)

However (1) Epilog causes some of the line to be displayed outside the graphics' frame, eg in the following graphic (which shows the interpolated spectrum between the signed & unsigned graph Laplacian), and (2) specifying -50, and 50 (or -500 to 500) is a hack since for some graphs it may not cover the range (for very large graphs or if zooming out to view beyond the D-A to D+A vertical range).

enter image description here

share|improve this question
    
Related: mathematica.stackexchange.com/q/3561/121 –  Mr.Wizard Dec 7 '12 at 10:34

2 Answers 2

up vote 13 down vote accepted

You are looking for the GridLines option. For example,

Plot[Sin[x]^2, {x, 0, 10}, GridLines -> {Automatic, {1/2}}, 
 Frame -> True, Axes -> False]

Mathematica graphics

If you want other shapes or a diagonal line that is independent of the coordinate range, use scaled coordinates, like this:

Plot[Sin[x]^2, {x, 0, 10}, 
 Epilog -> {Gray, Line[{Scaled[{0, 0}], Scaled[{1, 1}]}]}, 
 Frame -> True, Axes -> False]

Mathematica graphics

Scaled coordinates will make sure the line won't go outside the frame if you only use coordinates between 0 and 1.

share|improve this answer
1  
I wish I could +2 this. Great to know about "scaled" coordinates for GridLines. –  Jagra Jul 23 '12 at 21:52
    
Thank you Szabolcs, that should work fine. –  alancalvitti Jul 23 '12 at 21:53
    
Here's a problem: how would I use Scaled to draw crosshairs through the origin? Recall in the problem above PlotRange->All is critical. How to map the origin {0,0} to Scaled coordinates? –  alancalvitti Jul 23 '12 at 22:01
1  
@alancalvitti use GridLines if you want to pass through the origin as per example above. –  Mike Honeychurch Jul 23 '12 at 23:02
    
Welcome to the 20K club. :-) –  Mr.Wizard Jul 24 '12 at 13:33

In the spirit of offering alternatives, here is the following which combines an independently specified set of lines with any plot using Show.

Frame Padding

Just combining a line in absolute coordinates shows that Frame has padded the edge of the plot:

p = Plot[Sin[x]^2, {x, 0, 10}, Frame -> True, Axes -> False, 
   GridLines -> None];

Show[p, Graphics[Line[{{0, 0.5}, {10, 0.5}}]]]

Mathematica graphics

Scaled Coordinates

Using scaled coordinates allows the line to span the whole plot area.

Show[p, Graphics[Line[{Scaled@{0, 0.5}, Scaled@{10, 0.5}}]]]

Mathematica graphics

Many Lines

Multiple lines can be handled thus:

lines = {{{0, 1/3}, {1, 1/3}}, {{0, 2/3}, {1, 2/3}}, {{1/3, 0}, {1/3, 
     1}}, {{2/3, 0}, {2/3, 1}}};

makeGrid[lines_] := Graphics[Line[Scaled /@ #] & /@ lines]

Show[p, makeGrid@lines]

Mathematica graphics

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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