Ask Your Question
1

Interact 3d plot(Polyhedron)

asked 2012-03-29 02:23:22 +0200

updated 2015-01-13 20:36:06 +0200

FrédéricC gravatar image

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/gra..., 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?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
3

answered 2012-03-29 09:45:29 +0200

niles gravatar image

The 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.

edit flag offensive delete link more

Comments

Great! 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.

Zheng gravatar imageZheng ( 2012-03-29 16:33:19 +0200 )edit
3

answered 2012-03-29 18:05:32 +0200

niles gravatar image

updated 2012-03-29 18:10:02 +0200

If 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()


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.

edit flag offensive delete link more

Comments

p.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.

niles gravatar imageniles ( 2012-03-29 18:07:34 +0200 )edit

Wow this is really magical. Thanks a lot!

Zheng gravatar imageZheng ( 2012-03-29 19:46:46 +0200 )edit

8 years later, sage still doesn't have 3d animation as far as I can tell. This solution still works, however :)

cole gravatar imagecole ( 2020-09-16 22:17:31 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-03-29 02:23:22 +0200

Seen: 989 times

Last updated: Mar 29 '12