ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Sep 2020 15:17:31 -0500Interact 3d plot(Polyhedron)https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/Hi there, I got confused about the interact:
def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp)
show(G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black'))
@interact
def _(x=(0,10)):
par(x)
It is quite weird since in the examples of http://wiki.sagemath.org/interact/graphics, it seems that the interactive parameters could only be something controlling a plot. However in my case, the interactive parameter(x) is not used for controlling plot but for obtaining an [[],[]]. so it fails to interact. Any idea of that?
Wed, 28 Mar 2012 19:23:22 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/Answer by niles for <p>Hi there, I got confused about the interact:</p>
<pre><code>def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp)
show(G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black'))
@interact
def _(x=(0,10)):
par(x)
</code></pre>
<p>It is quite weird since in the examples of <a href="http://wiki.sagemath.org/interact/graphics">http://wiki.sagemath.org/interact/gra...</a>, it seems that the interactive parameters could only be something controlling a plot. However in my case, the interactive parameter(x) is not used for controlling plot but for obtaining an [[],[]]. so it fails to interact. Any idea of that?</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?answer=13407#post-id-13407If you want to animate a 3d plot, I think there must be a better way. Here's one hacky workaround:
First, generate a "dummy animation" with the number of frames you want, and note where the output images are stored:
N = 10
A = animate([i for i in range(N)])
print A.png()
os.system('ls '+ A.png())
This will return something like
/sagenb/sagenbws/.sage/temp/mod.math.washington.edu/13021/dir_3
00000000.png 00000002.png 00000004.png 00000006.png 00000008.png
00000001.png 00000003.png 00000005.png 00000007.png 00000009.png
Now save the list of images you want to animate, using the same names as those listed above:
def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp, field=RDF)
return G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black')+plot3d(0,(0,200),(0,300),color='red',opacity=.1)
for i in range(N):
P = par(2+i)
P.save(A.png()+'/%08d.png'%i)
When you call `A.show()`, it just checks for images in the directory returned by `A.png` and uses `convert` or `ffmpeg` to return a gif animation. Since you've cleverly replaced the original images generated by the `animate` command with the ones you want, then you will get your animation with
A.show()
<img src="/upfiles/13330552838285333.gif" width="300"></img>
----
Note: it's been bugging me for a long time that there's no good way to animate 3D plots. Maybe this hack will convince someone else that they should figure out a better way to do it! Alternatively, maybe this will tell me that there's already a ticket open for fixing the `animate` command :)
----
Second note: the function `par` from the OP seems to return the same picture, just on different scales. This makes the animation basically constant, so I threw in a fixed rectangle in the xy plane because I couldn't immediately figure out the command to set `xmin`, `xmax` or the like for 3D plots. I also updated the `Polyhedron` command to use floating point numbers instead of integers. This makes `par` accept non-integral values for some reason, and thus means that the original interact from the OP should work with this updated `par` function.Thu, 29 Mar 2012 11:05:32 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?answer=13407#post-id-13407Comment by cole for <p>If you want to animate a 3d plot, I think there must be a better way. Here's one hacky workaround:</p>
<p>First, generate a "dummy animation" with the number of frames you want, and note where the output images are stored:</p>
<pre><code>N = 10
A = animate([i for i in range(N)])
print A.png()
os.system('ls '+ A.png())
</code></pre>
<p>This will return something like</p>
<pre><code>/sagenb/sagenbws/.sage/temp/mod.math.washington.edu/13021/dir_3
00000000.png 00000002.png 00000004.png 00000006.png 00000008.png
00000001.png 00000003.png 00000005.png 00000007.png 00000009.png
</code></pre>
<p>Now save the list of images you want to animate, using the same names as those listed above:</p>
<pre><code>def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp, field=RDF)
return G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black')+plot3d(0,(0,200),(0,300),color='red',opacity=.1)
for i in range(N):
P = par(2+i)
P.save(A.png()+'/%08d.png'%i)
</code></pre>
<p>When you call <code>A.show()</code>, it just checks for images in the directory returned by <code>A.png</code> and uses <code>convert</code> or <code>ffmpeg</code> to return a gif animation. Since you've cleverly replaced the original images generated by the <code>animate</code> command with the ones you want, then you will get your animation with </p>
<pre><code>A.show()
</code></pre>
<p><img src="/upfiles/13330552838285333.gif" width="300"/></p>
<hr/>
<p>Note: it's been bugging me for a long time that there's no good way to animate 3D plots. Maybe this hack will convince someone else that they should figure out a better way to do it! Alternatively, maybe this will tell me that there's already a ticket open for fixing the <code>animate</code> command :)</p>
<hr/>
<p>Second note: the function <code>par</code> from the OP seems to return the same picture, just on different scales. This makes the animation basically constant, so I threw in a fixed rectangle in the xy plane because I couldn't immediately figure out the command to set <code>xmin</code>, <code>xmax</code> or the like for 3D plots. I also updated the <code>Polyhedron</code> command to use floating point numbers instead of integers. This makes <code>par</code> accept non-integral values for some reason, and thus means that the original interact from the OP should work with this updated <code>par</code> function.</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=53472#post-id-534728 years later, sage still doesn't have 3d animation as far as I can tell. This solution still works, however :)Wed, 16 Sep 2020 15:17:31 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=53472#post-id-53472Comment by Zheng for <p>If you want to animate a 3d plot, I think there must be a better way. Here's one hacky workaround:</p>
<p>First, generate a "dummy animation" with the number of frames you want, and note where the output images are stored:</p>
<pre><code>N = 10
A = animate([i for i in range(N)])
print A.png()
os.system('ls '+ A.png())
</code></pre>
<p>This will return something like</p>
<pre><code>/sagenb/sagenbws/.sage/temp/mod.math.washington.edu/13021/dir_3
00000000.png 00000002.png 00000004.png 00000006.png 00000008.png
00000001.png 00000003.png 00000005.png 00000007.png 00000009.png
</code></pre>
<p>Now save the list of images you want to animate, using the same names as those listed above:</p>
<pre><code>def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp, field=RDF)
return G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black')+plot3d(0,(0,200),(0,300),color='red',opacity=.1)
for i in range(N):
P = par(2+i)
P.save(A.png()+'/%08d.png'%i)
</code></pre>
<p>When you call <code>A.show()</code>, it just checks for images in the directory returned by <code>A.png</code> and uses <code>convert</code> or <code>ffmpeg</code> to return a gif animation. Since you've cleverly replaced the original images generated by the <code>animate</code> command with the ones you want, then you will get your animation with </p>
<pre><code>A.show()
</code></pre>
<p><img src="/upfiles/13330552838285333.gif" width="300"/></p>
<hr/>
<p>Note: it's been bugging me for a long time that there's no good way to animate 3D plots. Maybe this hack will convince someone else that they should figure out a better way to do it! Alternatively, maybe this will tell me that there's already a ticket open for fixing the <code>animate</code> command :)</p>
<hr/>
<p>Second note: the function <code>par</code> from the OP seems to return the same picture, just on different scales. This makes the animation basically constant, so I threw in a fixed rectangle in the xy plane because I couldn't immediately figure out the command to set <code>xmin</code>, <code>xmax</code> or the like for 3D plots. I also updated the <code>Polyhedron</code> command to use floating point numbers instead of integers. This makes <code>par</code> accept non-integral values for some reason, and thus means that the original interact from the OP should work with this updated <code>par</code> function.</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20017#post-id-20017Wow this is really magical. Thanks a lot!Thu, 29 Mar 2012 12:46:46 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20017#post-id-20017Comment by niles for <p>If you want to animate a 3d plot, I think there must be a better way. Here's one hacky workaround:</p>
<p>First, generate a "dummy animation" with the number of frames you want, and note where the output images are stored:</p>
<pre><code>N = 10
A = animate([i for i in range(N)])
print A.png()
os.system('ls '+ A.png())
</code></pre>
<p>This will return something like</p>
<pre><code>/sagenb/sagenbws/.sage/temp/mod.math.washington.edu/13021/dir_3
00000000.png 00000002.png 00000004.png 00000006.png 00000008.png
00000001.png 00000003.png 00000005.png 00000007.png 00000009.png
</code></pre>
<p>Now save the list of images you want to animate, using the same names as those listed above:</p>
<pre><code>def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp, field=RDF)
return G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black')+plot3d(0,(0,200),(0,300),color='red',opacity=.1)
for i in range(N):
P = par(2+i)
P.save(A.png()+'/%08d.png'%i)
</code></pre>
<p>When you call <code>A.show()</code>, it just checks for images in the directory returned by <code>A.png</code> and uses <code>convert</code> or <code>ffmpeg</code> to return a gif animation. Since you've cleverly replaced the original images generated by the <code>animate</code> command with the ones you want, then you will get your animation with </p>
<pre><code>A.show()
</code></pre>
<p><img src="/upfiles/13330552838285333.gif" width="300"/></p>
<hr/>
<p>Note: it's been bugging me for a long time that there's no good way to animate 3D plots. Maybe this hack will convince someone else that they should figure out a better way to do it! Alternatively, maybe this will tell me that there's already a ticket open for fixing the <code>animate</code> command :)</p>
<hr/>
<p>Second note: the function <code>par</code> from the OP seems to return the same picture, just on different scales. This makes the animation basically constant, so I threw in a fixed rectangle in the xy plane because I couldn't immediately figure out the command to set <code>xmin</code>, <code>xmax</code> or the like for 3D plots. I also updated the <code>Polyhedron</code> command to use floating point numbers instead of integers. This makes <code>par</code> accept non-integral values for some reason, and thus means that the original interact from the OP should work with this updated <code>par</code> function.</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20018#post-id-20018p.s. note that the saved images are generated with Tachyon by default, and thus look somewhat different from what you see with the jmol viewer.Thu, 29 Mar 2012 11:07:34 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20018#post-id-20018Answer by niles for <p>Hi there, I got confused about the interact:</p>
<pre><code>def par(t):
tmp = [[200-t,-1,0,0],[300-t,0,-1,0],[400-t,-1,-1,-1],[600-t,0,-1,-3],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
G = Polyhedron(ieqs = tmp)
show(G.render_solid(rgbcolor='blue',alpha=.2)+G.render_wireframe(rgbcolor='black'))
@interact
def _(x=(0,10)):
par(x)
</code></pre>
<p>It is quite weird since in the examples of <a href="http://wiki.sagemath.org/interact/graphics">http://wiki.sagemath.org/interact/gra...</a>, it seems that the interactive parameters could only be something controlling a plot. However in my case, the interactive parameter(x) is not used for controlling plot but for obtaining an [[],[]]. so it fails to interact. Any idea of that?</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?answer=13394#post-id-13394The problem seems to be that `par` should only take integer inputs. Even outside of the interact, `par(2.5)` does not show a picture but returns a number. You can restrict the possible inputs to integers by giving a step size for the parameters in the interact:
@interact
def _(x=(0,10,1)):
par(x)
This now shows pictures for each (integer) value of `x`.Thu, 29 Mar 2012 02:45:29 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?answer=13394#post-id-13394Comment by Zheng for <p>The problem seems to be that <code>par</code> should only take integer inputs. Even outside of the interact, <code>par(2.5)</code> does not show a picture but returns a number. You can restrict the possible inputs to integers by giving a step size for the parameters in the interact:</p>
<pre><code>@interact
def _(x=(0,10,1)):
par(x)
</code></pre>
<p>This now shows pictures for each (integer) value of <code>x</code>.</p>
https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20019#post-id-20019Great! Thanks. in this way we should be able to animate a 3D plot. B.T.W. is it because Polyhedron only takes integer input that makes the function par(t) only takes integer inputs? An alternative way to animate the behavior of the Polyhedron is by using a loop and time.sleep(#). But it seems not work.Thu, 29 Mar 2012 09:33:19 -0500https://ask.sagemath.org/question/8834/interact-3d-plotpolyhedron/?comment=20019#post-id-20019