3
$\begingroup$

I need to add arrows & text that determine which function has the highest value in a given region. I do not even know where to start. Imagine I have the following plot:

  line1 = Line[{{.292893, 0}, {.292893, 2}}];
  line2 = Line[{{.75, 0}, {.75, 2}}];
  Plot[
  {.5 x^2 + 1, x + .75, 2 x}, {x, 0, 1},
   Epilog -> {line1, line2}
   ]

enter image description here

And this is what I need: enter image description here

Thanks a lot!

$\endgroup$
2
$\begingroup$
line1 = Line[{{.292893, 0}, {.292893, 2}}];
line2 = Line[{{.75, 0}, {.75, 2}}];
arrow = {
   Arrowheads[{-.1, .1}],
   Arrow[{{0, -0.1}, {.292893, -0.1}}],
   Arrow[{{.292893, -0.1}, {.75, -0.1}}],
   Arrow[{{.75, -0.1}, {1, -0.1}}]
   };
text = {Text["region A", {.292893/2, -0.4}],
   Text["region B", {.53, -0.4}],
   Text["region C", {.89, -0.4}]
   };
Plot[{.5 x^2 + 1, x + .75, 2 x}, {x, 0, 1}, 
 PlotRange -> {Automatic, {-1, 2}} , 
 Epilog -> {line1, line2, arrow, text}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot! I thought about something that is separate and not in the plot. But of course, this is also a way to do it! $\endgroup$ – Karl A Jan 20 '20 at 12:11
  • 1
    $\begingroup$ @KarlA, take a look at kglr’s use of labeled arrows, you might save yourself a lot of finagling to get the text positioning as you would like it. Very nice answer though, wuyudi! $\endgroup$ – CA Trevillian Jan 20 '20 at 21:30
3
$\begingroup$

You can also add text labels using custom Arrowheads:

ClearAll[arrowHeads, labeledArrows]
arrowHeads[lbls_,  offset_: 2, size_: .04] := Arrowheads[{-size, 
   {Automatic, Automatic, Graphics @ Text[#, {0, 0}, {0, offset}]}, 
    size}] & /@ lbls;

labeledArrows[lbls_, lst_,  offset_: 2, size_: .04] := MapThread[
   List, {arrowHeads[lbls, offset, size], Arrow /@ Partition[lst, 2, 1]}];

Examples:

labels = {"region 1", "region 2", "region 3"};
list = Thread[{{0, .29, .75, 1}, -.25}];

Plot[{.5 x^2 + 1, x + .75, 2 x}, {x, 0, 1}, 
 PlotRangePadding -> 0, PlotRangeClipping -> False,
 ImagePadding -> {{Automatic, Automatic}, {Scaled[.05], Scaled[.03]}},
 GridLines -> {{.292893, .75}, None}, 
 Epilog -> labeledArrows[labels, list]]

enter image description here

You can add styling directives and use the third and fourth arguments to control the label position and the size of the arrow glyph, respectively:

Plot[{.5 x^2 + 1, x + .75, 2 x}, {x, 0, 1}, 
 PlotRangePadding -> 0, PlotRangeClipping -> False,
 ImagePadding -> {{Automatic, Automatic}, {Scaled[.05], Scaled[.03]}},
 GridLines -> {{.292893, .75}, None}, 
 Epilog -> {{labeledArrows[labels, list],
   Dashed, labeledArrows[{"region 4"}, {{.29, 2 .29 - .1}, {.75, 2 .75 - .1}}], 
   Dotted, Red, FontColor -> Purple, FontSize -> 12, 
    labeledArrows[{"label 1", "label 2", "label 3"},
      Thread[{{0, .29, .75, 1}, 2.1}], -1, .06]}}]

enter image description here

Update: An alternative way to use labeledArrows is to generate a graphics object and use Grid to show it under the main plot. (Note the use of special settings for ImageSize, PlotRangePadding and ImagePadding to align the two graphics objects):

options = {ImageSize -> 1 -> 400, PlotRangePadding -> 0, 
  ImagePadding -> {{Automatic, 10}, {Automatic, Automatic}}};

plot = Plot[{.5 x^2 + 1, x + .75, 2 x}, {x, 0, 1}, 
  GridLines -> {{.292893, .75}, None}, Evaluate @ options];

arrowsandlabels = Graphics[labeledArrows[labels, Thread[{{0, .29, .75, 1}, 0}]], 
   PlotRange -> {{0, 1}, {-.05, 0.01}}, Evaluate @ options];

Grid[{{plot}, {arrowsandlabels}}, Alignment -> Right, Spacings -> {None, 1}]

enter image description here

$\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.