1
$\begingroup$

If I want to draw a line on a Plot I can use Prolog or Epilog. However, this goes wrong if the scale of the plot is small. Here is a minimum working example.

Plot[Sin[x], {x, 0, 3 π}, Frame -> True, 
 Prolog -> {Red, Line[{{4, -1}, {4, 1}}]}]
Plot[10^-13 Sin[x], {x, 0, 3 π}, Frame -> True, 
 Prolog -> {Red, Line[{{4, -1}, {4, 1}}]}]

Mathematica graphics Mathematica graphics

No vertical line is drawn in the second case. This is the problem. I guess that it is due to the small scale and the line is not drawn to this resolution. If I change the scale of the vertical line it works

Plot[10^-13 Sin[x], {x, 0, 3 π}, Frame -> True, 
 Prolog -> {Red, Line[{{4, -10^-13}, {4, 10^-13}}]}]

Mathematica graphics

However this requires me to find the scale of the plot first and put this into the line. I am working with the strains from the Finite Element Method within a Dynamic and I can't predict the scale in advance. I thought of this

Show[Plot[10^-13 Sin[x], {x, 0, 3 π}, Frame -> True],
 Graphics[{Red, Line[{{4, -1}, {4, 1}}]}]
 ]

However this does not work either. Is it possible to use Scaled or Offset? I failed when I tried. Thanks for suggestions.

$\endgroup$

1 Answer 1

1
$\begingroup$

(Too long for a comment.)

Yes, you can use Scaled[], as I did in this answer. To wit,

{Plot[Sin[x], {x, 0, 3 π}, Frame -> True, 
      Prolog -> {Red, Line[{Scaled[{0, 1}, {4, 0}], Scaled[{0, -1}, {4, 0}]}]}], 
 Plot[1*^-13 Sin[x], {x, 0, 3 π}, Frame -> True, 
      Prolog -> {Red, Line[{Scaled[{0, 1}, {4, 0}], Scaled[{0, -1}, {4, 0}]}]}]}
// GraphicsRow

vertical line

Or, you can use InfiniteLine[]:

Plot[1*^-13 Sin[x], {x, 0, 3 π}, Frame -> True, 
     Prolog -> {Red, InfiniteLine[{4, 0}, {0, 1}]}]

which should yield the same picture as the one on the right.

$\endgroup$
1
  • $\begingroup$ Very helpful thanks. I have never quite understood Scaled this is a good example to learn. InfiniteLine is more intuitive. All good and helpful, thanks. $\endgroup$
    – Hugh
    Commented Mar 28, 2018 at 9:44

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.