2014-04-11 17 views
0

下面的代码已经从here采取和修改,以适应我的要求:使用Func键动画蟒蛇

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
import math 
import linecache 
fig = plt.figure() 
ax = plt.axes(xlim=(0,600), ylim=(0,600)) 
line, = ax.plot([], [], lw=3, color='r') 
mul=2*math.pi/3 
samp_rate=10 
print ax 
def init(): 
    line.set_data([], []) 
    return line, 

def animate(i): 
    print i 
    global mul 
    global samp_rate 
    line1=linecache.getline("data.txt", i+1) 
    if i==0: 
     x = float(line1)*math.cos(0) 
     y = float(line1)*math.sin(0) 
     line.set_data(x, y) 
     return line, 
    else: 
     x=float(line1)*math.cos((i)*mul/samp_rate) 
     y=float(line1)*math.cos((i)*mul/samp_rate) 
     line.set_data(x, y) 
     return line, 

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=5, blit=True) 

plt.show() 

上打印斧头给出的输出如下:

Axes(0.125,0.1;0.775x0.8)

我设置值从0到600,但是限制是0.775 to 0.8?这是为什么? 另外,我正在密谋值从100到400和输出窗口:

enter image description here

我要去哪里错了?

EDIT 1:

我改变了代码比特。现在,我从列表中输入值,即,我不打开文件的值,我只是用值创建一个列表。该代码是:

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
import math 
import linecache 
# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0,600), ylim=(0,600)) 
line, = ax.plot([], [], lw=3, color='r') 
mul=2*math.pi/3 
samp_rate=10 
print ax 

def init(): 
    line.set_data([], []) 
    return line, 

def animate(i): 
    print i 
    global mul 
    global samp_rate 
    line1=linecache.getline("data.txt", i+1) 
    x=[some vlaues that I have to plot] 
    y=[some vlaues that I have to plot] 
    line.set_data(x, y) 
    i+=1 
    return line, 

anim = animation.FuncAnimation(fig,animate,init_func=init,frames=200,interval=24,blit=True) 

plt.show() 

在这种情况下,输出如下: enter image description here

而且,因为我马上打印的i值作为我进入功能动画提供的输出不断增加。

我已经为x提供了约20k的值,并为y提供了相同数量的值。正如你在屏幕截图中看到的那样,并不是将它们全部绘制在一起。我如何绘制所有点?

回答

0

print(ax)不会给你x和y的限制。它给出了图中轴的位置。 print使用方法__str__。在IPython中,你可以用看的方法的文档:

In [16]: ax.__str__?? 
Source: 
    def __str__(self): 
     return "Axes(%g,%g;%gx%g)" % tuple(self._position.bounds) 

您还可以得到轴与位置:

In[4]:ax._position.bounds 
Out[4]: (0.125, 0.099999999999999978, 0.77500000000000002, 0.80000000000000004) 

为了得到X和轴Y的限制使用:

In[2]: ax.get_ylim() 
Out[2]: (0.0, 600.0) 

In[3]: ax.get_xlim() 
Out[3]: (0.0, 600.0) 

我不知道为什么线条行不出来,看不到data.txt的内容。我建议检查line1是你期望的。

+0

我调试,发现'line1'是我的预期。还有什么可以出错? –