使用PySide按钮重置Matplotlib动画

时间:2014-07-11 15:46:19

标签: python animation matplotlib pyside

我想创建一个按钮,每当我点击它时,它将重置并执行相同的动画。看看我在下面尝试过的。

以下是完整的代码并注释:

import sys
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib import pyplot
import numpy as np
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PySide import QtCore, QtGui
from matplotlib import animation

app = QtGui.QApplication(sys.argv) 

#movements data
moves_x = [12, 14, 13, 15, 13, 14, 16, 18, 16, 18, 17, 19, 21, 23, 24, 24, 22, 23, 24, 24, 24, 22, 23, 24, 24, 24, 24, 24, 24, 24, 24, 22, 23, 24, 24, 23, 23, 21, 20, 21, 19, 20, 21, 20, 22, 23, 21, 22, 22, 20, 19]
moves_y = [12, 11, 13, 14, 14, 16, 16, 17, 17, 16, 17, 16, 17, 18, 20, 20, 20, 18, 19, 21, 21, 20, 22, 24, 24, 23, 24, 23, 24, 23, 23, 24, 24, 24, 23, 21, 19, 20, 22, 24, 24, 24, 24, 24, 24, 24, 24, 24, 22, 23, 24]

#grid configuration
fig = pyplot.figure()
ax = pyplot.axes(xlim=(-0.5, 24.5), ylim=(-0.5, 24.5))
line, = ax.plot([], [], '-r', lw=2)
ax.set_xticks(np.arange(-0.5, 25 - 0.5, 1))
ax.set_yticks(np.arange(-0.5, 25 - 0.5, 1))
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.grid(True, color = 'black', linestyle = '-')

#function to config background of each frame 
def init():
    line.set_data([], [])
    return line,

#function that feed the plot data in function of the actual frame
def animate(i):
    aux = 0
    run_x = []
    run_y = []
    while aux < i:
        run_x.append(moves_x[aux])
        run_y.append(moves_y[aux])
        aux += 1
    line.set_data(run_x, run_y)
    return line,

#generate the canvas to display the plot
canvas = FigureCanvas(fig)

#call animator
def run_animation():
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(moves_x), interval=30, blit=True, repeat=False)

#start and set window
win = QtGui.QMainWindow()
win.setGeometry(50, 50, 600, 600)

#add the plot canvas to a window
win.setCentralWidget(canvas)

#create the button thats gonna call the animation
btn = QtGui.QPushButton("Go!", win)
btn.move(73, 20)
btn.clicked.connect(run_animation) # <--- CALL ANIMATION

win.show()
sys.exit(app.exec_())

代码的方式,如果我切掉这部分:

btn = QtGui.QPushButton("Go!", win)
btn.move(73, 20)
btn.clicked.connect(run_animation)

并替换此部分:

def run_animation():
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(moves_x), interval=30, blit=True, repeat=False)

由此:

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(moves_x), interval=30, blit=True, repeat=False)

动画将正常在PySide窗口运行。但是我想用一个按钮来控制它(重置动画)。

我希望我很清楚!感谢。


有趣的是:

如果我在这里发出非句法错误:

def run_animation():
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(moves_x), interval=30, blit=True, repeat=False)
    error.bla.bla.bla #thats gonna give me a runtime error

代码完全符合我的要求hahahaha ...... 但我不能让这个错误......不冷静:(

1 个答案:

答案 0 :(得分:-1)

您需要在canvas.draw()末尾添加对def run_animation():的通话。如果您希望重复动画,则可能需要设置QTimer以每隔几百毫秒运行canvas.draw()