Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

python - Bar graph in subplot2grid

I want to have several subplots, two of which showing frames from a video feed, and a third showing computed results as a bar graph. After creating a matplotlib figure, I create several subplot2grids, which I then update with FuncAnimation.

The usual way I would create a bar graph (to be updated) is:

fig = plt.figure()
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
rects = plt.bar(res_x, res_y, color='b')

def animate(args):
    ...
    ...
    for rect, yi in zip(rects, results):
        rect.set_height(yi*100)
    return rects

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

I am now trying to add a bar graph along side the other subplots:

fig = plt.figure()
plt1 = plt.subplot2grid((2, 2), (0, 0), rowspan=2)
plt2 = plt.subplot2grid((2, 2), (0, 1))

#Confusion with the following
bar_plot = plt.subplot2grid((2,2), (1,1))
ax = plt.axes(xlim=(0, 9), ylim=(0, 100))
rects = plt.bar(res_x, res_y, color='b')


def animate(args):
    ...
    ...

    im1 = plt1.imshow(...)
    im2 = plt2.imshow(...)

    for rect, yi in zip(rects, results):
        rect.set_height(yi*100)
    return im1, im2, rects

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20, blit=True)
plt.show()

I get the following error: AttributeError: 'BarContainer' object has no attribute 'set_animated'

Any ideas how I can "place" a bar graph as a subplot, and have it update together with other data from subplots?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...