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


2 Answers 2


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.

  • 1
    $\begingroup$ I wish I could +2 this. Great to know about "scaled" coordinates for GridLines. $\endgroup$
    – Jagra
    Jul 23, 2012 at 21:52
  • $\begingroup$ Thank you Szabolcs, that should work fine. $\endgroup$ Jul 23, 2012 at 21:53
  • $\begingroup$ 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? $\endgroup$ Jul 23, 2012 at 22:01
  • 1
    $\begingroup$ @alancalvitti use GridLines if you want to pass through the origin as per example above. $\endgroup$ Jul 23, 2012 at 23:02
  • $\begingroup$ Welcome to the 20K club. :-) $\endgroup$
    – Mr.Wizard
    Jul 24, 2012 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


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.