0
嗨我無法讓matplotlib動畫起作用 這個想法是在幾個列表和圖像上生成動畫。 a [0]基本上是時間點/圖像繪製/ imshow 「時間點[a [0] .astype(np.int)] [:i]/60」僅僅是索引的「實時」 x軸讓matplotlib動畫起作用
任何人都可以給我一些提示嗎?
讚賞
def draw_plot(resolution,timepoints,a,ts_g,ts_r,contrast):
def animate(i):
#~ ax.cla()
#~ ax2.cla()
#~ ax3.cla()
#~ ax4.cla()
print "ax"
ax.errorbar(timepoints[a[0].astype(np.int)][:i]/60,list(a[1][0])[:i], yerr=list(a[4][0])[:i], fmt='-o',color='green')
ax.errorbar(timepoints[a[0].astype(np.int)][:i]/60,list(a[1][1])[:i], yerr=list(a[4][1])[:i], fmt='-o',color='red')
ax.axis([0,timepoints[a[0].astype(np.int)][-1]/60,np.min(np.concatenate((a[1][0],a[1][1])))*0.75,np.max(np.concatenate((a[1][0],a[1][1])))*1.3])
ax.set_autoscale_on(False)
print "ax2"
ax2.plot(timepoints[a[0].astype(np.int)][:i]/60,list(a[2][0])[:i],color='green')
ax2.plot(timepoints[a[0].astype(np.int)][:i]/60,list(a[2][1])[:i],color='red')
ax2.axis([0,timepoints[a[0].astype(np.int)][-1]/60,np.min(np.concatenate((a[2][0],a[2][1])))*0.75,np.max(np.concatenate((a[2][0],a[2][1])))*1.3])
ax2.set_autoscale_on(False)
j = int(a[0][i]-1)
r = ts_r[j]#.T
g = ts_g[j]#.T
max_ = contrast[3]
min_ = contrast[2] #~
r[r>max_]=max_
r[r<min_]=min_
r -= min_
r *= _16bit/(max_-min_)#r.max()
max_ = contrast[1]
min_ = contrast[0]
g[g>max_]=max_
g[g<min_]=min_
g -= min_
g *= _16bit/(max_-min_)#r.max()
#~
g_16 = g
r = (r*ratio).astype(np.uint8)
g = (g*ratio).astype(np.uint8)
b = np.zeros(r.shape).astype(np.uint8)
centered = np.dstack((r,g,b)).astype(np.uint8)
#~ aa = np.dstack((np.zeros(ts[0].shape).T,ts[j].T,np.zeros(ts[0].shape)))
print "ax3"
ax3.imshow(centered)
ax3.plot(list(a[5][0]/resolution)[:i],list(512-a[5][1]/resolution)[:i],color='blue')
ax3.axis([0,512,512,0])
print "ax4"
ax4.imshow(centered)
x = int (list(a[5][0]/resolution)[i])
y = int (list(512-a[5][1]/resolution)[i])
#~ ax4.axis([512,0,512,0])
ax4.axis([x-10,x+10,y-10,y+10])
ax4.get_xaxis().set_visible(False)
ax4.get_yaxis().set_visible(False)
print "ax5"
ax5.imshow(g_16,cmap='gray')
#~ x = int (list(a[5][0]/resolution)[i])
#~ y = int (list(512-a[5][1]/resolution)[i])
#~ ax4.axis([512,0,512,0])
ax5.axis([x-10,x+10,y-10,y+10])
ax5.get_xaxis().set_visible(False)
ax5.get_yaxis().set_visible(False)
plt.draw()
fig = plt.figure()
ax = plt.subplot2grid((2,5), (0,0),colspan=2)
ax2 = plt.subplot2grid((2,5), (1,0),colspan=2)
ax3 = plt.subplot2grid((2,5), (0, 2), colspan=2,rowspan=2)
ax4 = plt.subplot2grid((2,5), (0,4))
ax5 = plt.subplot2grid((2,5), (1, 4))
line, = ax.plot([], [], lw=2)
line2, = ax2.plot([], [], lw=2)
ani = animation.FuncAnimation(fig, animate, frames= len(a[0]), interval=20000,repeat=False,blit=True)
plt.show()
如果您花時間簡化一點代碼,提供代表模型的最短可執行代碼,這將幫助我們檢查它。 – joaquin 2014-09-21 11:11:38
你應該從這裏開始:http://matplotlib.org/examples/animation/simple_anim.html你需要一個init函數。另外,一般來說,我不認爲你應該每次都在繪製動畫功能;你應該調用該圖的set_data方法並簡單地改變數據。否則它會很慢(我甚至不知道它是否會起作用)。 – 2014-09-22 08:37:14
我修好了。我小到大。唯一不能正確的是ax.errorbar的垂直條。我用lineerr(底部,頂部),vers = ax.errorbar(etcetcetcetc)來稱呼它。我能夠lineerr.setdata(list1,list2)bottoms.setdata(list1,list2)但我無法顯示豎線。 :( – 2014-09-22 21:07:19