2015-07-21 82 views
0

我想繪製我的中間結果,並想看看算法是如何進行的。我也發佈了演示代碼。假設我的算法歷時20個紀元,我想在同一個文件中繪製每個紀元的結果。我嘗試使用下面的演示代碼。但是我在a.png上看不到任何情節。matplotlib:連續覆蓋

有人可以幫我嗎我該怎麼辦?

import matplotlib.pylab as plt 
import numpy as np 


for i in range(20): 
    y = np.random.random() 
    plt.plot(i, y) 
    plt.savefig('a.png') 
+0

抓取引用到'Line2D'對象並使用'set_data' – tacaswell

回答

1

您必須在變量中提供整個歷史記錄,例如作爲列表:

import matplotlib.pylab as plt 
import numpy as np 

# creates two lists with the same length 
x = range(20) 
y = [0] * 20 
for i in x: 
    y.insert(i, np.random.random()) 
    plt.plot(x, y) 
    plt.savefig('plot_%d.png' % i)