4

I'm implementing the kmeans clustering algorithm in Python. I would like to plot at each iteration the status (image) of the clusters quality. So, basically I have a cycle which plot at each iteration an image and I want to animate this. I don't know if I made that clear. At the moment I just use the show() command which plot the image but then I have to close it in order to continue the iteration.

So, is there some way to animate the sequence of images computed at each step?

0

4 Answers 4

4

I tried the ion() method and it works fine for small amount of data, but if you have large images or images streaming in relatively quickly, this method is horrendously slow. From what I understand, ion() will redraw everything each time you make a change to your figure, including axes and labels, etc. Which might not be what you want.

This thread shows a much nicer way of doing things

Here's a simple example that I made showing how to do this:

import time
import numpy
import matplotlib.pyplot as plt


fig = plt.figure( 1 )
ax = fig.add_subplot( 111 )
ax.set_title("My Title")

im = ax.imshow( numpy.zeros( ( 256, 256, 3 ) ) ) # Blank starting image
fig.show()
im.axes.figure.canvas.draw()

tstart = time.time()
for a in xrange( 100 ):
  data = numpy.random.random( ( 256, 256, 3 ) ) # Random image to display
  ax.set_title( str( a ) )
  im.set_data( data )
  im.axes.figure.canvas.draw()

print ( 'FPS:', 100 / ( time.time() - tstart ) )

I get about 30 FPS on my machine with the above code. When I run the same thing with plt.ion() and ax.imshow( data ) instead of im.axes.figure.canvas.draw() and im.set_data( data ), I get around 1 FPS

Sign up to request clarification or add additional context in comments.

Comments

2

Use pause(). For a set of images stored in video[t, x, y], this makes a simple animation:

import matplotlib.pyplot as plt
for i in range(video.shape[0]):
    plt.imshow(video[i,:,:])
    plt.pause(0.5)

2 Comments

This only shows the last image of the loop in my case
Now it works! I had to add %matplotlib qt to make python plot into an external window. Also this answer is refreshingly compact
0

Just enable the interactive mode:

ion()
show()

And it will work. This is a little bit strange. But remember: At the end of a python script, it will close the windows. You have to call

ion()
show()

at the end of the script, if you don't want the windows to close.

Comments

0

I have implemented an image sequence visualization utility that may be helpful. Try it out here

Below is a example that draws a dynamic sine wave.

import numpy as np

def redraw_fn(f, axes):
    amp = float(f) / 3000
    f0 = 3
    t = np.arange(0.0, 1.0, 0.001)
    s = amp * np.sin(2 * np.pi * f0 * t)
    if not redraw_fn.initialized:
        redraw_fn.l, = axes.plot(t, s, lw=2, color='red')
        redraw_fn.initialized = True
    else:
        redraw_fn.l.set_ydata(s)

redraw_fn.initialized = False

num_time_steps = 100
videofig(num_time_steps, redraw_fn)

Comments

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.