3

I'm writing some python functions that deals with manipulating sets of 2D/3D coordinates, mostly 2D.

The issue is debugging such code is made difficult just by looking at the points. So I'm looking for some software that could display the points and showing which points have been added/removed after each step. Basically, I'm looking for a turn my algorithm into an animation.

I've seen a few applets online that do things similar what I was looking for, but I lack the graphics/GUI programming skills to write something similar at this point, and I'm not sure it's wise to email the authors of things whose last modified timestamps reads several years ago. I should note I'm not against learning some graphics/GUI programming in the process, but I'd rather not spend more than 1-3 days if it can't be helped, although such links are still appreciated. In this sense, linking to a how-to site for writing such a step-by-step program might be acceptable.

Nuclearman
  • 5,029
  • 1
  • 19
  • 35
  • Likely the best thing to do is save a table of points at each step then plot them in something like http://www.gnuplot.info. – Daniel Dec 05 '12 at 14:37

1 Answers1

2

With the matplotlib library it is very easy to get working animations up. Below is a minimal example to work with. The function generate_data can be adapted to your needs:

import numpy as np
import matplotlib.pyplot as plt 
import matplotlib.animation as animation

def generate_data():
    X = np.arange(25)
    Y = X**2 * np.random.rand(25)
    return X,Y 

def update(data):
    mat[0].set_xdata(data[0])
    mat[0].set_ydata(data[1])
    return mat 

def data_gen():
    while True:
        yield generate_data()

fig, ax = plt.subplots()
X,Y = generate_data()
mat = ax.plot(X,Y,'o')
ani = animation.FuncAnimation(fig, update, data_gen, interval=500,
                              save_count=10)

ani.save('animation.mp4')
plt.show()

enter image description here

This example was adapted from a previous answer and modified to show a line plot instead of a colormap.

Community
  • 1
  • 1
Hooked
  • 84,485
  • 43
  • 192
  • 261
  • Huh, seems I somehow overlooked the animation aspect of the matplotlib API. Seems I'll be reviewing that for a while. I was plotting them before, but dealing with 20+ popup windows is a pain. – Nuclearman Dec 06 '12 at 05:33
  • @MC It _used_ to be a huge pain to do animations in matplotlib. It's still not as nice as it could be, but the newer versions of matplotlib have a much nicer API – Hooked Dec 06 '12 at 15:12