1. 程式人生 > 其它 >基於Python實現matplotlib中動態更新圖片(互動式繪圖)

基於Python實現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

@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)