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

  • SEARCH
  • Home
  • 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 8114247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:05:42+00:00 2026-06-06T03:05:42+00:00

I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am

  • 0

I am new to Matplotlib and Python. I mostly use Matlab. Currently, I am working with a Python code where I want to run a loop. In each loop, I will do some data processing and then show an image based on the processed data. When I go to the next loop, I want the previously stored image to be closed and generate a new image based on the latest data.

In other words, I want a python code equivalent to the following Matlab code:

x = [1 2 3];

for loop = 1:3

    close all;

    y = loop * x;

    figure(1);

    plot(x,y)

    pause(2)

end

I tried the following python code to achieve my goal:

import numpy as np
import matplotlib
import matplotlib.lib as plt

from array import array
from time import sleep

if __name__ == '__main__':

    x = [1, 2, 3]

    for loop in range(0,3):

        y = numpy.dot(x,loop)

        plt.plot(x,y)

       plt.waitforbuttonpress

    plt.show()

This code puts all plots superimposed in the same figure. If I put the plt.show() command inside the for loop, only the first image is shown. Therefore, I could not replicate my Matlab code in Python.

  • 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-06T03:05:43+00:00Added an answer on June 6, 2026 at 3:05 am

    try this:

    import numpy
    from matplotlib import pyplot as plt
    
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion() # turn on interactive mode
        for loop in range(0,3):
            y = numpy.dot(x, loop)
            plt.figure()
            plt.plot(x,y)
            plt.show()
            _ = input("Press [enter] to continue.")
    

    if you want to close the previous plot, before showing the next one:

    import numpy
    from matplotlib import pyplot as plt
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion() # turn on interactive mode, non-blocking `show`
        for loop in range(0,3):
            y = numpy.dot(x, loop)
            plt.figure()   # create a new figure
            plt.plot(x,y)  # plot the figure
            plt.show()     # show the figure, non-blocking
            _ = input("Press [enter] to continue.") # wait for input from the user
            plt.close()    # close the figure to show the next one.
    

    plt.ion() turns on interactive mode making plt.show non-blocking.

    and heres is a duplicate of your matlab code:

    import numpy
    import time
    from matplotlib import pyplot as plt
    
    if __name__ == '__main__':
        x = [1, 2, 3]
        plt.ion()
        for loop in xrange(1, 4):
            y = numpy.dot(loop, x)
            plt.close()
            plt.figure()
            plt.plot(x,y)
            plt.draw()
            time.sleep(2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to StackOverflow here. I'm working on the first Euler problem and have run
I'm very new to python. two days. trying to get a plot working with
I am trying to use networkx with Python. When I run this program it
I am new user of matplotlib, my platform is Ubuntu 10.04 Python 2.6.5 This
First off, I'm new to python and matplotlib. I need to plot several systems
I'm very new to python and matplotlib and having trouble with adjusting the height
New to PHP, would love to get to know it better. Currently working on
I have an image and want to get somehow a new image which will
I am trying to adapt the underlying structure of plotting code (matplotlib) that is
I am new to matplotlib and I am trying to figure out if I

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.