Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

As for the AttributeError, that's happening because the range function returns Python ints and the function expects Sage Integers. They're slightly different types, with different behaviour; usually in Sage you want Integers, so I seldom use range myself when writing Sage code. You can replace range with any of srange, xsrange, sxrange, IntegerRange, or write (1..49) instead to fix it, anything which returns an Integer.

[It should probably be considered a bug that this doesn't happen automatically here.]

To put a line in the background, though, I think it's easiest to forget that the animate command accepts functions and treat it merely as a tool to join plots together. That is, first you want to make the plot that you want, and then get animate to combine them. You can use the animate coercion as a shortcut sometimes, but when you try to start adding extra features it's much easier to work with plots as normal.

For example, something like:

frames = []
xr = (x, 0, 1)
for k in srange(1, 50):
    g = plot((sum((-1)^(n-1)*sin(n*x)/n,n,1,k)), xr, color="blue", legend_label='k = %d' % k)
    g += plot(x/2, xr, color="green", legend_label="x/2")
    frames.append(g)

a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))
a.show()

You could do the above in two lines if you really wanted to, but I think the above is clearer and more easily extensible.

As for the AttributeError, that's happening because the range function returns Python ints and the function expects Sage Integers. They're slightly different types, with different behaviour; usually in Sage you want Integers, so I seldom use range myself when writing Sage code. You can replace range with any of srange, xsrange, sxrange, IntegerRange, or write (1..49) instead to fix it, anything which returns an Integer.

[It should probably be considered a bug that this doesn't happen automatically here.]

To put a line in the background, though, I think it's easiest to forget that the animate command accepts functions and treat it merely as a tool to join plots together. That is, first you want to make the plot plots that you want, and then get animate to combine them. You can use the animate coercion as a shortcut sometimes, but when you try to start adding extra features it's much easier to work with plots as normal.

For example, something like:

frames = []
xr = (x, 0, 1)
for k in srange(1, 50):
    g = plot((sum((-1)^(n-1)*sin(n*x)/n,n,1,k)), xr, color="blue", legend_label='k = %d' % k)
    g += plot(x/2, xr, color="green", legend_label="x/2")
    frames.append(g)

a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))
a.show()

You could do the above in two lines if you really wanted to, but I think the above is clearer and more easily extensible.

As for the AttributeError, that's happening because the range function returns Python ints and the function expects Sage Integers. They're slightly different types, with different behaviour; usually in Sage you want Integers, so I seldom use range myself when writing Sage code. You can replace range with any of srange, xsrange, sxrange, IntegerRange, or write (1..49) instead to fix it, anything which returns an Integer.

[It should probably be considered a bug that this doesn't happen automatically here.]

To put a line in the background, though, I think it's easiest to forget that the animate command accepts functions and treat it merely as a tool to join plots together. That is, first you want to make the plots that you want, and then get animate to combine them. You can use the animate coercion as a shortcut sometimes, but when you try to start adding extra features it's much easier to work with plots as normal.

For example, something like:

n = var("n")
frames = []
xr = (x, 0, 1)
for k in srange(1, 50):
    g = plot((sum((-1)^(n-1)*sin(n*x)/n,n,1,k)), xr, color="blue", legend_label='k = %d' % k)
    g += plot(x/2, xr, color="green", legend_label="x/2")
    frames.append(g)

a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))
a.show()

You could do the above in two lines if you really wanted to, but I think the above is clearer and more easily extensible.