5
$\begingroup$

In this question it is answered how to add a vertical line to a graph. However, it has not been shown that how can someone add the position of that vertical line to the axis. How can I add the coordinate of a vertical line to the graph itself? I want to be able to change the font size of the coordinate too. By using the code below I can change the font size of the coordinate of axes. I want similar thing for the solution to this question too.

FrameStyle -> Directive[Black, Bold, 80]

Here is an example:

line3 = Line[{{2.33, -2}, {2.33, 2}}];
Plot[Sin[x], {x, 0, 2*Pi}, PlotStyle -> {Thick, Red}, Frame -> True, FrameStyle ->Directive[Thick, Bold, 20], Epilog -> {Directive[Thick, Blue], line3}]

Result: enter image description here

$\endgroup$
8
  • $\begingroup$ Can you provide the code for the Plot as well? And where you want your vertical line. $\endgroup$
    – Öskå
    Jun 17, 2014 at 10:48
  • $\begingroup$ @Öskå. I added more details. $\endgroup$
    – MOON
    Jun 17, 2014 at 10:54
  • $\begingroup$ FrameTicks -> {{0, 1, 2.33, 3, 4, 5, 6}, Automatic}? $\endgroup$
    – C. E.
    Jun 17, 2014 at 11:00
  • $\begingroup$ Where should be the position of the vertical line located? I have this but it's ugly since everything is so huge. $\endgroup$
    – Öskå
    Jun 17, 2014 at 11:01
  • $\begingroup$ @Öskå. The position is not that much important. In this example I placed the vertical line at 2.33. $\endgroup$
    – MOON
    Jun 17, 2014 at 11:03

2 Answers 2

6
$\begingroup$

The easiest way to accomplish this is to change the ticks (with FrameTicks) on the axes manually.

FrameTicks -> {{Automatic, Automatic}, {{0, 1, 2.33, 3, 4, 5, 6}, None}}

If you have a large number of ticks on your axes writing them down like this could be cumbersome, in which case you could try to generate the range and then replace another tick with the new tick or insert the extra tick that you want, like this:

{{Automatic, Automatic}, {Range[6] /. 2 -> 2.33, None}}

or

{{Automatic, Automatic}, {Insert[Range[6], 2.33, 3], None}}

Result:

Plot


One can then easily make it automatic (thank you Öskå for this code and animation):

line3[y_] := Line[{{y, -2}, {y, 2}}];
Manipulate[
  Plot[Sin[x], {x, 0, 2 Pi}, PlotStyle -> {Thick, Red}, Frame -> True, 
    FrameStyle -> Directive[Thick, Bold, 20], 
    Epilog -> {Directive[Thick, Blue], line3[t]}, 
    FrameTicks -> {{Automatic, Automatic}, {Range[0, 6] /. Round@t -> t, None}}], {t, 0, 6}]

enter image description here

$\endgroup$
3
  • $\begingroup$ I added a more general case based on your idea, I hope you don't mind. If you do feel free to delete it :) $\endgroup$
    – Öskå
    Jun 17, 2014 at 12:17
  • $\begingroup$ @Öskå I think it's great, thank you! :) $\endgroup$
    – C. E.
    Jun 17, 2014 at 18:38
  • 1
    $\begingroup$ Stop thanking me in public, I'm going to blush ;o) It's just that I was about the post this answer right after you posted yours, so I thought that I would put mine in yours.. :) $\endgroup$
    – Öskå
    Jun 17, 2014 at 18:41
4
$\begingroup$

An answer with GridLines:

Plot[Sin[x], {x, 0, 2*Pi}, PlotStyle -> {Thick, Red}, Frame -> True, 
 FrameStyle -> Directive[Thick, Bold, 20], 
 GridLines -> {{2.33}, None}, 
 GridLinesStyle -> Directive[Blue, Thick], 
 Epilog -> Text[Framed[2.33], {2.33, -0.9}, Background -> White]]

enter image description here

Manipulate[
 Plot[Sin[x], {x, 0, 2*Pi}, PlotStyle -> {Thick, Red}, Frame -> True, 
  FrameStyle -> Directive[Thick, Bold, 20], GridLines -> {{m}, None}, 
  GridLinesStyle -> Directive[Blue, Thick], 
  Epilog -> 
   Text[Framed[NumberForm[m, 3]], {m, -0.9}, 
    Background -> White]], {m, 0, 2 Pi}]

enter image description here

$\endgroup$

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.