我正在使用Python创建多个直方图的动画,但是我无法这样做。我试过在python上使用plt.pause和time.sleep命令,但没有成功。这是代码:
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import math
import random
from numpy import zeros
from time import sleep
m = 1
g = 9.8
b = 0.1
A = math.sqrt((m*g)/b)
f=1000
a=[0]*f
z=np.zeros(f)
def velocity(tvel, vvel):
return(A - (math.pow(vvel,1)/A)+(random.randint(-500,500)))
for k in range(0,f):
endtime = 100
steps = 1000
h = endtime/steps
timearr = np.linspace(0,endtime,steps)
vIni = 0
v = vIni
varr= []
for t in timearr:
varr.append(v)
v1 = h*velocity(t,v)
v2 = h*velocity((t+(h/2)), (v+(v1/2)))
v3 = h*velocity((t+(h/2)), (v+(v2/2)))
v4 = h*velocity((t+(h/2)), (v+(v3/2)))
v = v+((1.0/6.0)*(v1+(2*v2)+(2*v3)+v4))
xrandom=np.zeros(len(varr))
xrandom[0]=0
c=0
for i in range(0,len(varr)-1):
c=c+((varr[i]+varr[i+1])/2)*0.01
xrandom[i+1]=c
a[k]=xrandom
for o in range(len(a)):
for i in range(0,f):
z[i]=a[i][o]
plt.hist(z,bins=150,normed=True)
plt.show()
本代码的基本功能是绘制1000个不同的直方图。我想要做的是创建一个动画,它将使用具有相关边界的固定帧显示每个直方图(按顺序)。我该怎么办?