17

I have a numpy array whose elements are updated in a for loop:

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

for t in range(0,10):
    imshow(a)

    for i in range(0,a.shape[0]):
        for j in range(0,a.shape[1]):
            a[i][j] += 1

I want to display the array at each iteration, but imshow() doesn't work, it just displays the image once the loop terminates.

ps. I'm using an Ipython notebook

I found different things on the web but none of them work on my computer (for example I tried to use matplotlib's animation module)

The strange thing is that if I try to execute this example (http://matplotlib.org/examples/animation/dynamic_image2.html) using the standard python prompt everything works fine, while on the Ipython notebook it doesn't work. Can anyone explain me why?

notes:

Maybe I oversimplified my code;

I'm working on a forest-fire model, the array is a grid filled with 0 = empty site, 1 = tree, 2 = fire.

At each time step (iteration):

  1. a tree is dropped on a randomly chosen site and if the site is free the tree is planted
  2. a tree ignites with a probability f

I want to display the array using a colormap to visualize the evolution of my model

2
  • Just a side note, why don't you do something like a += np.ones(a.shape)? Commented Sep 12, 2014 at 16:39
  • 1
    Yes, I can do that, but I wrote down this array and this for loop just to explain my problem. This isn't the actual code I'm working on :) Commented Sep 12, 2014 at 16:44

2 Answers 2

28

imshow(a) will plot the values of the array a as pixel values, but it won't display the plot. To view the image after each iteration of the for loop, you need to add show().

This should do what you want:

from matplotlib.pyplot import imshow, show    

a = np.array([[1,2,3],[4,5,6],[7,8,9]])

for t in range(0,10):
    imshow(a)
    show()

    for i in range(0,a.shape[0]):
        for j in range(0,a.shape[1]):
            a[i][j] += 1
1
  • 4
    Required import statements: from matplotlib.pyplot import imshow, show Commented Feb 4, 2018 at 15:33
1

For me just using show() doesn't always work, and when it does, things tend to get very slow over time. To handle these problems, I import display and use its display() and clear_output() methods.

import numpy as np
import matplotlib.pyplot as plt
from IPython import display
import time

pause_time = 0.2  # seconds between frames
a = np.random.rand(3,3)

for t in range(0,10):
    plt.imshow(a)
    plt.title(t)
    display.display(plt.gcf())
    display.clear_output(wait=True)
    time.sleep(pause_time)

    a = np.random.rand(3,3)

The above is adapted from this answer. The time module is used just to let you pause the display and control the frame rate to the level you want: it is optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.