# Making an array of animations

I'm making a simple demo of the relationship between the unit circle and the sine function, like the one at http://treeblurb.com/dev_math/sin_can.... 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])
singraph.show()
unitcircle.show()

edit retag close merge delete

Sort by » oldest newest most voted

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]))

show(animate(frames))


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)
sin_frames.append(singraph)
circ_frames.append(unitcircle)

A1 = animate(sin_frames)
A2 = animate(circ_frames)
show(A1+A2) # superimpose frames

more

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('<table><tr>')
html('<td>TITLE HERE</td></tr>')
html('<tr><td>')
singraph.show()
html('</td>')
html('<td>')
unitcircle.show()
html('</td>')
html('</tr></table>')


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

more

Here's a simpler version of your code: html.table([['One graph','Another graph'],[singraph.save(),unitcircle.save()]]) (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.