I browsed similar questions related to Animate jerkiness like here, but haven't found the right options to control animation rate while also displaying all frames:

Animate[Prime[Range[400]] // Outer[(-Mod[#1, #2]/#2) &, # + k, #] & //
   Column@{Style[k, Large], 
     MatrixPlot[Transpose@#, Frame -> False, ImageSize -> 600]} &, {k,
   1, 1000, 1}, AnimationRate -> 1]

enter image description here

  • Even with smaller ImageSize occasionally a frame is skipped.

  • AnimationRate is difficult to tune and some changes seem to have no effect.

  • Alternatives like ListAnimate take too long to preprocess as noted by cormullion and others.

  • $\begingroup$ How about the option DisplayAllSteps->True of Animate? $\endgroup$
    – halirutan
    Dec 17, 2014 at 15:45
  • $\begingroup$ Also check ArrayPlot, I think it is faster than MatrixPlot, and you might try to precompute stuff, like the Prime[Range[400]], not big overhead, but you can keep the list in a variable. Also writing it all as an expression is slightly faster than with pure functions just applied once. $\endgroup$
    – FJRA
    Dec 17, 2014 at 16:02
  • $\begingroup$ @halirutan, Im hesitant to accept below b/c I tried DisplayAllSteps->True' with AnimationRate->10` skips steps w/o actually speeding up the animation. $\endgroup$ Dec 18, 2014 at 18:48

2 Answers 2


There are some place which can be optimized in your animation. When I see this right, then your function

Outer[(-Mod[#1, #2]/#2) &, # + k, #] &

is similar to

Outer[(-Mod[#1+k, #2]/#2) &, #, #] &

but the latter has the big advantage, that the calculation of your Outer does not rely on k. It is even better than that, because now we can calculate the your whole prime matrix upfront exactly one time and use it over and over again.

To apply the Mod expression to each matrix element, I would compile it down and make it Listable. Let's start with that:

myMod = Compile[{{p, _Integer, 1}, {k, _Integer, 0}},
  -Mod[p[[1]] + k, p[[2]]]/(p[[2]]), CompilationTarget -> "C", 
  RuntimeAttributes -> {Listable}, Parallelization -> True, 
  RuntimeOptions -> "Speed"]

If you don't have a C compiler installed then just remove the CompilationTarget option. In the final animation, we pre-calculate the data matrix. This should make the approach faster. To display the matrix, I like to use Image exactly as Jens did, because it is made for speed. If you want to color it, then apply Colorize or any other function but be aware that this costs time too:

With[{data = Transpose[Outer[List, #, #] &@Prime[Range[400]]]},
    Style[k, Large],
    Image[Rescale@myMod[data, k]]
  {k, 1, 1000, 1}, DisplayAllSteps -> True]

The speed improvement is enormous on my machine here and now you can reduce the animation speed and you should see the change.

  • $\begingroup$ Thanks for working out all those improvements that I hadn't even considered, the metric was to minimize code size. These are worth pursuing for a larger version. $\endgroup$ Dec 18, 2014 at 18:51
  • $\begingroup$ Something to keep in mind though is a comparison b/w Mathematica & C++ - to model benzene electronic structure I think - revealed a neck to neck, something like 5 hrs vs 5 1/2 hours - so not necessarily worth compiling. The future --> VM optimization. $\endgroup$ Dec 18, 2014 at 18:54

The fastest way to plot large data in my experience is Image, as I do here:

Animate[Prime[Range[400]] // Outer[(-Mod[#1, #2]/#2) &, # + k, #] & //
   Column@{Style[k, Large], 
     Colorize[Image[Transpose@Rescale@#, ImageSize -> 600], 
      ColorFunction -> "LakeColors"]} &, {k, 1, 1000, 1}, 
 AnimationRate -> 1]
  • $\begingroup$ Nice. However, using AnimationRate -> 10 skips steps while not actually speeding it up at all. $\endgroup$ Dec 18, 2014 at 18:46
  • $\begingroup$ Yes, the truth is that for one-parameter list of images I usually would suggest pre-computing all images and using ListAnimate, but you already ruled that out in the question. The reason I prefer this is that you will often want to display the animation more than once anyway, and then it would be increasingly inefficient to re-do the necessary computations every time, the more often you intend to display them. $\endgroup$
    – Jens
    Dec 18, 2014 at 19:13
  • $\begingroup$ Good points, but during exploratory analysis you cannot afford that cold start - too slow. Wish there were an interface to map b/w the methods. $\endgroup$ Dec 18, 2014 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.