Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:08:26+00:00 2026-06-08T16:08:26+00:00

I am processing observing data from many antenna baseline. Currently what I am working

  • 0

I am processing observing data from many antenna baseline. Currently what I am working is to plot ~ 40 figures, each of which has 4×5 subplot region. I found it slow when plotting and saving figures with matplotlib in loops. Here are my codes:

    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

And the excution time info is:

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

As you have seen, I just plot 4 figures which cost about 11 seconds. It takes ~2 minutes to plot and save all 39 figures. I dont’ know where the bottleneck is. May you help to make it faster?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:08:27+00:00Added an answer on June 8, 2026 at 4:08 pm

    I’ve modified your code to make it runnable:

    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
    

    On my machine, each figure takes about 3 seconds:

    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
    

    Using ideas from the Matplotlib Animations Cookbook (and also demonstrated by Joe Kington, here), we can speed this up by about 33% (1 second per figure) by reusing the same axes and simply redefining the y-data for each plot:

    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
    

    which yields these execution times:

    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
    

    (The first figure still takes 3 seconds to set up the initial plots. It is on subsequent figures where we can save some time.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im working on processing data. I setup a worker function thats called every 5ms
Processing has Android support and it seems to be pretty awesome from my 10
I'm working on processing a URL in to it's constituent parts from my stand
After processing form from POST I should redirect, to prevent user from hitting back.
I'm processing some 3rd party HTML which is semi-structured marked-up text (bold, italics, etc).
I am processing a flat file, with data in line by line format, like
I'm processing a large amount of : splitted data with Python. And I'm having
AudioPannerNode is a processing node which positions / spatializes an incoming audio stream in
Processing has a great function I use all the time: map(value, low1, high1, low2,
after processing my first steps in working with XML in java I am now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.