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.

4

1 回答 1

2

使用该matplotlib库,很容易获得工作动画。下面是一个可以使用的最小示例。该功能generate_data可以根据您的需要进行调整:

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()

在此处输入图像描述

此示例改编自先前的答案并修改为显示线图而不是颜色图。

于 2012-12-05T14:53:52.267 回答