Python中的动画

时间:2015-11-12 23:39:28

标签: python animation matplotlib

我正在尝试创建一个动画情节,从我使用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()

不幸的是我收到以下错误:

AttributeError                            Traceback (most recent call last)
C:\Users\Beaks\Documents\Clemson\research\DIFFEQS\plotting.py in <module>()
     80 
     81 
---> 82 ani = animation.FuncAnimation(fig,animate,init_func=init,frames=30,interval=500,blit=True)
     83 
     84 plt.show()

C:\Users\Beaks\Anaconda\lib\site-packages\matplotlib\animation.pyc in __init__(self, fig, func, frames, init_func, fargs, save_count, **kwargs)
   1065         self._save_seq = []
   1066 
-> 1067         TimedAnimation.__init__(self, fig, **kwargs)
   1068 
   1069         # Need to reset the saved seq, since right now it will contain data

C:\Users\Beaks\Anaconda\lib\site-packages\matplotlib\animation.pyc in __init__(self, fig, interval, repeat_delay, repeat, event_source, *args, **kwargs)
    911 
    912         Animation.__init__(self, fig, event_source=event_source,
--> 913                            *args, **kwargs)
    914 
    915     def _step(self, *args):

C:\Users\Beaks\Anaconda\lib\site-packages\matplotlib\animation.pyc in __init__(self, fig, event_source, blit)
    589 
    590         # Clear the initial frame
--> 591         self._init_draw()
    592 
    593         # Instead of starting the event source now, we connect to the figure's

C:\Users\Beaks\Anaconda\lib\site-packages\matplotlib\animation.pyc in _init_draw(self)
   1092             self._draw_frame(next(self.new_frame_seq()))
   1093         else:
-> 1094             self._drawn_artists = self._init_func()
   1095 
   1096     def _draw_frame(self, framedata):

C:\Users\Beaks\Documents\Clemson\research\DIFFEQS\plotting.py in init()
     68 
     69 def init():
---> 70         line.set_data([],[])
     71         return lines,
     72 

AttributeError: 'str' object has no attribute 'set_data'

我已经尝试了所有我知道的改变这种设置,但我得到了各种错误。有没有办法在此图表上为文本和图表设置动画?

0 个答案:

没有答案