0

I've successfully animated a single bar chart using FuncAnimation, but I would now like to have 3 bar charts concurrently in subplots. I can't work out what my animation function should return. I tried to adapt my code for one bar chart to make it work for 3. This was my attempt but I get an error saying "AttributeError: 'BarContainer' object has no attribute 'get_zorder'". My data 'soln' is a 3x1000x10 list. The 3 is for each subplot, and the 10 is for each bar.

I believe this is a minimum example. enter image description here


import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

soln = np.array([[[1, 0, 0],[1,1,0],[1, 2, 3]],[[1, 0, 0],[1,1,0],[1, 2, 3]],[[1, 0, 0],[1,1,0],[1, 2, 3]]])
print(soln.shape)

def animate(frame):
    for i, list in enumerate(rects):
        for j, rect in enumerate(list):
            rect.set_height(soln[i, frame, j])
    return rects

fig, axs = plt.subplots(1, 3)
xs = range(0, 3)
rects = []
for i in range(soln.shape[0]):
    rects.append(axs[i].bar(xs, soln[i, 0, :]))
ani = animation.FuncAnimation(fig, animate, blit=True, repeat=False)
TIF
  • 191
  • 1
  • 2
  • 11
  • Your code works fine for me using matplotlib v.3.3.1. If it doesn't for you provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that includes a toy dataset (refer to [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples)) – Diziet Asahi Oct 30 '20 at 14:59
  • Okay, I've now added what I believe is an appropriate example. Cheers – TIF Oct 30 '20 at 15:17
  • seems to work fine. **However**, you are overwriting the builtin `list` in your function, which can cause all sorts of problem. Use another name for that variable. – Diziet Asahi Oct 30 '20 at 15:29
  • I'm getting the first frame of the animation appear in a matplotlib window, and then the attribute error again without the rest of the animation occurring. I've just updated matplotlib (to 3.3.2, with python 3.8) to no avail – TIF Oct 30 '20 at 15:34
  • [Plotly Express](https://plotly.com/python/animations/) is good at animating these types of graphs with minimal code – Jacob K Nov 01 '20 at 23:56

0 Answers0