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 7791321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:51:02+00:00 2026-06-01T21:51:02+00:00

I want to plot a sequence of .png images in matplotlib. The goal is

  • 0

I want to plot a sequence of .png images in matplotlib. The goal is to plot them rapidly to simulate the effect of a movie, but I have additional reasons for wanting to avoid actually creating an .avi file or saving matplotlib figures and then viewing them in sequence outside of Python.

I’m specifically trying to view the image files in sequence inside a for-loop in Python. Assuming I have imported matplotlib correctly, and I have my own functions ‘new_image()’ and ‘new_rect()’, here’s some example code that fails to work because of the blocking effect of the show() function’s call to the GUI mainloop:

 for index in index_list:
     img = new_image(index)
     rect = new_rect(index)

     plt.imshow(img)
     plt.gca().add_patch(rect)
     plt.show()

     #I also tried pausing briefly and then closing, but this doesn't
     #get executed due to the GUI mainloop from show()
     time.sleep(0.25)
     plt.close()

The above code works to show only the first image, but then the program just hangs and waits for me to manually close the resultant figure window. Once I do close it, the program then just hangs and doesn’t re-plot with the new image data. What should I be doing? Also note that I have tried replacing the plt.show() command with a plt.draw() command, and then adding the plt.show() outside of the for-loop. This doesn’t display anything and just hangs.

  • 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-01T21:51:04+00:00Added an answer on June 1, 2026 at 9:51 pm

    The best way I have found for this was with the command pylab.ion() after you import pylab.

    Here is a script that does use show(), but which displays the different plots each time pylab.draw() is called, and which leaves the plot windows showing indefinitely. It uses simple input logic to decide when to close the figures (because using show() means pylab won’t process clicks on the windows x button), but that should be simple to add to your gui as another button or as a text field.

    import numpy as np
    import pylab
    pylab.ion()
    
    def get_fig(fig_num, some_data, some_labels):
    
        fig = pylab.figure(fig_num,figsize=(8,8),frameon=False)
        ax = fig.add_subplot(111)
        ax.set_ylim([0.1,0.8]); ax.set_xlim([0.1, 0.8]);
        ax.set_title("Quarterly Stapler Thefts")
        ax.pie(some_data, labels=some_labels, autopct='%1.1f%%', shadow=True);
        return fig
    
    my_labels = ("You", "Me", "Some guy", "Bob")
    
    # To ensure first plot is always made.
    do_plot = 1; num_plots = 0;
    
    while do_plot:
        num_plots = num_plots + 1;
        data = np.random.rand(1,4).tolist()[0]
    
        fig = get_fig(num_plots,data,my_labels)
        fig.canvas.draw()
        pylab.draw()
    
        print "Close any of the previous plots? If yes, enter its number, otherwise enter 0..."
        close_plot = raw_input()
    
        if int(close_plot) > 0:
            pylab.close(int(close_plot))
    
        print "Create another random plot? 1 for yes; 0 for no."
        do_plot = raw_input();
    
        # Don't allow plots to go over 10.
        if num_plots > 10:
            do_plot = 0
    
    pylab.show()
    

    By modifying the basic logic here, I can have it close windows and plot images consecutively to simulate playing a movie, or I can maintain keyboard control over how it steps through the movie.

    Note: This has worked for me across platforms and seems strictly superior to the window canvas manager approach above, and doesn’t require the ‘TkAgg’ option.

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

Sidebar

Related Questions

I want to plot some data. The first column contains the x-data. But matplotlib
I have a bunch of measurements over time and I want to plot them
I want to plot some confidence interval graphs in MATLAB but I don't have
I want to simulate values that represent a geometric distribution. The plot I have
I have many 3D vectors. I want to plot them in a cube so
I have a data which I want to plot in cumulative function. The R
With this approach. I have a line plot graph. I want to plot 'two'
I have a vectors of strings, say c(E^A,S^P,lambda,T,E^Q,E^Q,AT) , and I want to plot
I have 6 vectors which I want to plot. How I can make each
I have data that looks like this. In which I want to plot accumulative

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.