make a button for animation in matplotlib
<p>I just made a simple gui using Qt Designer, the gui has 4 buttons and a widget. The widget will show the animation and the buttons are for pause animation,resume, clean the canvas and start animation. I made this code:</p>

<pre><code>import sys
from PyQt4 import QtGui, uic
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


def start():

    def datos(t=0):
        while True:
            t += 0.1
            yield t, np.cos(t)

    def init():
        ax.set_ylim(-1, 1) 
        ax.set_xlim(0, 5)

    def run(data):
        t,y = data
        xdata.append(t)
        ydata.append(y)
        line.set_data(xdata, ydata)
        xmin,xmax =ax.get_xlim()

        if t &gt; xmax:
            ax.set_xlim(xmin, 1.5*xmax)
            ax.figure.canvas.draw()



    ani = animation.FuncAnimation(fig, run, datos, blit=False, interval=50,
                              repeat=False, init_func=init)                                


    def stop():
        ani.event_source.stop()   

    def borr():
        plt.clf()
        canvas.draw()                       

    def anim():
        ani.event_source.start()

    window.resume.clicked.connect(anim)
    window.pause.clicked.connect(stop)
    window.clean.clicked.connect(borr)    
    return ani        

layout=QtGui.QVBoxLayout()
fig=plt.figure()
canvas=FigureCanvas(fig)
layout.addWidget(canvas)

ax = fig.add_subplot(111)
line,=ax.plot([],[],lw=2)
ax.grid()
xdata, ydata = [], []


app = QtGui.QApplication(sys.argv)
window = uic.loadUi("animacion.ui")


window.start.clicked.connect(start)

window.widget.setLayout(layout)


window.show()
sys.exit(app.exec_())
</code></pre>

<p>this shows the grid, but when I press the start button it doesnt show the animation</p>

<p>I also made this code:</p>

<pre><code>import sys
from PyQt4 import QtCore, QtGui, uic
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas



def datos(t=0):
    while True:
        t += 0.1
        yield t, np.cos(t)

def init():
    ax.set_ylim(-1, 1) 
    ax.set_xlim(0, 5)

def run(data):
    t,y = data
    xdata.append(t)
    ydata.append(y)
    line.set_data(xdata, ydata)
    xmin,xmax =ax.get_xlim()

    if t &gt; xmax:
        ax.set_xlim(xmin, 1.5*xmax)
        ax.figure.canvas.draw()
def start():
    window.widget.setLayout(layout)                                

def stop():
    ani.event_source.stop()   

def borr():
    plt.clf()
    canvas.draw()                       

def anim():
    ani.event_source.start()

layout=QtGui.QVBoxLayout()
fig=plt.figure('test')
canvas=FigureCanvas(fig)
layout.addWidget(canvas)

ax = fig.add_subplot(111)
line,=ax.plot([],[],lw=2)
ax.grid()
xdata, ydata = [], []






app = QtGui.QApplication(sys.argv)
window = uic.loadUi("animacion.ui")

window.resume.clicked.connect(anim)
window.pause.clicked.connect(stop)
window.clean.clicked.connect(borr)
window.start.clicked.connect(start)



ani = animation.FuncAnimation(fig, run, datos, blit=False, interval=50,
                              repeat=False, init_func=init)
window.show()
sys.exit(app.exec_())
</code></pre>

<p>In this case, when I press start the animation begins, I can pause and resume. But when a clean the canvas an press start again it doesnt show the function.</p>

<p>How can I make it works?
thanks!</p>

<p>I solved the problem making a function with the animation </p>

<pre><code>import sys
from PyQt4 import QtGui, uic
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas


def start():

    def datos(t=0):
        while True:
            t += 0.1
            yield t, np.cos(t)

    def init():
        ax.set_ylim(-1, 1) 
        ax.set_xlim(0, 5)

    def run(data):
        t,y = data
        xdata.append(t)
        ydata.append(y)
        line.set_data(xdata, ydata)
        xmin,xmax =ax.get_xlim()

        if t &gt; xmax:
            ax.set_xlim(xmin, 1.5*xmax)
            ax.figure.canvas.draw()


    def stop():
        ani.event_source.stop()


    def borr():
        plt.clf()
        canvas.draw()


    def anim():
        ani.event_source.start()


    window.resume.clicked.connect(anim)
    window.pause.clicked.connect(stop)
    window.clean.clicked.connect(borr)

    ax = fig.add_subplot(111)
    line,=ax.plot([],[],lw=2)
    ax.grid()
    xdata, ydata = [], []

    ani = animation.FuncAnimation(fig, run, datos, blit=False, interval=50,
                              repeat=False, init_func=init)
    canvas.draw()    


layout=QtGui.QVBoxLayout()
fig=plt.figure()
canvas=FigureCanvas(fig)
layout.addWidget(canvas)




app = QtGui.QApplication(sys.argv)
window = uic.loadUi("animacion.ui")


window.start.clicked.connect(start)

window.widget.setLayout(layout)


window.show()
sys.exit(app.exec_())
</code></pre>

python qt animation button matplotlib