首页 > 解决方案 > 如何从列表列表中为直方图设置动画

问题描述

我有一份拍卖回报清单。每个列表都可以通过直方图进行汇总。我想为这个直方图制作动画以显示每个列表。

我尝试过 funcanimation 但我无法理解大部分文档。同样,我尝试使用来自类似堆栈溢出问题的代码,但我无法为我的 plt.hist 设置动画

#Attempt 1
a = [4,3,1,7,9,5,3,4,6,0,3,1,4,5,4,6,0]
b = [9,4,5,1,6,5,3,4,0,6,7,3,4,7,6,3,7]
c = [4,3,1,7,9,5,3,4,6,0,3,1,4,5,4,6,0]
d = [9,4,5,1,6,5,3,4,0,6,7,3,4,7,6,3,7]
e = [4,3,1,7,9,5,3,4,6,0,3,1,4,5,4,6,0]
data = [a,b,c,d,e]

number_of_frames = 5
def update_hist(num, data):
    plt.cla()
    plt.hist(data[num])

fig = plt.figure()
hist = plt.hist(data[0])

animation = animation.FuncAnimation(fig, update_hist,   number_of_frames, fargs=(data, ), interval = 2000 )
plt.show()

#Attempt 2
fig = plt.figure()
#creating a subplot 
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    ax1.clear()
    plt.hist(data[i])
    plt.ylabel('Frequency')
    plt.xlabel('Percentage Return')
    plt.title('Auction Histograph') 


ani = animation.FuncAnimation(fig, animate, interval=5000) 
plt.show()

这两种尝试都显示了第一个直方图,但它从未过渡到第二个。

标签: matplotlibanimationhistogram

解决方案


推荐阅读