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.
ListAnimateon 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 withinAnimatetogether withDisplayAllSteps -> True, and perhaps withAnimationRate. – MarcoB Oct 02 '16 at 00:41ListAnimate. 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 usingDynamicinside the body ofManipulateto 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. – Pirx Oct 02 '16 at 00:57Animateinsists 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. – Pirx Oct 02 '16 at 00:59spp(it is aGraphics[]object after all) and put that instead in the animation. – J. M.'s missing motivation Oct 02 '16 at 13:45Animate[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? – Pirx Oct 02 '16 at 13:50spp, you can't do that withTimingorAbsoluteTiming, which just measure the Kernel time. Here is what I do: In one input cell, putfoo = SessionTime[]; AbsoluteTiming[Show[spp]]. In another cell just below it, putSessionTime[] - foo. Select both cells an execute simultaneously. The FE sends the first cell to the Kernel; gets the output and displays it; then sends the second cell etc. – Michael E2 Oct 03 '16 at 02:50