Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用PySide按钮重置Matplotlib动画_Python_Animation_Matplotlib_Pyside - Fatal编程技术网

Python 使用PySide按钮重置Matplotlib动画

Python 使用PySide按钮重置Matplotlib动画,python,animation,matplotlib,pyside,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 Fi

我想创建一个按钮,每当我点击它,它将重置并执行相同的动画。看看下面我都试了些什么

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

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_())
并将此部分替换为:

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
代码完全符合我的要求,哈哈哈。。。
但我不能让这个错误。。。不酷:(

您需要在
def run\u animation():
的末尾添加对
canvas.draw()
的调用。如果希望动画重复,您可能需要设置一个QTimer,以便每隔几百毫秒运行
canvas.draw()

查看以下答案:
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