FYI 本初稿完成於2017年,內容更新於
個人網站 - 基於 interactive mode 實現 matplotlib 動態更新圖片(交互式繪圖),請移步閱讀最新內容。
最近在研究動態障礙物避障算法,在Python語言進行算法仿真時需要實時顯示障礙物和運動物的當前位置和軌跡,利用Anaconda的Python打包集合,在Spyder中使用Python3.5語言和matplotlib實現路徑的動態顯示和交互式繪圖(和Matlab功能類似)。
Anaconda是一個用於科學計算的Python發行版,支持 Linux, Mac, Windows系統,提供了包管理與環境管理的功能,可以很方便地解決多版本python並存、切換以及各種第三方包安裝問題。Anaconda利用工具/命令conda來進行package和environment的管理,並且已經包含了Python和相關的配套工具。Anaconda官方地址:https://www.continuum.io/downloads/
matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合交互式地進行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應用程序中。其中,matplotlib的pyplot子庫提供了和matlab類似的繪圖API,方便用戶快速繪制2D圖表,它的文檔相當完備,並且 Gallery頁面中有上百幅縮略圖,打開之后都有源程序。matplotlib官方地址:http://matplotlib.org/
在調研matplotlib動態繪制曲線方法中,和matlab相似有animation方法和交互式繪圖,但是animation方法靈活性不高,不太適合路徑的實時動態顯示,本文最后采用交互式繪圖模(interactive mode)。具體參見
http://matplotlib.org/users/shell.html。
The interactive property of the pyplot interface controls whether a figure canvas is drawn on every pyplot command. If interactive is False, then the figure state is updated on every plot command, but will only be drawn on explicit calls to
draw(). When interactive is True, then every pyplot command triggers a draw.
當繪圖語句中加入pl.ion()時,表示打開了交互模式。此時python解釋器解釋完所有命令后,給你出張圖,但不會結束會話,而是等着你跟他交流交流。如果你繼續往代碼中加入語句,run之后,你會實時看到圖形的改變。當繪圖語句中加入pl.ioff()時或不添加pl.ion()時,表示打關了交互模式。此時要在代碼末尾加入pl.show()才能顯示圖片。python解釋器解釋完所有命令后,給你出張圖,同時結束會話。如果你繼續往代碼中加入語句,再不會起作用,除非你關閉當前圖片,重新run。
采用交互式繪圖模式后,可以方便地繪出障礙物的運動軌跡和當前位置,深切感覺matplotlib和matlab很類似,基本matlab的功能都可以在matplotlib中找到,所以matlab中的代碼也可以很快移植到python中!
代碼示例:
# -*- coding: utf-8 -*- """ Created on Sat Mar 25 23:28:29 2017 original link: https://www.yanlongwang.net/Python/python-interactive-mode/ @author: wyl """ import matplotlib.pyplot as plt from matplotlib.patches import Circle import numpy as np import math plt.close() #clf() # 清圖 cla() # 清坐標軸 close() # 關窗口 fig=plt.figure() ax=fig.add_subplot(1,1,1) ax.axis("equal") #設置圖像顯示的時候XY軸比例 plt.grid(True) #添加網格 plt.ion() #interactive mode on IniObsX=0000 IniObsY=4000 IniObsAngle=135 IniObsSpeed=10*math.sqrt(2) #米/秒 print('開始仿真') try: for t in range(180): #障礙物船只軌跡 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='.') #散點圖 #ax.lines.pop(1) 刪除軌跡 #下面的圖,兩船的距離 plt.pause(0.001) except Exception as err: print(err)