python暂停黑框显示_停止/开始/暂停在Python matplotlib动画

I'm using FuncAnimation in matplotlib's animation module for some basic animation. This function perpetually loops through the animation. Is there a way by which I can pause and restart the animation by, say, mouse clicks?

解决方案

Here is a FuncAnimation example which I modified to pause on mouse clicks.

Since the animation is driven by a generator function, simData, when the global variable pause is True, yielding the same data makes the animation appear paused.

The value of paused is toggled by setting up an event callback:

def onClick(event):

global pause

pause ^= True

fig.canvas.mpl_connect('button_press_event', onClick)

import matplotlib.pyplot as plt

import numpy as np

import matplotlib.animation as animation

pause = False

def simData():

t_max = 10.0

dt = 0.05

x = 0.0

t = 0.0

while t < t_max:

if not pause:

x = np.sin(np.pi*t)

t = t + dt

yield x, t

def onClick(event):

global pause

pause ^= True

def simPoints(simData):

x, t = simData[0], simData[1]

time_text.set_text(time_template%(t))

line.set_data(t, x)

return line, time_text

fig = plt.figure()

ax = fig.add_subplot(111)

line, = ax.plot([], [], 'bo', ms=10) # I'm still not clear on this stucture...

ax.set_ylim(-1, 1)

ax.set_xlim(0, 10)

time_template = 'Time = %.1f s' # prints running simulation time

time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

fig.canvas.mpl_connect('button_press_event', onClick)

ani = animation.FuncAnimation(fig, simPoints, simData, blit=False, interval=10,

repeat=True)

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值