How to draw an infinitely long line that automatically scales with the plot?
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.
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}]
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