2012-07-27 9 views
5

多くのアンテナベースラインからデータを観測しています。私が現在取り組んでいるのは、それぞれが4x5サブプロット領域を持つ〜40の図をプロットすることです。 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 

あなたが見てきたように、私はちょうど約11秒を要した4つの数字をプロットします。 39の図形すべてをプロットして保存するのに約2分かかります。私はボトルネックがどこにあるのかわかりません。早くするのを手伝ってもらえますか? ありがとうございます!

答えて

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秒に短縮されたことは素晴らしいことです。おかげさまで大変助かりました。同じ軸を再利用する(またはフリーズする)ことは良いヒントです! –

関連する問題