ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 03 Jun 2012 01:37:41 +0200plotting multiple functions from a for loophttps://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/Here I link to a worksheet with which I'm having the darnedest trouble...
[PlotTest](http://www.sagenb.org/home/pub/4787/)
The code in the worksheet makes it easy to plot many similar plots, and this is a pretty organized way to do it. I got it to work once, but I changed something and made too many changes afterward to get it working again to undo back to my original code.
The problem is in the third cell, obviously. I want to assign to '**a**' "*plot all of the functions in the list **funclist***." I realize that they aren't functions, really (they aren't assigned names such as f0,f1,f2, etc), but I'll try to solve that another day. But anyway, how can I set this loop up to add all of these plots and dump them into an object? I need to be able to input '**a**' and get a plot containing all of these.
As is evident from the worksheet, I can't use the operator += (though I swear I did the first time).
I would rather not to do it like this:
a = plotlist[0]
for i in range(1,len(plotlist)):
a += plotlist[i]
Because that requires that I spread out the definition of '**a**' and has an unnecessary amount of code. However, that seems to be the easiest way to do it.
But I would like to be able to do it in two lines of code --- for i in plotlist, add all these plots to this object. Or three lines, if I have to initialize '**a**' beforehand (give it a type or whatever.)Sat, 02 Jun 2012 01:25:51 +0200https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/Answer by daniel.e2718 for <p>Here I link to a worksheet with which I'm having the darnedest trouble...</p>
<p><a href="http://www.sagenb.org/home/pub/4787/">PlotTest</a></p>
<p>The code in the worksheet makes it easy to plot many similar plots, and this is a pretty organized way to do it. I got it to work once, but I changed something and made too many changes afterward to get it working again to undo back to my original code.</p>
<p>The problem is in the third cell, obviously. I want to assign to '<strong>a</strong>' "<em>plot all of the functions in the list <strong>funclist</strong></em>." I realize that they aren't functions, really (they aren't assigned names such as f0,f1,f2, etc), but I'll try to solve that another day. But anyway, how can I set this loop up to add all of these plots and dump them into an object? I need to be able to input '<strong>a</strong>' and get a plot containing all of these.</p>
<p>As is evident from the worksheet, I can't use the operator += (though I swear I did the first time).</p>
<p>I would rather not to do it like this:</p>
<pre><code>a = plotlist[0]
for i in range(1,len(plotlist)):
a += plotlist[i]
</code></pre>
<p>Because that requires that I spread out the definition of '<strong>a</strong>' and has an unnecessary amount of code. However, that seems to be the easiest way to do it.</p>
<p>But I would like to be able to do it in two lines of code --- for i in plotlist, add all these plots to this object. Or three lines, if I have to initialize '<strong>a</strong>' beforehand (give it a type or whatever.)</p>
https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?answer=13639#post-id-13639Even better! To achieve this in **one line**:
plot = sum(i for i in plotlist)Sat, 02 Jun 2012 02:06:42 +0200https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?answer=13639#post-id-13639Comment by Eviatar Bach for <p>Even better! To achieve this in <strong>one line</strong>:</p>
<pre><code>plot = sum(i for i in plotlist)
</code></pre>
https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?comment=19699#post-id-19699You should be able to do simply `plot = sum(plotlist)`. But since `plot` is a Sage function, it's better to not overwrite it.Sun, 03 Jun 2012 01:37:41 +0200https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?comment=19699#post-id-19699Answer by DSM for <p>Here I link to a worksheet with which I'm having the darnedest trouble...</p>
<p><a href="http://www.sagenb.org/home/pub/4787/">PlotTest</a></p>
<p>The code in the worksheet makes it easy to plot many similar plots, and this is a pretty organized way to do it. I got it to work once, but I changed something and made too many changes afterward to get it working again to undo back to my original code.</p>
<p>The problem is in the third cell, obviously. I want to assign to '<strong>a</strong>' "<em>plot all of the functions in the list <strong>funclist</strong></em>." I realize that they aren't functions, really (they aren't assigned names such as f0,f1,f2, etc), but I'll try to solve that another day. But anyway, how can I set this loop up to add all of these plots and dump them into an object? I need to be able to input '<strong>a</strong>' and get a plot containing all of these.</p>
<p>As is evident from the worksheet, I can't use the operator += (though I swear I did the first time).</p>
<p>I would rather not to do it like this:</p>
<pre><code>a = plotlist[0]
for i in range(1,len(plotlist)):
a += plotlist[i]
</code></pre>
<p>Because that requires that I spread out the definition of '<strong>a</strong>' and has an unnecessary amount of code. However, that seems to be the easiest way to do it.</p>
<p>But I would like to be able to do it in two lines of code --- for i in plotlist, add all these plots to this object. Or three lines, if I have to initialize '<strong>a</strong>' beforehand (give it a type or whatever.)</p>
https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?answer=13641#post-id-13641I think you're going to be interested in [list comprehensions](http://docs.python.org/tutorial/datastructures.html#list-comprehensions) and [generator expressions](http://docs.python.org/tutorial/classes.html#generator-expressions).
Using listcomps, your code compresses to:
sage: funclist = [1/x^i for i in range(10)]
sage: plotlist = [f.plot((x, 1, 10)) for f in funclist]
sage: plot(plotlist)
(Note that I changed the x lower limit to make it look better.)
![image description](/upfiles/13386134691903631.png)
[BTW, as for your ability to use "a += something", that was almost certainly because you already had an "a" floating around from before.]
If you want to one-line it:
plot([(1/x^i).plot((x, 1, 10)) for i in range(10)])
Sat, 02 Jun 2012 02:06:55 +0200https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?answer=13641#post-id-13641Comment by daniel.e2718 for <p>I think you're going to be interested in <a href="http://docs.python.org/tutorial/datastructures.html#list-comprehensions">list comprehensions</a> and <a href="http://docs.python.org/tutorial/classes.html#generator-expressions">generator expressions</a>.</p>
<p>Using listcomps, your code compresses to:</p>
<pre><code>sage: funclist = [1/x^i for i in range(10)]
sage: plotlist = [f.plot((x, 1, 10)) for f in funclist]
sage: plot(plotlist)
</code></pre>
<p>(Note that I changed the x lower limit to make it look better.) </p>
<p><img alt="image description" src="/upfiles/13386134691903631.png"/></p>
<p>[BTW, as for your ability to use "a += something", that was almost certainly because you already had an "a" floating around from before.]</p>
<p>If you want to one-line it:</p>
<pre><code>plot([(1/x^i).plot((x, 1, 10)) for i in range(10)])
</code></pre>
https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?comment=19703#post-id-19703I'm very very new to Sage and programming in general; I only started looking at in like a week or so ago haha. Thanks for the answer!
I probably would have done the same thing eventually after realizing how the immediate issue condensed to one line.
And yeah, I thought the same thing about my having used successfully "a += i", though I'm not sure how I didn't get a TypeError, doubled a graph, or whatever. Oh well, unimportant.Sat, 02 Jun 2012 02:55:37 +0200https://ask.sagemath.org/question/9024/plotting-multiple-functions-from-a-for-loop/?comment=19703#post-id-19703