# How to draw an infinitely long line that automatically scales with the plot?

I'm trying to plot a function inside a Manipulate and draw a vertical line at some $x$ position. The scale of the plot can vary a lot depending on the Manipulate settings. Example:

h = 1;
Manipulate[
Plot[10^f*x^3, {x, -1, 1},
Epilog -> {Line[{{0.5, -h}, {0.5, h}}]}], {{f, 0}, -5, 5}]


Now the problem is to choose a good value for h: If it's too small, it's not as high as the plot. If it's too large, the line isn't drawn at all. (Sidenote: Don't try to make the line dashed or dotted. Apparently the dashing/dotting is done before the clipping, so Mathematica just freezes up if f gets too small.)

Ideally, I'd just want to draw a line from $y=-\infty \ldots +\infty$. Is there a simple way to do that?

-
Can't you replace h with 10^f which is the maximum of the function you're plotting in the given interval ? – b.gatessucks Oct 14 '13 at 7:38

The most natural way to do this is to use GridLines:

Manipulate[Plot[10^f*x^3, {x, -1, 1}, GridLines -> {{.5}, {}}], {{f, 0}, -5, 5}]


It automatically scales with the plot range, no need to add a magical scaling factor. By using GridLinesStyle, one can customize the appearance of the line, e.g.:

Manipulate[
Plot[10^f*x^3, {x, -1, 1}, GridLines -> {{.5}, {}},
GridLinesStyle -> Directive[Thick, Red]], {{f, 0}, -5, 5}]


To see a use case, I refer to this application where I needed such functionality in an experiment where the plot range was manipulated by a user, where I could not estimate beforehand the vertical limits of the visual field (as the user might wander quite far from the baseline). GridLines was proven stable and it works with extreme distances.

-
Perfect! I was looking for a Plot option for clipping, but this is even better. – nikie Oct 14 '13 at 9:58

The best options is to use mixed (scaled/absolute) coordinates, which unfortunatelly are not supported by Mathematica: combine absolute and scaled...

You can always add a big number for $y$ value specification or find Min and Max, of the functions in given interval, for this purpose.

Or, use less general but faster method.

h = 1;
Manipulate[
Plot[10^f*x^3, {x, -1, 1}, PlotRangePadding -> 0,
Epilog -> {Line[{Scaled[{0.75, 0}], Scaled[{0.75, 1}]}]}],
{{f, 0}, -5, 5}]


It's not so much localized, it can be also used for less convenient interval with help of Rescale:

h = 1;
Manipulate[
Plot[10^f*x^3, {x, -E/2, 1}, PlotRangePadding -> 0,
Epilog -> {Line[{Scaled[{Rescale[.5, {-E/2, 1}], 0}],
Scaled[{Rescale[.5, {-E/2, 1}], 1}]}]}]
, {{f, 0}, -5, 5}]


-
GridLines is easier to use, but this is more powerful. I wish I could accept both. Thanks! – nikie Oct 14 '13 at 10:03
@nikie No problem, I also find GridLines better here :) – Kuba Oct 14 '13 at 10:06

It is possible to use the Scaled command with two arguments: Scaled[{dx,dy},{x0,y0}]. The second argument {x0,y0} specifies the starting point in absolute values, while the first one {dx,dy} specifies an offset in relative coordinates.

So one way to draw an infinitely long line at $x=0.5$ is:

Manipulate[
Plot[10^f*x^3, {x, -1, 1},
Epilog -> {Line[{Scaled[{0, -1},{0.5,0}],Scaled[{0,1},{0.5, 0}]}]}], {{f, 0},-5, 5}]

-

Edit: this is a safer version of the original hack:

Manipulate[g = Plot[10^f*x^3, {x, -1, 1}];
Show[{g,
Graphics[
Line[{0.5, #} & /@ (PlotRange /. Options[g])[[2]]]]}], {{f,0}, -5, 5}]


Interestingly, PlotRange also works as a function in 9.01:

PlotRange[g] == PlotRange /. Options[g]


True

-
@Kuba yes it does, but it is a gamble (I was just being lazy). Perhaps I should re-write or delete to avoid grievance. – Yves Klett Oct 14 '13 at 8:06
@Kuba removed offending code, hopefully this is more stable :-) – Yves Klett Oct 14 '13 at 8:18
It is working now :) +1 – Kuba Oct 14 '13 at 8:28
@Kuba sorry for the crash - next kernel is on me :-) – Yves Klett Oct 14 '13 at 8:30