Python學習-使用matplotlib畫動態多圖


最近常常使用matplotlib進行數學函數圖的繪制,可是怎樣使用matplotlib繪制動態圖,以及繪制動態多圖。直到今天才學會。

1.參考文字 

首先感謝幾篇文字的作者。幫我學會了怎樣繪制。大家也能夠參考他們的文字。

  1. http://blog.csdn.net/rumswell/article/details/11731003:文字作者給出了數個演示樣例的源代碼。可是沒有非常具體的解說。源代碼面前無秘密。自己看吧。

  2. http://mytrix.me/2013/08/matplotlib-animation-tutorial/:這位作者的解說非常具體,主要講了matplotlib官方演示樣例。大家能夠參閱。


  3. http://blog.yangyu.me/2014/08/06/matplotlib-graphing-series/:這位作者,給出了不同的演示樣例,並且非常具體,告訴了大家怎樣一步步學習Matplotlib畫圖
  4. http://sebug.net/paper/books/scipydoc/matplotlib_intro.html#id4:用Python做科學計算,非常好非常詳實的書。

2.程序源代碼

先貼出程序源代碼,在一步步做解釋。

<span style="font-family:SimSun;font-size:10px;">import numpy as np   
from matplotlib import pyplot as plt   
from matplotlib import animation   
  
# first set up the figure, the axis, and the plot element we want to animate   
fig = plt.figure() 
ax1 = fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))
ax2 = fig.add_subplot(2,1,2,xlim=(0, 2), ylim=(-4, 4))
line, = ax1.plot([], [], lw=2)  
line2, = ax2.plot([], [], lw=2)  
def init():  
    line.set_data([], [])  
    line2.set_data([], [])  
    return line,line2

# animation function.  this is called sequentially   
def animate(i):

    x = np.linspace(0, 2, 100)   
    y = np.sin(2 * np.pi * (x - 0.01 * i))  
    line.set_data(x, y)      


    x2 = np.linspace(0, 2, 100)   
    y2 = np.cos(2 * np.pi * (x2 - 0.01 * i))* np.sin(2 * np.pi * (x - 0.01 * i))  
    line2.set_data(x2, y2)   
    return line,line2

anim1=animation.FuncAnimation(fig, animate, init_func=init,  frames=50, interval=10)  
plt.show()  </span>

3.解釋

如今就來解釋下。這個程序我到底干了啥

3.1建立子圖、空白線

fig = plt.figure() 
ax1 = fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))
ax2 = fig.add_subplot(2,1,2,xlim=(0, 2), ylim=(-4, 4))
line, = ax1.plot([], [], lw=2)  
line2, = ax2.plot([], [], lw=2)  
在上面的程序能夠看到,先建立了一個figure對象。之后fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))就是建立子圖,關於子圖的概念和做法,大家能夠參閱下文字【4】“用Python做科學計算” 關於子圖的介紹。

3.2創建動畫發生時調用的函數

Init()是我們的動畫在在創建動畫基礎框架(base frame)時調用的函數。這里我們們用一個非常easy的對line什么都不做的函數。這個函數一定要返回line對象,這個非常重要。由於這樣就能告訴動畫之后要更新的內容,也就是動作的內容是line。--來自( http://mytrix.me/2013/08/matplotlib-animation-tutorial/
上面的這段話,解釋了Init()這個函數是干嘛的,由於我的程序比較特殊,希望可以在一張圖中顯示兩個子圖,如圖3.1。所以我必須在兩個坐標軸ax1和ax2中創建兩個空白的線line,line2且在Init()中返回這兩個Line。


圖3.1

def init():  
    line.set_data([], [])  
    line2.set_data([], [])  
    return line,line2

3.3動畫函數

接下來你須要一個動畫函數。在這個動畫函數中改動你的圖。相同的我須要一張圖中顯示兩個東西,所以在動畫函數中,我更新了兩個圖,且返回了line和line2
def animate(i):

    x = np.linspace(0, 2, 100)   
    y = np.sin(2 * np.pi * (x - 0.01 * i))  
    line.set_data(x, y)      


    x2 = np.linspace(0, 2, 100)   
    y2 = np.cos(2 * np.pi * (x2 - 0.01 * i))* np.sin(2 * np.pi * (x - 0.01 * i))  
    line2.set_data(x2, y2)   
    return line,line2

3.4顯示動畫

最后你須要用例如以下的兩個語句來顯示動畫,這里有個注意的地方。須要調整interval參數(這個參數指明了時間間隔)的大小,不然會出現如圖3.2一樣的情況(當你使用了,blit=True這個選項)。
同一時候http://mytrix.me/2013/08/matplotlib-animation-tutorial/)給我們說明了幾個參數的作用,我在不在復述:

這個對象須要持續存在,全部我們要將它賦給一個變量。我們選擇了一個100幀的動畫(譯者注:你上邊的代碼還是200幀,怎么到這兒就變成100幀了……。另外。這里也不一定一定要是個數字,能夠是個generator 或iterable,詳見API說明)而且幀與幀之間間隔20ms,blit是一個很重要的keyword。它告訴動畫僅僅重繪改動的部分。結合上面保存的時間, blit=true會使動畫顯示得會很很快。


圖3.2

anim1=animation.FuncAnimation(fig, animate, init_func=init,  frames=50, interval=10)  
plt.show()  

3.5結束

上面的工作解釋完了,來看看成果。程序寫的不好。我也是才初學。希望看到博客的人。能多多給我不吝賜教,不勝感激。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM