5
$\begingroup$

Is there any easy way to have an animated bar chart (one where the heights of the bars change with time)? I currently have the following code:

α := 1/2
β := 1/2
γ := 0
δ := 0
ϵ := 0
ζ := 0
η := 1/2
θ := 1/2
w := 2 Pi

DSolve[{a'[t] == (-2 I*w/3) a[t], b'[t] + c'[t] + e'[t] == 0, 
d'[t] + f'[t] + g'[t] == 0, b'[t] - c'[t] == (I*w/3) (b[t] - c[t]), 
f'[t] - g'[t] == (I*w/3) (f[t] - g[t]), 
b'[t] + c'[t] - 2 e'[t] == (I*w/3) (b[t] + c[t] - 2 e[t]), 
2 d'[t] - f'[t] - g'[t] == (I*w/3) (2 d[t] - f[t] - g[t]), 
h'[t] == (-2 I*w/3) h[t], a[0] == α, b[0] == β, 
c[0] == γ, d[0] == δ, e[0] == ϵ, 
f[0] == ζ, g[0] == η, h[0] == θ}, {a[t], b[t], 
c[t], d[t], e[t], f[t], g[t], h[t]}, t]

Animate[Show[
BarChart[{{Re[a[t]] /. %, Im[a[t]] /. %}, {Re[b[t]] /. %, 
Im[b[t]] /. %}, {Re[c[t]] /. %, Im[c[t]] /. %}, {Re[d[t]] /. %, 
Im[d[t]] /. %}, {Re[e[t]] /. %, Im[e[t]] /. %}, {Re[f[t]] /. %, 
Im[f[t]] /. %}, {Re[g[t]] /. %, Im[g[t]] /. %}, {Re[h[t]] /. %, 
Im[h[t]] /. %}}], BoxRatios -> Automatic], {t, 0, 30}, 
AnimationRate -> 1, AnimationRunning -> False, RefreshRate -> 30]

I have 8 differential equations being solved, and the real and imaginary parts of each solution is being plotted, so there are 8x2 bars. Understandably, though, this gives me the error 'BarChart is not a type of graphics'. Any help would be greatly appreciated, and thanks in advance ;)

$\endgroup$
  • 2
    $\begingroup$ There are a surprisingly large number of posts recently where Show is unnecessarily used. I'm wondering if these posts all originate from the same classroom and are due to (incorrect) teacher instruction. $\endgroup$ – Mike Honeychurch Nov 17 '15 at 23:36
  • 1
    $\begingroup$ There's a lot of close votes here, but I cannot agree that the mistake is "simple". It's a matter of Animate localizing the variable t, while the output from DSolve has a different t in mind. I'm quite sure, a duplicate question (or rather one that points specifically to localization) should be here somewhere. $\endgroup$ – LLlAMnYP Nov 18 '15 at 9:15
8
$\begingroup$

There are two problems. One is that in your DSolve call, you should solve for the functions {a, b, ...} instead of the expressions {a[t], b[t],...}. (In my experience, it's almost always better this way.) The other is that to get the proper list structure for BarChart, you should use First@DSolve[..] to remove an unnecessary `{}.

dsol = First@
   DSolve[{a'[t] == (-2 I*w/3) a[t], b'[t] + c'[t] + e'[t] == 0, 
     d'[t] + f'[t] + g'[t] == 0, 
     b'[t] - c'[t] == (I*w/3) (b[t] - c[t]), 
     f'[t] - g'[t] == (I*w/3) (f[t] - g[t]), 
     b'[t] + c'[t] - 2 e'[t] == (I*w/3) (b[t] + c[t] - 2 e[t]), 
     2 d'[t] - f'[t] - g'[t] == (I*w/3) (2 d[t] - f[t] - g[t]), 
     h'[t] == (-2 I*w/3) h[t], a[0] == α, b[0] == β, 
     c[0] == γ, d[0] == δ, e[0] == ϵ, 
     f[0] == ζ, g[0] == η, h[0] == θ},
    {a, b, c, d, e, f, g, h}, t];

Animate[
 BarChart[{{Re[a[t]], Im[a[t]]}, {Re[b[t]], Im[b[t]]},
           {Re[c[t]], Im[c[t]]}, {Re[d[t]], Im[d[t]]},
           {Re[e[t]], Im[e[t]]}, {Re[f[t]], Im[f[t]]},
           {Re[g[t]], Im[g[t]]}, {Re[h[t]], Im[h[t]]}} /. dsol,
   PlotRange -> 0.5 {-1, 1}, Frame -> True],
 {t, 0, 30}, AnimationRate -> 1, AnimationRunning -> False, 
 RefreshRate -> 30]

enter image description here

For those with V10.1+ and a fear of braces:

Animate[BarChart[ReIm@Through@{a, b, c, d, e, f, g, h}@t /. dsol, 
  PlotRange -> 0.5 {-1, 1}, Frame -> True], {t, 0, 30}, 
 AnimationRate -> 1, AnimationRunning -> False, RefreshRate -> 30]
$\endgroup$
  • $\begingroup$ Fantastic! Thanks for the help ;) $\endgroup$ – turbodiesel4598 Nov 17 '15 at 23:58
  • $\begingroup$ @turbodiesel4598 You're welcome. $\endgroup$ – Michael E2 Nov 17 '15 at 23:59
  • $\begingroup$ +1, please consider (ReIm[#@t] & /@ {a, b, c, d, e, f, g, h}) /. dsol $\endgroup$ – Kuba Nov 18 '15 at 7:37
  • $\begingroup$ @Kuba Thanks, and my own version. For some reason I don't like ReIm. Maybe when they get to 30000 functions in WL, I'll feel more friendly to these trivial extensions of the language. (Also it's less work to copy/paste/replace than to think.) $\endgroup$ – Michael E2 Nov 18 '15 at 11:04
-3
$\begingroup$
barData = Table[{RandomReal[], RandomReal[], RandomReal[]}, {5}];
Manipulate[
 BarChart[barData[[i]]],
 {i, 1, 5, 1}]

or

Animate[
 BarChart[barData[[i]]],
 {i, 1, 5, 1}]
$\endgroup$
  • 1
    $\begingroup$ Maybe you meant this as a joke, but I don't see how this helps the OP solve the problem. (I guess it answers the title of the question, but not the body.) $\endgroup$ – Michael E2 Nov 17 '15 at 23:53
  • $\begingroup$ The question poser seemed to know how to get the raw data to be plotted (from the differential equations) but was asking about animating a bar graph. My answer addresses everything the poser asked, and was listed in the title. $\endgroup$ – David G. Stork Nov 17 '15 at 23:56
  • 1
    $\begingroup$ The OP does not have data to be plotted, not like barData. They have expressions that evaluate to numerical values. (That's the difference I see.) $\endgroup$ – Michael E2 Nov 17 '15 at 23:58

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.