8
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Can't you replace h with 10^f which is the maximum of the function you're plotting in the given interval ? $\endgroup$ – b.gates.you.know.what Oct 14 '13 at 7:38
17
$\begingroup$

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

Mathematica graphics


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.

$\endgroup$
  • $\begingroup$ Perfect! I was looking for a Plot option for clipping, but this is even better. $\endgroup$ – Niki Estner Oct 14 '13 at 9:58
7
$\begingroup$

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

enter image description here

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

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}]
$\endgroup$
4
$\begingroup$

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

Mathematica graphics

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

This can now be done with an InfiniteLine in Epilog or Prolog since version 10:

Manipulate[
 Plot[10^f*x^3, {x, -E/2, 1}, PlotRangePadding -> 0, 
  Epilog -> InfiniteLine[{0.5, 0}, {0, 1}]], {{f, 0}, -5, 5}]

Mathematica graphics

or in a separate Show on Graphics:

Manipulate[
 Plot[10^f*x^3, {x, -E/2, 1}, PlotRangePadding -> 0]~Show~
  Graphics@InfiniteLine[{0.5, 0}, {0, 1}], {{f, 0}, -5, 5}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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