Python 使用基于示例的代码暂停动画时遇到问题

Python 使用基于示例的代码暂停动画时遇到问题,python,animation,matplotlib,event-handling,Python,Animation,Matplotlib,Event Handling,我想设置一些数据的动画,我一直在按照另一个堆栈问题的示例来启用暂停。然而,我正在做一些有点不同的事情。在该示例中,他们使用的是正弦函数,该函数可以在参数中接受非整数值。我所做的是在y轴上绘制一组数据,并将其与相应的x值(本例中为时间)匹配 期望输出 我只是希望输出能够绘制数据并在运行时更新记号(因此下面的ax.set\u xlim(0,I/sf)),同时能够暂停或播放动画 接近但不正确的解决方案 这个解决方案的问题是,当我暂停然后取消暂停动画时,如果我没有暂停,绘图会将数据绘制到该时间点的任

我想设置一些数据的动画,我一直在按照另一个堆栈问题的示例来启用暂停。然而,我正在做一些有点不同的事情。在该示例中,他们使用的是正弦函数,该函数可以在参数中接受非整数值。我所做的是在y轴上绘制一组数据,并将其与相应的x值(本例中为时间)匹配


期望输出 我只是希望输出能够绘制数据并在运行时更新记号(因此下面的
ax.set\u xlim(0,I/sf)
),同时能够暂停或播放动画


接近但不正确的解决方案 这个解决方案的问题是,当我暂停然后取消暂停动画时,如果我没有暂停,绘图会将数据绘制到该时间点的任何位置

您应该能够复制并粘贴此代码,以便在自己的计算机上复制


也试过 我尝试了与我链接的示例类似的结构。它也不起作用

问题是当我运行程序时,似乎什么都没有发生。我想它在画些什么,因为当我试着在图上移动时,我能看到图

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as am

length = 8e6
data = np.random.rand(length)
sf = 100 #Sampling frequency in MHz
x = np.arange(length)/sf 

pause = False

def init():
    line.set_data([], [])
    return line,

def theData():
    i=0
    while i<50:
         if not pause:
            y = data[0:i]
            t = x[0:i]
            i=i+1
            yield t, y, i

def animate(theData):
    t = theData[0]
    y = theData[1]
    i = theData[2]
    line.set_data(t, y)
    ax.set_xlim(0, i/sf)
    time_text.set_text(time_template%(t))
    return line, time_text

def onPress(event):
    if event.key==' ':
        global pause
        pause ^= True 

fig = plt.figure(figsize=(15,15)) 
ax = fig.add_subplot(111)
ax.set_ylim(min(data),max(data))
line, = ax.plot([], [], lw=2)

time_template = 'Time = %.1f $\mu$s'    # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
anim = am.FuncAnimation(fig, animate, theData, interval=40, init_func=init)
fig.canvas.mpl_connect('key_press_event', onPress)

plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
将matplotlib.animation作为am导入
长度=8e6
数据=np.random.rand(长度)
sf=100#采样频率,以MHz为单位
x=净距(长度)/sf
暂停=错误
def init():
行。设置_数据([],[])
回程线,
定义数据():
i=0

而i传递给
animate
的参数是一个帧编号。但是,无论动画是否暂停,该数字都会增加

因此,请引入您自己的全局变量,
frame
,该变量记录真实的帧数,并且仅在动画未暂停时递增:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as am

length = 8*10**6
data = np.random.rand(length)
sf = 100 #Sampling frequency in MHz
x = np.arange(length)/sf

pause = False
frame = 0

def init():
    line.set_data([], [])
    return line,

def animate(i):
    global frame
    if not pause:
        frame += 1
        y = data[0:frame]
        xax = x[0:frame]
        line.set_data(xax, y)
        ax.set_xlim(0, frame/sf)
        time_text.set_text(time_template%(frame/sf))
        return line, time_text

def onPress(event):
    if event.key==' ':
        global pause
        pause ^= True 

fig = plt.figure(figsize=(15,15)) 
ax = fig.add_subplot(111)
ax.set_ylim(min(data),max(data))
line, = ax.plot([], [], lw=2)

time_template = 'Time = %.1f $\mu$s'    # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
anim = am.FuncAnimation(fig, animate, interval=40, init_func=init)
fig.canvas.mpl_connect('key_press_event', onPress)

plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as am

length = 8*10**6
data = np.random.rand(length)
sf = 100 #Sampling frequency in MHz
x = np.arange(length)/sf

pause = False
frame = 0

def init():
    line.set_data([], [])
    return line,

def animate(i):
    global frame
    if not pause:
        frame += 1
        y = data[0:frame]
        xax = x[0:frame]
        line.set_data(xax, y)
        ax.set_xlim(0, frame/sf)
        time_text.set_text(time_template%(frame/sf))
        return line, time_text

def onPress(event):
    if event.key==' ':
        global pause
        pause ^= True 

fig = plt.figure(figsize=(15,15)) 
ax = fig.add_subplot(111)
ax.set_ylim(min(data),max(data))
line, = ax.plot([], [], lw=2)

time_template = 'Time = %.1f $\mu$s'    # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
anim = am.FuncAnimation(fig, animate, interval=40, init_func=init)
fig.canvas.mpl_connect('key_press_event', onPress)

plt.show()