I have an animation coded like this:
plt[t_] := ParametricPlot[{Cos[s] + t, Sin[s] + t (t + 2 Cos[s])}, {s, 0, 2 Pi}];
spp = StreamPlot[{1, 2 x}, {x, -1.2, 2.0}, {y, -1.2, 2.0}, StreamStyle -> {"Line"}];
Animate[Show[plt[u], spp, PlotRange -> All], {u, 0, 0.5}]
Which works fine and runs smoothly. However, if I want a more fancy StreamPlot
, dropping the "Line"
directive so I now have
plt[t_] := ParametricPlot[{Cos[s] + t, Sin[s] + t (t + 2 Cos[s])}, {s, 0, 2 Pi}];
spp = StreamPlot[{1, 2 x}, {x, -1.2, 2.0}, {y, -1.2, 2.0}];
Animate[Show[plt[u], spp, PlotRange -> All], {u, 0, 0.5}]
The animation slows down significantly and becomes really choppy. I'm fairly sure this is because the StreamPlot spp
takes a lot more work to compute now. However, spp
is fixed and does not change during the animation, and one would hope that Mathematica can take advantage of that fact.
I tried a few things I could think of, but I'd rather not embarrass myself even more by showing those; suffice it to say that none of them worked. The advanced documentation on Manipulate
talks about using Dynamic
inside the body of Manipulate to prevent Mathematica from updating parts that do not need to be updated, but I couldn't get that to work.
Is there a way to tell Mathematica to plot spp
, and then animate plt[t]
"on top of it", without reevaluating either?
Extended Comment:
Following J.M.'s suggestion, I just tried
plt[t_] := ParametricPlot[{Cos[s] + t, Sin[s] + t (t + 2 Cos[s])}, {s, 0, 2 Pi}];
spp = Graphics[StreamPlot[{1, 2 x}, {x, -1.2, 2.0}, {y, -1.2, 2.0}][[1]]];
Animate[Show[plt[u], spp, PlotRange -> All], {u, 0, 0.5}]
but the animation is just as choppy. However, if I do
Timing[Show[spp]]
I get 0.
. I take that to mean that showing the streamplot should not slow things down that much, but perhaps I am missing something.
ListAnimate
on the list. For instance,ListAnimate@ Table[Show[plt1, plt2[u], spp, PlotRange -> All], {u, 0, 0.5, 0.005}]
is pretty smooth. Alternatively, you could try and play with the step size in independent variable withinAnimate
together withDisplayAllSteps -> True
, and perhaps withAnimationRate
. $\endgroup$ListAnimate
. The issue with that is that it may take a long time to initialize the animation, and/or it makes the size of the notebook (and later the CDF) balloon enormously. The advanced documentation on Manipulate talks about usingDynamic
inside the body ofManipulate
to prevent Mathematica from updating parts that do not need to be updated, but I couldn't get that to work. I'm certain that it's possible to speed things up, I just seem to be unable to figure this out. $\endgroup$Animate
insists on redoing the streamplot for every frame, even though that plot never changes. If I could find a way to keep Mathematica from updating this plot, things would be perfect. $\endgroup$spp
(it is aGraphics[]
object after all) and put that instead in the animation. $\endgroup$Animate[Show[plt[u], Graphics[spp[[1]]], PlotRange -> All], {u, 0, 0.5}]
? That's just as slow as the first. P.S.: Are you saying you're not seeing a big difference between the two versions I have given? $\endgroup$