我正在尝试创建一个动画绘图从一个文本文件,我在以前的代码中使用Odeint生成的。此外,系统的状况已被记录到不同的文本文件中。我已经把这些信息分成2个数组,分别是数字和条件。从这里开始,我将条件转换成我希望每个条件都显示为的文本,并将其存储在“文本框”中。现在,使用matplotlib,我想创建一个动画绘图,其中数据和文本都会出现和更改。下面是我的代码:import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.axes as text_maker
import time
# Read in the data for plotting DifEq solutions to x''=-x+c*x'+(Fo/m)cos(w*t)
data = open('data.txt','r')
# declare an array to hold the data
numbers = np.zeros((30,1001),dtype=float)
i = 0
j = 0
# loop through each line to put all data into its own column in array
for line in data:
x=line
if x=='XXXXXXXXXXXX\n': # each set ended with x string
i=i+1
j=0
else:
numbers[i,j]=x
j=j+1
data.close() # close the data file
# Read in the data concerning the conditions of each data set
labels = open('labels.txt','r')
# declare an array to hold the conditions
conditions = np.zeros((30,6), dtype = int)
j = 0
k = 0
# loop through each line to parse the data.
for line in labels:
x = line
conditions[j,:] = x.split(" ")
j=j+1
textboxes = [0,0,0,0,0,0]
while k<6:
a = conditions[k,0]
b = conditions[k,1]
c = conditions[k,2]
d = conditions[k,3]
e = conditions[k,4]
f = conditions[k,5]
textboxes[k] = "w = %d\nFo = %d\nc = %d\nx_0 = %d\nv_0 = %d\nm = %d" %(a,b,c,d,e,f)
k=k+1
time = np.linspace(0,25,1001)
# start the animation!
fig = plt.figure(figsize=(6,6), facecolor='white')
ax = fig.add_axes([0,0,1,1], frameon=False, aspect=1)
ax.set_xlim(0,max(time))
ax.set_ylim(-10000,10000)
ax.set_xlabel('Time (T) [arb units]')
ax.set_ylabel('Displacement (x) [arb units]')
ax.set_title('x\'\' = -x+c*x\'+(Fo/m)*cos(w*t)')
timetext = ax.text(100,100,'')
def init():
line.set_data([],[])
return lines,
def animate(i):
plot_y = numbers[i,:]
plot_x = time
timetext.set_text(textboxes[i])
line.set_data(plot_x,plot_y)
return tuple(line) + (timetext,)
ani = animation.FuncAnimation(fig,animate,init_func=init,frames=30,interval=500,blit=True)
plt.show()
很遗憾,我得到以下错误:
^{pr2}$
我尝试了我所知道的一切来改变这个设置,但我得到了各种各样的错误。有没有办法让这个图表上的文字和图形都有动画效果?在