ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 21 Feb 2011 05:06:36 -0600Plotting arrows at the edges of a curvehttp://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/How could I plot a plane curve (either the graph of a function or an implicit plot) so that where the curve leaves the bounds of the plot, an arrow in the tangent direction is added?
I'm trying to produce plots for quiz and exam questions that are similar to ones you see in many calculus books where arrows are added to the ends of the curve to indicate that the curve continues in a certain direction "off the screen".
I've searched sage-support, the manual, and the documentation without much success. Perhaps I'm searching for the wrong term. Searching for "arrow" and "plot" or "curve" hasn't gotten me anywhere. I've also looked through examples in the matplotlib gallery, but I don't see any examples of what I want there.
I can imagine writing code myself to add such arrows to a plot, but I'm sure someone has thought about and implemented this before.Sat, 19 Feb 2011 14:11:26 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/Answer by Simon for <p>How could I plot a plane curve (either the graph of a function or an implicit plot) so that where the curve leaves the bounds of the plot, an arrow in the tangent direction is added? </p>
<p>I'm trying to produce plots for quiz and exam questions that are similar to ones you see in many calculus books where arrows are added to the ends of the curve to indicate that the curve continues in a certain direction "off the screen". </p>
<p>I've searched sage-support, the manual, and the documentation without much success. Perhaps I'm searching for the wrong term. Searching for "arrow" and "plot" or "curve" hasn't gotten me anywhere. I've also looked through examples in the matplotlib gallery, but I don't see any examples of what I want there.</p>
<p>I can imagine writing code myself to add such arrows to a plot, but I'm sure someone has thought about and implemented this before.</p>
http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12131#post-id-12131Here's a quick hack. With the code given by example (I'm not the best sage/python programmer)
f = x^3; xmin = -1; xmax = 1; small=.001;
fd = f.derivative(x)
P = plot(f, (x, xmin, xmax));
P + arrow((xmin, f(x=xmin)),(xmin-small,f(x=xmin-small)))\
+ arrow((xmax, f(x=xmax)),(xmax+small,f(x=xmax+small)))
![x^3](http://i.imgur.com/Mf5HA.png)
Replace `x^3` with `cos(x^2)` to get
![cos(x^2) arrow plot](http://i.imgur.com/3a1HU.png)
Edit
----
Here's a similar method for an implicit plot
var("x y")
f = x^2+y^3-2; xmin = -2; xmax = 2; small=.01;
fd = f.derivative(x)/f.derivative(y);
ymin = find_root(f(x=xmin),-2,2); ymax = find_root(f(x=xmax),-2,2);
P = implicit_plot(f, (x,xmin,xmax), (y,-2,2))
(P + arrow((xmin, ymin),(xmin-small,ymin+small*fd(x=xmin,y=ymin))) \
+ arrow((xmax, ymax),(xmax+small,ymax-small*fd(x=xmax,y=ymax))) \
).show(aspect_ratio=1)
![implicit arrowed plot](http://i.imgur.com/eL9g0.jpg)Sun, 20 Feb 2011 18:42:34 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12131#post-id-12131Comment by niles for <p>Here's a quick hack. With the code given by example (I'm not the best sage/python programmer)</p>
<pre><code>f = x^3; xmin = -1; xmax = 1; small=.001;
fd = f.derivative(x)
P = plot(f, (x, xmin, xmax));
P + arrow((xmin, f(x=xmin)),(xmin-small,f(x=xmin-small)))\
+ arrow((xmax, f(x=xmax)),(xmax+small,f(x=xmax+small)))
</code></pre>
<p><img alt="x^3" src="http://i.imgur.com/Mf5HA.png"/></p>
<p>Replace <code>x^3</code> with <code>cos(x^2)</code> to get
<img alt="cos(x^2) arrow plot" src="http://i.imgur.com/3a1HU.png"/></p>
<h2>Edit</h2>
<p>Here's a similar method for an implicit plot</p>
<pre><code>var("x y")
f = x^2+y^3-2; xmin = -2; xmax = 2; small=.01;
fd = f.derivative(x)/f.derivative(y);
ymin = find_root(f(x=xmin),-2,2); ymax = find_root(f(x=xmax),-2,2);
P = implicit_plot(f, (x,xmin,xmax), (y,-2,2))
(P + arrow((xmin, ymin),(xmin-small,ymin+small*fd(x=xmin,y=ymin))) \
+ arrow((xmax, ymax),(xmax+small,ymax-small*fd(x=xmax,y=ymax))) \
).show(aspect_ratio=1)
</code></pre>
<p><img alt="implicit arrowed plot" src="http://i.imgur.com/eL9g0.jpg"/></p>
http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?comment=22072#post-id-22072Nice! I think either you or @benjaminfjones should open a ticket to add this to Sage :)Mon, 21 Feb 2011 00:25:32 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?comment=22072#post-id-22072Comment by benjaminfjones for <p>Here's a quick hack. With the code given by example (I'm not the best sage/python programmer)</p>
<pre><code>f = x^3; xmin = -1; xmax = 1; small=.001;
fd = f.derivative(x)
P = plot(f, (x, xmin, xmax));
P + arrow((xmin, f(x=xmin)),(xmin-small,f(x=xmin-small)))\
+ arrow((xmax, f(x=xmax)),(xmax+small,f(x=xmax+small)))
</code></pre>
<p><img alt="x^3" src="http://i.imgur.com/Mf5HA.png"/></p>
<p>Replace <code>x^3</code> with <code>cos(x^2)</code> to get
<img alt="cos(x^2) arrow plot" src="http://i.imgur.com/3a1HU.png"/></p>
<h2>Edit</h2>
<p>Here's a similar method for an implicit plot</p>
<pre><code>var("x y")
f = x^2+y^3-2; xmin = -2; xmax = 2; small=.01;
fd = f.derivative(x)/f.derivative(y);
ymin = find_root(f(x=xmin),-2,2); ymax = find_root(f(x=xmax),-2,2);
P = implicit_plot(f, (x,xmin,xmax), (y,-2,2))
(P + arrow((xmin, ymin),(xmin-small,ymin+small*fd(x=xmin,y=ymin))) \
+ arrow((xmax, ymax),(xmax+small,ymax-small*fd(x=xmax,y=ymax))) \
).show(aspect_ratio=1)
</code></pre>
<p><img alt="implicit arrowed plot" src="http://i.imgur.com/eL9g0.jpg"/></p>
http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?comment=22071#post-id-22071I was thinking of something similar, just thought someone might have already implemented it. These plots look great. Thanks! Mon, 21 Feb 2011 05:06:36 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?comment=22071#post-id-22071Answer by John Palmieri for <p>How could I plot a plane curve (either the graph of a function or an implicit plot) so that where the curve leaves the bounds of the plot, an arrow in the tangent direction is added? </p>
<p>I'm trying to produce plots for quiz and exam questions that are similar to ones you see in many calculus books where arrows are added to the ends of the curve to indicate that the curve continues in a certain direction "off the screen". </p>
<p>I've searched sage-support, the manual, and the documentation without much success. Perhaps I'm searching for the wrong term. Searching for "arrow" and "plot" or "curve" hasn't gotten me anywhere. I've also looked through examples in the matplotlib gallery, but I don't see any examples of what I want there.</p>
<p>I can imagine writing code myself to add such arrows to a plot, but I'm sure someone has thought about and implemented this before.</p>
http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12130#post-id-12130As far as I know, the plotting in Sage is done by matplotlib. I browsed through the [matplotlib docs](http://matplotlib.sourceforge.net/contents.html) and couldn't find anything about arrows at the ends of curves. The docs also contain a gallery of examples, and I didn't see any suitable examples. In any case, I think matplotlib is the place to look; if matplotlib can do it, then Sage probably can, but if matplotlib can't then Sage can't.
Sun, 20 Feb 2011 09:54:22 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12130#post-id-12130Answer by niles for <p>How could I plot a plane curve (either the graph of a function or an implicit plot) so that where the curve leaves the bounds of the plot, an arrow in the tangent direction is added? </p>
<p>I'm trying to produce plots for quiz and exam questions that are similar to ones you see in many calculus books where arrows are added to the ends of the curve to indicate that the curve continues in a certain direction "off the screen". </p>
<p>I've searched sage-support, the manual, and the documentation without much success. Perhaps I'm searching for the wrong term. Searching for "arrow" and "plot" or "curve" hasn't gotten me anywhere. I've also looked through examples in the matplotlib gallery, but I don't see any examples of what I want there.</p>
<p>I can imagine writing code myself to add such arrows to a plot, but I'm sure someone has thought about and implemented this before.</p>
http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12129#post-id-12129I' have thought about this, but I have to admit it was only when I was reading the [TikZ/PGF Manual](http://tug.org/svn/texlive/trunk/Master/texmf-dist/doc/generic/pgf/pgfmanual.pdf), which remarks that perhaps the reason arrow tips are missing from so many scientific diagrams is simply that people can't figure out how to draw them :)
So I don't know how to do it in Sage, but I'll say that I use TikZ for my quiz/exam diagrams, and maybe this would make sense for you too if you're already texing them. If you've never used it before, here are a couple of examples that I thought were particularly relevant:
* [plotting functions](http://www.texample.net/tikz/examples/parameterized-plots/)
* [arrow tips](http://www.texample.net/tikz/examples/animated-definite-integral/) [<->]
These are probably more complicated than what you want to do -- the manual is really a very good place to start. Sorry this doesn't tell you how to get arrow tips in Sage, but maybe this non-answer will provoke someone who does know how to do it :)Sun, 20 Feb 2011 06:32:34 -0600http://ask.sagemath.org/question/7958/plotting-arrows-at-the-edges-of-a-curve/?answer=12129#post-id-12129