Ask Your Question

Making an array of animations

asked 2012-07-14 21:37:22 +0200

jaia gravatar image

I'm making a simple demo of the relationship between the unit circle and the sine function, like the one at I would like to plot the circle and function side by side, but graphics_array gives an error, saying that all its elements must be graphics. Is there anything I can do?

Here's the code as it now stands.

singraph=animate([point((i,sin(i)), color="green", size=50) + plot(sin(x),(0,2*pi)) for i in srange(0,2*pi,0.2)], xmin=0, xmax=7, ymin=-1, ymax=1, figsize=[2,2])
unitcircle = animate([point((cos(i),sin(i)), color="green", size=50) + circle((0,0),1, color="blue") for i in srange(0,2*pi,0.2)], figsize=[2,2])
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-07-14 22:52:56 +0200

benjaminfjones gravatar image

updated 2012-07-14 23:02:42 +0200

The following doesn't work (but maybe it could after patching animate). Make a graphics array for each frame of the animation and then use animate on the result. Something like this:

frames = []
for i in srange(0,2*pi,0.2):
    singraph = point((i,sin(i)), color="green", size=50)
    singraph += plot(sin(x),(0,2*pi), xmin=0, xmax=7, ymin=-1, ymax=1, figsize=[2,2])
    unitcircle = point((cos(i),sin(i)), color="green", size=50)
    unitcircle += circle((0,0),1, color="blue", figsize=[2,2])
    frames.append(graphics_array([singraph, unitcircle]))


This doesn't work because animate expects a list of Graphics objects and GraphicsArray is not a Graphics object in Sage because of the way Graphics objects are expected to behave. For example how would adding to GraphicsArray objects be handled? Each array has multiple plots in it so it's not clear what the behavior should be. If you give animate non Graphics objects, then Sage tries to call plot on them which is what really fails in this example.

Here is a solution that works. After reading the source code for animate I discovered that you can "superimpose" two animations frame by frame by simply adding the corresponding animation objects. This isn't documented in the docstring for animate unfortunately. The following code does what you want:

sin_frames = []
circ_frames = []
circ_x = -1.5 # offset center of the circle
circ_y = 0
for i in srange(0,2*pi,0.2):
    singraph = point((i,sin(i)), color="green", size=50)
    singraph += plot(sin(x),(0,2*pi), xmin=0, xmax=7, ymin=-1, ymax=1, figsize=[2,2], axes=False)
    unitcircle = point((cos(i)+circ_x,sin(i)+circ_y), color="green", size=50)
    unitcircle += circle((circ_x,circ_y),1, color="blue", figsize=[2,2], axes=False)

A1 = animate(sin_frames)
A2 = animate(circ_frames)
show(A1+A2) # superimpose frames
edit flag offensive delete link more


Please open a ticket to document this, Ben :)

kcrisman gravatar imagekcrisman ( 2012-07-16 10:28:33 +0200 )edit

Done. This is now #13279

benjaminfjones gravatar imagebenjaminfjones ( 2012-07-21 03:10:52 +0200 )edit

answered 2012-07-14 23:06:05 +0200

calc314 gravatar image

Here is something else that doesn't work, but I think someone else might be able to help with fixing it. The idea is to use html to get the animations side-by-side.

html('<td>TITLE HERE</td></tr>')

At the moment, these animations don't come out side-by-side. However, the animations do appear to be in sync.

edit flag offensive delete link more


Here's a simpler version of your code: `html.table([['One graph','Another graph'],[,]])` (You could use `show()` or `save()`.) The problem is that the returned object in both cases is None, so that is what shows up in the HTML table; the graphics are always put afterwords by Sagenb's code for finding graphics to show. This would be an interesting sagenb problem to fix.

kcrisman gravatar imagekcrisman ( 2012-07-16 10:33:36 +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



Asked: 2012-07-14 21:37:22 +0200

Seen: 1,403 times

Last updated: Jul 14 '12