2012-07-27 17 views
5

Estoy procesando datos de observación desde la línea base de muchas antenas. Actualmente, lo que estoy trabajando es trazar ~ 40 figuras, cada una de las cuales tiene una región de subparcela de 4x5. Lo encontré lento al trazar y guardar figuras con matplotlib en bucles. Aquí están mis códigos:¿Cómo acelerar el matplotlib al trazar y guardar muchas figuras?

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 

Y la información en tiempo excution es:

######### 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 

Como se ha visto, sólo Plot 4 cifras que cuestan aproximadamente 11 segundos. Toma ~ 2 minutos para trazar y guardar las 39 figuras. No sé dónde está el cuello de botella. ¿Puedes ayudar a hacerlo más rápido? Gracias!

Respuesta

3

He modificado el código para que sea ejecutable:

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 

En mi máquina, cada figura tarda 3 segundos:

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 

Uso de las ideas de la Matplotlib Animations Cookbook (y también demostró por Joe Kington, here), podemos acelerar esto en alrededor de 33% (1 segundo por la figura) mediante la reutilización de los mismos ejes y simplemente la redefinición de la ordenada en el de datos para cada trama:

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 

que produce estos tiempos de ejecución:

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 

(La primera figura todavía tarda 3 segundos para establecer las parcelas iniciales. Es en las figuras siguientes donde podemos ahorrar algo de tiempo.)

+0

unutbu, intenté ejecutar sus códigos. Es sorprendente que el tiempo total de ejecución en mi computadora portátil se acorte a 68.515999794 segundos. Una gran ayuda para mí, gracias. ¡Y reutilizar (o "congelar") los mismos ejes es una buena pista! –

Cuestiones relacionadas