2012-07-27 65 views
5

我正在處理來自多個天線基線的觀測數據。目前我的工作是繪製大約40個數字,每個數字都有4x5的子區域。使用matplotlib進行循環繪圖和保存時,我發現它很慢。這裏是我的代碼:如何在繪圖和保存大量數字時加速matplotlib?

import numpy as np 
    import matplotlib.pyplot as plt 
    import time 
    ... 

    PLT_PAGE_NUM = 39 # default is 39 
    SUB_PLT_NUM = 20 # default is 20 

    for pp in xrange(0,PLT_PAGE_NUM): 

     plt.figure(figsize=(20,12)) 

     start_time = time.clock() 
     for kk in xrange(0,SUB_PLT_NUM): 
      plt.subplot(5,4,kk+1) 
      plt.plot(np.arange(0,TIME_LENGTH), xcor_real_arr[20*pp+kk,0:],'r-', 
        range(0,TIME_LENGTH), xcor_imag_arr[20*pp+kk,0:],'b-') 
      plt.title('XCOR of '+ ind_arr[20*pp+kk], color='k') 

     plt.savefig('test_imag_real'+str(pp)+'.png',format='png',dpi=100) 
     print 'Fig-'+str(pp)+' has been saved' 
     print "Excution time:", time.clock()-start_time 

而且excution時間信息是:

######### Check your inputs setting ######### 
You have selected 2 files. 
The time interval is From 2011-10-20_14:28:38 to 2011-10-20_15:10:54 
Your time resolution is set to 1.125s 
The total plot points number is: 100 
Your frequency channel is: ch2 
######### Hardworking...please wait ######### 
Fig-0 has been saved 
Excution time: *2.52576639619* 
Fig-1 has been saved 
Excution time: *2.59867230708* 
Fig-2 has been saved 
Excution time: *2.81915188482* 
Fig-3 has been saved 
Excution time: *2.83102198991* 
Program ends 

正如你所看到的,我只積4個數字耗資約11秒。花費大約2分鐘來繪製並保存所有39個數字。我不知道瓶頸在哪裏。你能幫助它變得更快嗎? 謝謝!

回答

3

我已經修改了代碼,使其可運行:

import numpy as np 
import matplotlib.pyplot as plt 
import time 

PLT_PAGE_NUM = 39 # default is 39 
SUB_PLT_NUM = 20 # default is 20 
TIME_LENGTH = 1000 

xcor_real_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH)) 
xcor_imag_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH)) 
for pp in xrange(0,PLT_PAGE_NUM): 

    plt.figure(figsize=(20,12)) 

    start_time = time.time() 
    for kk in xrange(0,SUB_PLT_NUM): 
     plt.subplot(5,4,kk+1) 
     plt.plot(np.arange(0,TIME_LENGTH), xcor_real_arr[SUB_PLT_NUM*pp+kk,0:],'r-', 
       range(0,TIME_LENGTH), xcor_imag_arr[SUB_PLT_NUM*pp+kk,0:],'b-') 
     plt.title('XCOR of '+ str(SUB_PLT_NUM*pp+kk), color='k') 

    plt.savefig('test_imag_real'+str(pp)+'.png',format='png',dpi=100) 
    print 'Fig-'+str(pp)+' has been saved' 
    print "Excution time:", time.time()-start_time 

在我的機器,每個人物需要大約3秒:

Fig-0 has been saved 
Excution time: 3.01798415184 
Fig-1 has been saved 
Excution time: 3.08960294724 
Fig-2 has been saved 
Excution time: 2.9629740715 

使用的想法從Matplotlib Animations Cookbook(也證實通過Joe Kington,here),我們可以通過重複使用相同的軸並簡單地重新定義每個圖的y數據來加速大約33%(每個圖1秒):

import numpy as np 
import matplotlib.pyplot as plt 
import time 

PLT_PAGE_NUM = 39 # default is 39 
SUB_PLT_NUM = 20 # default is 20 
TIME_LENGTH = 1000 

xcor_real_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH)) 
xcor_imag_arr = np.random.random((SUB_PLT_NUM*PLT_PAGE_NUM,TIME_LENGTH)) 
plt.figure(figsize=(20,12)) 

ax = {} 
line1 = {} 
line2 = {} 

for pp in xrange(0,PLT_PAGE_NUM): 
    start_time = time.time() 
    for kk in xrange(0,SUB_PLT_NUM): 
     if pp == 0: 
      ax[kk] = plt.subplot(5,4,kk+1) 
      line1[kk], line2[kk] = ax[kk].plot(np.arange(0,TIME_LENGTH), 
            xcor_real_arr[SUB_PLT_NUM*pp+kk,0:],'r-', 
            range(0,TIME_LENGTH), 
            xcor_imag_arr[SUB_PLT_NUM*pp+kk,0:],'b-') 
     else: 
      line1[kk].set_ydata(xcor_real_arr[SUB_PLT_NUM*pp+kk,0:]) 
      line2[kk].set_ydata(xcor_imag_arr[SUB_PLT_NUM*pp+kk,0:]) 
     plt.title('XCOR of '+ str(SUB_PLT_NUM*pp+kk), color='k') 

    plt.savefig('test_imag_real'+str(pp)+'.png',format='png',dpi=100) 
    print 'Fig-'+str(pp)+' has been saved' 
    print "Excution time:", time.time()-start_time 

其產生這些執行次數:

Fig-0 has been saved 
Excution time: 3.0408449173 
Fig-1 has been saved 
Excution time: 2.05084013939 
Fig-2 has been saved 
Excution time: 2.01951694489 

(第一個圖仍然需要3秒的設置初始曲線。這是在後續的數字,我們可以節省一些時間。)

+0

unutbu,我試着運行你的代碼。令人驚訝的是,筆記本電腦上的總計時間縮短爲68.515999794秒。對我很有幫助,謝謝。重複使用(或「凍結」)同一個軸是一個很好的提示! – 2012-07-28 09:15:55

相關問題