使用PySide按钮重置Matplotlib动画

0 投票
1 回答
1054 浏览
提问于 2025-04-18 12:58

我想创建一个按钮,每次点击它时,它都会重置并执行相同的动画。看看我下面尝试的内容。

这是完整的代码和注释:

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

代码就会完全按照我想要的那样工作,哈哈哈……

但我不能让这个错误存在……这可不好 :(

1 个回答

-1

你需要在 def run_animation(): 的最后加上一个 canvas.draw() 的调用。如果你想让动画重复播放,可能还需要设置一个 QTimer,让 canvas.draw() 每隔几百毫秒执行一次。

撰写回答