# How to insert guide-lines in graphics without specifying range?

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). • – Mr.Wizard Dec 7 '12 at 10:34

You are looking for the GridLines option. For example,

Plot[Sin[x]^2, {x, 0, 10}, GridLines -> {Automatic, {1/2}},
Frame -> True, Axes -> False] 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] Scaled coordinates will make sure the line won't go outside the frame if you only use coordinates between 0 and 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
• @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.

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}}]]] 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}}]]] 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] 