以this answer中给出的示例为基础,我需要为大量数据制作动画。这需要在我成功实现的FuncAnimation中使用blitting。
问题是每次我将鼠标悬停在小部件按钮上以进行播放/暂停/等操作时,都会打开提示音。它导致整个图形闪烁。窗口小部件按钮如何重绘图形和FuncAnimation blitting似乎存在冲突。看起来这些小部件正在整个图形上重新绘制空白背景,然后动画轴很快得到刷新。
有没有一种方法可以使它们完美地搭配在一起?是否需要使FuncAnimation知道窗口小部件才能使blitting算法正常工作?
# global settings
framerate = 24 # frames per second to render animation
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import mpl_toolkits.axes_grid1
import matplotlib.widgets
class Player(FuncAnimation):
def __init__(self, fig, func, frames=None, init_func=None, fargs=None,
save_count=None, mini=0, maxi=100, pos=(0.125, 0.92),
stepsize=1, artists=None, **kwargs):
self.i = 0
self.min=mini
self.max=maxi
self.runs = True
self.forwards = True
self.fig = fig
self.func = func
self.setup(pos)
self.stepsize = stepsize
self.artists=artists
FuncAnimation.__init__(self, self.fig, self.update, frames=self.play(),
init_func=init_func, fargs=fargs,
save_count=save_count, **kwargs )
def play(self):
# generator function to drive animation
while self.runs:
self.i = self.i+self.forwards*self.stepsize-(not self.forwards)*self.stepsize
if self.i > self.min and self.i < self.max:
yield self.i
else:
self.stop()
yield self.i
def start(self):
self.runs=True
self.event_source.start()
def stop(self, event=None):
self.runs = False
self.event_source.stop()
def forward(self, event=None):
self.forwards = True
self.start()
def backward(self, event=None):
self.forwards = False
self.start()
def oneforward(self, event=None):
self.forwards = True
self.onestep()
def onebackward(self, event=None):
self.forwards = False
self.onestep()
def onestep(self):
if self.i > self.min and self.i < self.max:
self.i = self.i+self.forwards*self.stepsize-(not self.forwards)*self.stepsize
elif self.i == self.min and self.forwards:
self.i+=self.stepsize
elif self.i == self.max and not self.forwards:
self.i-=self.stepsize
self.func(self.i)
self.slider.set_val(self.i)
self.fig.canvas.draw_idle()
def setup(self, pos):
playerax = self.fig.add_axes([pos[0],pos[1], 0.64, 0.04])
divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax)
bax = divider.append_axes("right", size="80%", pad=0.05)
sax = divider.append_axes("right", size="80%", pad=0.05)
fax = divider.append_axes("right", size="80%", pad=0.05)
ofax = divider.append_axes("right", size="100%", pad=0.05)
sliderax = divider.append_axes("right", size="500%", pad=0.07)
self.button_oneback = matplotlib.widgets.Button(playerax, label=u'\u29CF')
self.button_back = matplotlib.widgets.Button(bax, label=u'\u25C0')
self.button_stop = matplotlib.widgets.Button(sax, label=u'\u25A0')
self.button_forward = matplotlib.widgets.Button(fax, label=u'\u25B6')
self.button_oneforward = matplotlib.widgets.Button(ofax, label=u'\u29D0')
self.button_oneback.on_clicked(self.onebackward)
self.button_back.on_clicked(self.backward)
self.button_stop.on_clicked(self.stop)
self.button_forward.on_clicked(self.forward)
self.button_oneforward.on_clicked(self.oneforward)
self.slider = matplotlib.widgets.Slider(sliderax, '',
self.min, self.max, valinit=self.i)
self.slider.on_changed(self.set_pos)
def set_pos(self,i):
self.i = int(self.slider.val)
self.func(self.i)
def update(self,i):
self.slider.set_val(i)
return self.artists
fig, ax = plt.subplots(2,1, sharex=True)
x = np.linspace(0,.025, num=1000)
# list of artists that need dynamic updating
dyn_artists = []
dyn_artists.append(ax[0].plot([], [], animated=True)[0])
dyn_artists.append(ax[1].plot([], [], animated=True)[0])
ax[0].set_ylim(-1,1)
ax[1].set_ylim(-1,1)
ax[1].set_xlim(0,.025)
def init_fig():
return dyn_artists
def calc_y(x, phi):
return np.sin(2*np.pi*400*x + phi*np.pi/180.)
def update_data(phi):
dyn_artists[0].set_data(x, calc_y(x, phi))
dyn_artists[1].set_data(x, calc_y(x, phi-120))
ani = Player(fig, update_data, maxi=360*100, stepsize=20,
interval=1000/framerate, blit=True, artists=dyn_artists,
init_func=init_fig)
plt.show()