關(guān)于Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線的方法改進(jìn)指南
很多時(shí)候,我們需要實(shí)時(shí)的繪制曲線,如實(shí)時(shí)的繪制串口接收到的數(shù)據(jù)。最先想到的解決策略是類似于Matlab種的drawnow函數(shù)。
在python中Matplotlib庫(kù)有著和Matlan繪圖庫(kù)相似的功能,但是并沒有drawnow這樣的函數(shù)。
已有的解決方案
通過網(wǎng)上現(xiàn)有的資料 基于Python實(shí)現(xiàn)matplotlib中動(dòng)態(tài)更新圖片(交互式繪圖) ,可以通過打開Matplotlib的交互模式來(lái)實(shí)現(xiàn)實(shí)時(shí)繪圖的目的,此時(shí)需要用到函數(shù)matplotlib.pyplot.ion
存在的問題
通過上述方法實(shí)時(shí)繪圖,存在一個(gè)嚴(yán)重的問題:隨著時(shí)間推移,CPU消耗越大,費(fèi)時(shí)越多,最終導(dǎo)致程序卡頓。這顯然無(wú)法滿足我們實(shí)時(shí)繪圖的要求。
以下通過time模塊計(jì)算每一步的耗時(shí),直觀地表現(xiàn)這一現(xiàn)象。
def Method(point): es_time = np.zeros([point]) fig=plt.figure() ax=fig.add_subplot(1,1,1) ax.axis("equal") #設(shè)置圖像顯示的時(shí)候XY軸比例 ax.set_xlabel('Horizontal Position') ax.set_ylabel('Vertical Position') ax.set_title('Vessel trajectory') plt.grid(True) #添加網(wǎng)格 plt.ion() #interactive mode on IniObsX=0000 IniObsY=4000 IniObsAngle=135 IniObsSpeed=10*math.sqrt(2)#米/秒 print('開始仿真') for t in range(point): t0 = time.time() #障礙物船只軌跡 obsX=IniObsX+IniObsSpeed*math.sin(IniObsAngle/180*math.pi)*t obsY=IniObsY+IniObsSpeed*math.cos(IniObsAngle/180*math.pi)*t ax.scatter(obsX,obsY,c='b',marker='.') #散點(diǎn)圖 #下面的圖,兩船的距離 plt.pause(0.001) es_time[t] = 1000*(time.time() - t0) return es_time
耗時(shí)結(jié)果
Method
很顯然每步繪圖時(shí)間與繪圖點(diǎn)數(shù)呈線性相關(guān)的趨勢(shì),且隨著點(diǎn)數(shù)增加,時(shí)間消耗越多。可以想象,當(dāng)繪圖的點(diǎn)數(shù)到達(dá)上萬(wàn)乃至億的時(shí)候,那電腦就卡住了。
分析原因
個(gè)人猜測(cè)出現(xiàn)上述這種現(xiàn)象的原因,是由代碼ax.scatter(obsX,obsY,c='b',marker='.')造成的。這段代碼每一循環(huán)一次就新畫一條曲線,而不清除之前的曲線,這就必然導(dǎo)致越往后循環(huán)所花費(fèi)的CPU資源內(nèi)存資源越多,最終機(jī)器卡死。
改進(jìn)方法
既然原因是因?yàn)椴粩嘀貜?fù)畫圖所致,導(dǎo)致機(jī)器資源的累積消耗,所以想到的第一個(gè)解決方法,那就是每次畫圖前,清除之前的曲線。
根據(jù)上述思想,在每一次的畫圖代碼ax.scatter(obsX,obsY,c='b',marker='.')前加上清除代碼plt.cla()。即:
plt.cla() ax.plot(obsX,obsY,'-g',marker='*') #散點(diǎn)圖
可是這樣做之后就會(huì)存在新的問題:之前定義的坐標(biāo)軸,標(biāo)題,圖例等等信息就都被清除了。解決方法則,需要在每一步的循環(huán)中,重新定義這些信息。
完整代碼
def Method_Improve(point): def initial(ax): ax.axis("equal") #設(shè)置圖像顯示的時(shí)候XY軸比例 ax.set_xlabel('Horizontal Position') ax.set_ylabel('Vertical Position') ax.set_title('Vessel trajectory') plt.grid(True) #添加網(wǎng)格 return ax es_time = np.zeros([point]) fig=plt.figure() ax=fig.add_subplot(1,1,1) ax = initial(ax) plt.ion() #interactive mode on IniObsX=0000 IniObsY=4000 IniObsAngle=135 IniObsSpeed=10*math.sqrt(2)#米/秒 print('開始仿真') obsX = [0,] obsY = [4000,] for t in range(point): t0 = time.time() #障礙物船只軌跡 obsX.append(IniObsX+IniObsSpeed*math.sin(IniObsAngle/180*math.pi)*t) obsY.append(IniObsY+IniObsSpeed*math.cos(IniObsAngle/180*math.pi)*t) plt.cla() ax = initial(ax) ax.plot(obsX,obsY,'-g',marker='*') #散點(diǎn)圖 #下面的圖,兩船的距離 plt.pause(0.001) es_time[t] = 1000*(time.time() - t0) return es_time
耗時(shí)結(jié)果
Method_Improve
顯然循環(huán)次數(shù)與耗時(shí)不再呈正相關(guān)趨勢(shì),可以說是在一定誤差范圍內(nèi),耗時(shí)保持穩(wěn)定。
改進(jìn)方法的改進(jìn)
改進(jìn)方法中仍存在一個(gè)問題:由于每次循環(huán)都需要清除坐標(biāo)軸信息,那么每次循環(huán)也必須再重新設(shè)置坐標(biāo)軸信息。顯然這種做法,導(dǎo)致了額外的算力消耗,那能否有新的方法,規(guī)避這種問題呢?答案顯然是有的。
但是解決思路還是得從原始問題出發(fā),即重復(fù)畫圖,導(dǎo)致資源的累積消耗。所以令一種新的思路:只畫一條(需要數(shù)量的)曲線,每次循環(huán)更改這些曲線的數(shù)據(jù)。
那么按照上述思路之后,只需程序開頭定義好坐標(biāo)軸信息,而不需要每次循環(huán)內(nèi)清除重設(shè)坐標(biāo)軸信息。
具體做法,就是獲取曲線的句柄,進(jìn)行修改,即有:
line.set_xdata(obsX) line.set_ydata(obsY)
完整代碼:
def ImprovedMethod_Improve(point): es_time = np.zeros([point]) fig=plt.figure() ax=fig.add_subplot(1,1,1) ax.set_xlabel('Horizontal Position') ax.set_ylabel('Vertical Position') ax.set_title('Vessel trajectory') line = ax.plot([0,0],[4000,4000],'-g',marker='*')[0] plt.grid(True) #添加網(wǎng)格 plt.ion() #interactive mode on IniObsX=0000 IniObsY=4000 IniObsAngle=135 IniObsSpeed=10*math.sqrt(2)#米/秒 print('開始仿真') obsX = [0,] obsY = [4000,] for t in range(point): t0 = time.time() #障礙物船只軌跡 obsX.append(IniObsX+IniObsSpeed*math.sin(IniObsAngle/180*math.pi)*t) obsY.append(IniObsY+IniObsSpeed*math.cos(IniObsAngle/180*math.pi)*t) line.set_xdata(obsX) line.set_ydata(obsY) ax.set_xlim([-200,10*point+200]) ax.set_ylim([3800-10*point,4200]) #下面的圖,兩船的距離 plt.pause(0.001) es_time[t] = 1000*(time.time() - t0) return es_time
三種方法對(duì)比
總結(jié)
到此這篇關(guān)于Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線的文章就介紹到這了,更多相關(guān)Matplotlib繪制動(dòng)態(tài)實(shí)時(shí)曲線內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。