Bar graph in subplot2grid

我只是一个虾纸丫 提交于 2019-11-29 16:50:32

The error comes from the line return im1, im2, rects.

While in the working solution, you have return rects, i.e. you return a list of artists which have a set_animated method. In the code that fails you have a tuple of one BarContainer and two artists. As the error suggests, AttributeError: 'BarContainer' object has no attribute 'set_animated'.

A solution might be to produce a list of the contents of the BarContainer which you can concatenate to the other two artists.

return [rect for rect in rects]+[im1, im2]

A full working example:

import matplotlib.pyplot as plt
import matplotlib.animation as animation

res_x, res_y = [1,2,3], [1,2,3]

fig = plt.figure()
ax = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
ax2 = plt.subplot2grid((2, 2), (0, 1))
ax3 = plt.subplot2grid((2,2), (1,1))

rects = ax3.bar(res_x, res_y, color='b')
im1 = ax.imshow([[1,2],[2,3]], vmin=0)
im2 = ax2.imshow([[1,2],[2,3]], vmin=0)

def animate(i):

    im1.set_data([[1,2],[(i/100.),3]])
    im2.set_data([[(i/100.),2],[2.4,3]])

    for rect, yi in zip(rects, range(len(res_x))):
        rect.set_height((i/100.)*(yi+0.2))
    return [rect for rect in rects]+[im1, im2]

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!