title: Plotting arrows at the edges of a curve
body: <p>Inspired by <a href="http://ask.sagemath.org/question/396/plotting-arrows-at-the-edges-of-a-curve" rel="noreferrer">this question</a> at ask.sagemath, what is the <strong>best</strong> way of adding arrows to the end of curves produced by <code>Plot</code>, <code>ContourPlot</code>, etc...? These are the types of plots seen in high school, indicating the curve continues off the end of the page.</p>
  
  <p>After some searching, I could not find a built-in way or up-to-date package to do this. (There is <a href="http://www.scientificarts.com/arrowextended.html" rel="noreferrer">ArrowExtended</a>, but it's quite old).</p>
  
  <p>The solution given in the ask.sagemath question relies on the knowledge of the function and its endpoints and (maybe) the ability to take derivatives. Its translation into Mathematica is </p>
  
  <pre><code>f[x_] := Cos[12 x^2]; xmin = -1; xmax = 1; small = .01; 
  Plot[f[x],{x,xmin,xmax}, PlotLabel -&gt; y==f[x], AxesLabel-&gt;{x,y},
    Epilog-&gt;{Blue,
      Arrow[{{xmin,f[xmin]},{xmin-small,f[xmin-small]}}],
      Arrow[{{xmax,f[xmax]},{xmax+small,f[xmax+small]}}]
    }]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/iAyrQ.png" alt="arrow cos(12 x^2)"></p>
  
  <p>An alternative method is to simply replace the <code>Line[]</code> objects generate by <code>Plot[]</code> with <code>Arrow[]</code>.  For example</p>
  
  <pre><code>Plot[{x^2, Sin[10 x], UnitStep[x]}, {x, -1, 1}, 
    PlotStyle -&gt; {Red, Green, {Thick, Blue}},
    (*AxesStyle -&gt; Arrowheads[.03],*) PlotRange -&gt; All] /. 
   Line[x__] :&gt; Sequence[Arrowheads[{-.04, .04}], Arrow[x]]
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/6M8NW.png" alt="multiple plots"></p>
  
  <p>But this has the problem that any discontinuities in the lines generate arrow heads where you don't want them (this can often be fixed by the option <code>Exclusions -&gt; None</code>). More importantly, this approach is hopeless with <code>CountourPlot</code>s. Eg try</p>
  
  <pre><code>ContourPlot[x^2 + y^3 == 1, {x, -2, 2}, {y, -2, 1}] /. 
    Line[x__] :&gt; Sequence[Arrowheads[{-.04, .04}], Arrow[x]]
  </code></pre>
  
  <p>(the problems in the above case can be fixed by the rule, e.g., <code>{a___, l1_Line, l2_Line, b___} :&gt; {a, Line[Join[l2[[1]], l1[[1]]]], b}</code> or by using appropriate single headed arrows.).</p>
  
  <p>As you can see, neither of the above (quick hacks) are particularly robust or flexible. Does anyone know an approach that is?</p>