7

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nazmul
  • 383
  • 3
  • 6
  • 17

1 Answers1

15

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)
keineahnung2345
  • 2,635
  • 4
  • 13
  • 28
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
  • There is no need to assign the return of `raw_input` to a variable called `_`. You can just do `raw_input("blah")` by itself on a line if you're not going to use the value. – BrenBarn Jun 21 '12 at 01:28
  • 1
    Yes im quite aware of that, this is why it's '_' the reason I did it, it's so when you copy and paste on the terminal it won't print what ever raw_input returns, then again most of the plt commands return objects ... so it's up to the user, either way I've translated his original matlab code, if I understood it correctly ... – Samy Vilar Jun 21 '12 at 02:24