2014-07-20 58 views
0
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import time 

fig=plt.figure() 
ax1=fig.add_subplot(1,1,1) 

def animate(): 
    data = np.loadtxt("new.txt") 
    ax1.plot(data[:,0], data[:,1]) 
    return 

ani=animation.FuncAnimation(fig,animate,frames=1000) 
plt.show() 

誤差坡平了是 類型錯誤:動畫()沒有參數(1給出) 做什麼?動態圖使用matplotlib

+0

從'animate'(我從來沒有使用它)刪除括號'()',但這是文檔說的。而事實上這兩個錯誤都是一些姓名錯字。 .. – Hima

回答

0

似乎有在你的代碼的兩個錯誤:

  1. 你試圖讀取並真的不存在或不是由你的腳本讀取文件。 (這就是爲什麼你會收到錯誤信息。)
  2. 由於Hima正確地說,FuncAnimation預計會收到一個函數(即animate),而animate()是該函數的返回值。

然後有一件事你應該考慮。如果您的數據是簡單的文件,您可以嘗試使用numpy.loadtxt來讀取該文件。在這種情況下,你的函數animate會是這樣的:

import numpy as np 

def animate(): 
    data = np.loadtxt("myfile.txt") 
    ax1.plot(data[:,0], data[:,1]) 

然而,即使你最終會與越來越多的地塊,因爲每個plot命令創建一個新的行。相反,您可能需要查看set_xdata方法。 (因爲我認爲這是一個學校作業,所以我不會給出完整的解決方案。)

+0

當我使用這段代碼時,出現了一個錯誤:TypeError:animate()不接受任何參數(給出1)...請幫助我,我必須在早上提交它 – user3820379

+0

PLease更新您的問題中的代碼什麼給你錯誤。請確保您通過啓動動畫'ANI = animation.FuncAnimate(圖,動畫,幀= 1000)' – DrV

+0

進口matplotlib.pyplot如PLT 進口matplotlib.animation動畫 進口時間 進口numpy的爲NP 圖= plt.figure() AX1 = fig.add_subplot(1,1,1) DEF動畫(): 數據= np.loadtxt( 「new.txt」) ax1.plot(數據[:,0] ,data [:,1]) return ani = animation.FuncAnimation(fig,animate,frames = 1000) plt.show() – user3820379