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.Sun, 15 Apr 2018 17:03:13 -0500Plotting life history vectors in sagehttp://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/I'm teaching a math bio class where I need to plot a life history graph. The matrix equation is `new=A*old`, where `A` is a square matrix. I want to create a matrix of the population at each iteration (for say 10 iterations) and plot each column separately. I'm having trouble figuring it out and would appreciate some help.Sun, 15 Apr 2018 13:08:56 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/Answer by slelievre for <p>I'm teaching a math bio class where I need to plot a life history graph. The matrix equation is <code>new=A*old</code>, where <code>A</code> is a square matrix. I want to create a matrix of the population at each iteration (for say 10 iterations) and plot each column separately. I'm having trouble figuring it out and would appreciate some help.</p>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?answer=42026#post-id-42026One way is to define the matrix `a` and the list `p` of
vectors of populations, initially containing just the vector
of initial populations:
sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]
then extend the list by applying a number of times the matrix to the
last vector:
sage: for k in range(20):
....: p.append(a * p[-1])
then observe the evolution of populations:
sage: p
[(300.000000000000, 200.000000000000),
(304.000000000000, 215.000000000000),
(307.930000000000, 230.570000000000),
(311.782900000000, 246.725000000000),
(315.551308000000, 263.480237000000),
(319.227531790000, 280.851183350000),
(322.803570592300, 298.853544804200),
(326.271106556104, 317.503258266095),
(329.621496104565, 336.816489210761),
(332.845761134549, 356.809628770221),
(335.934580069538, 377.499290467364),
(338.878278766255, 398.902306583471),
(341.666821275745, 421.035724143963),
(344.289800459820, 443.916800506554),
(346.736428463951, 467.562998535545),
(348.995527047875, 491.991981345530),
(351.055517775377, 517.221606597332),
(352.904412064911, 543.269920328514),
(354.529801102924, 570.155150300316),
(355.918845621980, 597.895698842414),
(357.058265545995, 626.510135176345)]
or plot it as a set of points:
sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive
or one could color the first point in red:
sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives
or one could use `line2d` instead of `point2d`,
or one could use `animate` to animate the evolution.
If you want to plot the evolution of each population:
sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitivesSun, 15 Apr 2018 13:32:46 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?answer=42026#post-id-42026Comment by maeve.mccarthy for <p>One way is to define the matrix <code>a</code> and the list <code>p</code> of
vectors of populations, initially containing just the vector
of initial populations:</p>
<pre><code>sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]
</code></pre>
<p>then extend the list by applying a number of times the matrix to the
last vector:</p>
<pre><code>sage: for k in range(20):
....: p.append(a * p[-1])
</code></pre>
<p>then observe the evolution of populations:</p>
<pre><code>sage: p
[(300.000000000000, 200.000000000000),
(304.000000000000, 215.000000000000),
(307.930000000000, 230.570000000000),
(311.782900000000, 246.725000000000),
(315.551308000000, 263.480237000000),
(319.227531790000, 280.851183350000),
(322.803570592300, 298.853544804200),
(326.271106556104, 317.503258266095),
(329.621496104565, 336.816489210761),
(332.845761134549, 356.809628770221),
(335.934580069538, 377.499290467364),
(338.878278766255, 398.902306583471),
(341.666821275745, 421.035724143963),
(344.289800459820, 443.916800506554),
(346.736428463951, 467.562998535545),
(348.995527047875, 491.991981345530),
(351.055517775377, 517.221606597332),
(352.904412064911, 543.269920328514),
(354.529801102924, 570.155150300316),
(355.918845621980, 597.895698842414),
(357.058265545995, 626.510135176345)]
</code></pre>
<p>or plot it as a set of points:</p>
<pre><code>sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>or one could color the first point in red:</p>
<pre><code>sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
<p>or one could use <code>line2d</code> instead of <code>point2d</code>,
or one could use <code>animate</code> to animate the evolution.</p>
<p>If you want to plot the evolution of each population:</p>
<pre><code>sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42032#post-id-42032Thanks so much! You've really rescued me!Sun, 15 Apr 2018 17:03:13 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42032#post-id-42032Comment by slelievre for <p>One way is to define the matrix <code>a</code> and the list <code>p</code> of
vectors of populations, initially containing just the vector
of initial populations:</p>
<pre><code>sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]
</code></pre>
<p>then extend the list by applying a number of times the matrix to the
last vector:</p>
<pre><code>sage: for k in range(20):
....: p.append(a * p[-1])
</code></pre>
<p>then observe the evolution of populations:</p>
<pre><code>sage: p
[(300.000000000000, 200.000000000000),
(304.000000000000, 215.000000000000),
(307.930000000000, 230.570000000000),
(311.782900000000, 246.725000000000),
(315.551308000000, 263.480237000000),
(319.227531790000, 280.851183350000),
(322.803570592300, 298.853544804200),
(326.271106556104, 317.503258266095),
(329.621496104565, 336.816489210761),
(332.845761134549, 356.809628770221),
(335.934580069538, 377.499290467364),
(338.878278766255, 398.902306583471),
(341.666821275745, 421.035724143963),
(344.289800459820, 443.916800506554),
(346.736428463951, 467.562998535545),
(348.995527047875, 491.991981345530),
(351.055517775377, 517.221606597332),
(352.904412064911, 543.269920328514),
(354.529801102924, 570.155150300316),
(355.918845621980, 597.895698842414),
(357.058265545995, 626.510135176345)]
</code></pre>
<p>or plot it as a set of points:</p>
<pre><code>sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>or one could color the first point in red:</p>
<pre><code>sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
<p>or one could use <code>line2d</code> instead of <code>point2d</code>,
or one could use <code>animate</code> to animate the evolution.</p>
<p>If you want to plot the evolution of each population:</p>
<pre><code>sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42030#post-id-42030Edited answer to address your follow-up question.Sun, 15 Apr 2018 16:14:06 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42030#post-id-42030Comment by maeve.mccarthy for <p>One way is to define the matrix <code>a</code> and the list <code>p</code> of
vectors of populations, initially containing just the vector
of initial populations:</p>
<pre><code>sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]
</code></pre>
<p>then extend the list by applying a number of times the matrix to the
last vector:</p>
<pre><code>sage: for k in range(20):
....: p.append(a * p[-1])
</code></pre>
<p>then observe the evolution of populations:</p>
<pre><code>sage: p
[(300.000000000000, 200.000000000000),
(304.000000000000, 215.000000000000),
(307.930000000000, 230.570000000000),
(311.782900000000, 246.725000000000),
(315.551308000000, 263.480237000000),
(319.227531790000, 280.851183350000),
(322.803570592300, 298.853544804200),
(326.271106556104, 317.503258266095),
(329.621496104565, 336.816489210761),
(332.845761134549, 356.809628770221),
(335.934580069538, 377.499290467364),
(338.878278766255, 398.902306583471),
(341.666821275745, 421.035724143963),
(344.289800459820, 443.916800506554),
(346.736428463951, 467.562998535545),
(348.995527047875, 491.991981345530),
(351.055517775377, 517.221606597332),
(352.904412064911, 543.269920328514),
(354.529801102924, 570.155150300316),
(355.918845621980, 597.895698842414),
(357.058265545995, 626.510135176345)]
</code></pre>
<p>or plot it as a set of points:</p>
<pre><code>sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>or one could color the first point in red:</p>
<pre><code>sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
<p>or one could use <code>line2d</code> instead of <code>point2d</code>,
or one could use <code>animate</code> to animate the evolution.</p>
<p>If you want to plot the evolution of each population:</p>
<pre><code>sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42029#post-id-42029How do I plot each column separately? This gives the first column vs the second column. Suppose I want to plot the column entries from the first column on the y-axis and the index on the x-axis.Sun, 15 Apr 2018 14:58:30 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42029#post-id-42029Comment by maeve.mccarthy for <p>One way is to define the matrix <code>a</code> and the list <code>p</code> of
vectors of populations, initially containing just the vector
of initial populations:</p>
<pre><code>sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]
</code></pre>
<p>then extend the list by applying a number of times the matrix to the
last vector:</p>
<pre><code>sage: for k in range(20):
....: p.append(a * p[-1])
</code></pre>
<p>then observe the evolution of populations:</p>
<pre><code>sage: p
[(300.000000000000, 200.000000000000),
(304.000000000000, 215.000000000000),
(307.930000000000, 230.570000000000),
(311.782900000000, 246.725000000000),
(315.551308000000, 263.480237000000),
(319.227531790000, 280.851183350000),
(322.803570592300, 298.853544804200),
(326.271106556104, 317.503258266095),
(329.621496104565, 336.816489210761),
(332.845761134549, 356.809628770221),
(335.934580069538, 377.499290467364),
(338.878278766255, 398.902306583471),
(341.666821275745, 421.035724143963),
(344.289800459820, 443.916800506554),
(346.736428463951, 467.562998535545),
(348.995527047875, 491.991981345530),
(351.055517775377, 517.221606597332),
(352.904412064911, 543.269920328514),
(354.529801102924, 570.155150300316),
(355.918845621980, 597.895698842414),
(357.058265545995, 626.510135176345)]
</code></pre>
<p>or plot it as a set of points:</p>
<pre><code>sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>or one could color the first point in red:</p>
<pre><code>sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
<p>or one could use <code>line2d</code> instead of <code>point2d</code>,
or one could use <code>animate</code> to animate the evolution.</p>
<p>If you want to plot the evolution of each population:</p>
<pre><code>sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitives
</code></pre>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42027#post-id-42027This helps a lot. Thank you!Sun, 15 Apr 2018 14:45:13 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42027#post-id-42027Answer by tmonteil for <p>I'm teaching a math bio class where I need to plot a life history graph. The matrix equation is <code>new=A*old</code>, where <code>A</code> is a square matrix. I want to create a matrix of the population at each iteration (for say 10 iterations) and plot each column separately. I'm having trouble figuring it out and would appreciate some help.</p>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?answer=42025#post-id-42025We need much more input to help seriously, like the size of the matrix, a concrete example of `A`, an example of picture you want to obtain.
Waiting for more details, here is a sample of what you can easily get:
sage: A = matrix([[2,1],[1,1]])
sage: A
[2 1]
[1 1]
sage: C = matrix([[2],[3]])
sage: C
[2]
[3]
sage: A * C
[7]
[5]
sage: L = []
sage: L = [C]
sage: for i in range(10):
....: C = A*C
....: L.append(C)
sage: L
[
[2] [7] [19] [50] [131] [343] [898] [2351] [6155] [16114]
[3], [5], [12], [31], [ 81], [212], [555], [1453], [3804], [ 9959],
[42187]
[26073]
]Sun, 15 Apr 2018 13:29:47 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?answer=42025#post-id-42025Comment by maeve.mccarthy for <p>We need much more input to help seriously, like the size of the matrix, a concrete example of <code>A</code>, an example of picture you want to obtain.</p>
<p>Waiting for more details, here is a sample of what you can easily get:</p>
<pre><code>sage: A = matrix([[2,1],[1,1]])
sage: A
[2 1]
[1 1]
sage: C = matrix([[2],[3]])
sage: C
[2]
[3]
sage: A * C
[7]
[5]
sage: L = []
sage: L = [C]
sage: for i in range(10):
....: C = A*C
....: L.append(C)
sage: L
[
[2] [7] [19] [50] [131] [343] [898] [2351] [6155] [16114]
[3], [5], [12], [31], [ 81], [212], [555], [1453], [3804], [ 9959],
[42187]
[26073]
]
</code></pre>
http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42028#post-id-42028Thank you!Sun, 15 Apr 2018 14:45:18 -0500http://ask.sagemath.org/question/42023/plotting-life-history-vectors-in-sage/?comment=42028#post-id-42028