5
$\begingroup$

This test code is working and displays always the same histogram and stops at n=5.

nbars = 20;
data = Array[0 &, nbars];

Animate[
 data = RandomInteger[1, nbars];
 BarChart[data],
 {n, 1, 5, 1}, AnimationRepetitions -> 1
 ]

The following code in which the list data should be added to its previous values runs endless, why is it so?

nbars = 20;
data = Array[0 &, nbars];

Animate[
 data = data + RandomInteger[1, nbars];
 BarChart[data],
 {n, 1, 5, 1}, AnimationRepetitions -> 1
 ]
$\endgroup$
  • $\begingroup$ other oddities, notice your data gets updated 30+times before n gets to 5, and when it reaches 5 the "play" icon is displayed as if its stopped. Also AnimationRunning -> False does not work $\endgroup$ – george2079 Oct 21 '16 at 17:50
  • $\begingroup$ as a workaround do ListAnimate[Table[data = data + RandomInteger[1, nbars]; BarChart[data], {n, 1, 5, 1}]] $\endgroup$ – george2079 Oct 21 '16 at 17:58
  • $\begingroup$ Thank you for your help. Do you think this is an error? $\endgroup$ – lio Oct 22 '16 at 8:27
  • $\begingroup$ i cant see any sense to why it should behave that way or anything inherently wrong with the code. You should put your version info in the question in case it gets a bug label. $\endgroup$ – george2079 Oct 22 '16 at 12:04
4
$\begingroup$

This is not a bug.

Animate is a Dynamic function, this means that everything within Animate is repeatedly updated, regardless of whether the function of steps {n, 1, 5, 1} is running or not. Note that replacing Animate with Manipulate shows this explicitly.

To understand how this works, try running

a = 2;
Animate[Plot[a x + n, {x, 0, 2}], {n, 1, 5, 1}]

Then evaluate a=-2; in another cell, you will see that the plot output changes.

To update data, and run the code with Animate[] instead of ListAnimate[], you can run:

nbars = 20;
data = ConstantArray[0, {5, nbars}];
Table[data[[i]] = data[[Max[i - 1, 1]]] + RandomInteger[1, nbars], {i,
    5}];
Animate[BarChart[data[[n]]], {n, 1, 5, 1}, AnimationRepetitions -> 1]
$\endgroup$
  • $\begingroup$ why exactly does it display the "play" button, which would ordinarily indicate that the animation is stopped? I do agree dynamic functions are so generally unpredictable its hard to call anything a bug.. $\endgroup$ – george2079 Oct 22 '16 at 16:23
  • $\begingroup$ @george2079 The play goes through n, 1 through 5, (which in this case doesn't do anything), it doesn't affect whether or not the function is dynamic. Aside from that every "frame" it's updating data = data + RandomInteger[1, nbars];, this is independent of whether play is active. You can go to "Evaluation" in the menu and uncheck "Dynamic updating enabled", which will stop data from being updated. $\endgroup$ – Feyre Oct 23 '16 at 8:35
  • $\begingroup$ Thank you ... now I understand much more. $\endgroup$ – lio Oct 24 '16 at 9:11
4
$\begingroup$

armed with knowledge that this is a dynamic/manipulate issue we can fix by telling Manipulate via TrackedSymbols not to track data :

nbars = 20;
data = Array[0 &, nbars];
Animate[
 data = data + RandomInteger[1, nbars];
 BarChart[data], {n, 1, 5, 1}, TrackedSymbols -> {n}, 
 AnimationRepetitions -> 1]

this sort-of works, running through n and stopping, although it updates data 6 times, not 5. (If you set nbars large, say 100, you will consistently get a few bars with value 6. ) Additionally, if you manually move the slider after it stops it will update data with each move. ( not clear what you actually expect )

If you do this,

nbars = 20;
data = Array[0 &, nbars];
ListAnimate[
 Table[data = data + RandomInteger[1, nbars]; 
  BarChart[data], {n, 1, 5, 1}], AnimationRepetitions -> 1]

you get exactly 5 frames, that do not change after the first run though.

$\endgroup$
  • $\begingroup$ This is excellent ... $\endgroup$ – lio Oct 24 '16 at 17:07
3
$\begingroup$

Answer from Wolfram Technology Group:

In your second example, the value of data is being changed (data = data + RandomInteger[1, nbars]) while the expression is getting evaluated, and, as you mentioned, for each change the Animation is repeated. This is As Designed. If you look at the FullForm of your expression (you can add //FullForm at the end of your expression and evaluate it), you can see that Animate is basically a Manipulate module (with Animator for ControlType and SynchronousUpdating set to True):

nbars = 20;
data = Array[0 &, nbars];

Manipulate[
 CompoundExpression[Set[data, Plus[data, RandomInteger[1, nbars]]], 
  BarChart[data]],
 List[
  n, 1, 5, 1, Rule[AnimationRepetitions, 1], 
  Rule[AppearanceElements, 
   List["ProgressSlider", "PlayPauseButton", "FasterSlowerButtons", 
    "DirectionButton"]]
  ],
 Rule[ControlType, Animator], Rule[AppearanceElements, None], 
 Rule[DefaultBaseStyle, "Animate"], 
 Rule[DefaultLabelStyle, "AnimateLabel"], 
 Rule[SynchronousUpdating, True], Rule[ShrinkingDelay, 10.`]
 ]

And if you look at the output cell for the first expression (without FullForm) by selecting the cell and pressing Shift+Ctrl+E (or clicking on "Show Expression" in the "Cell" menu in Mathematica), you can see that the output cell is a DynamicModuleBox. It will dynamically update with the updated value of "data".

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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