【发布时间】:2016-04-06 10:31:32
【问题描述】:
我是 matplotlib 新手,我正在尝试了解动画的工作原理。
我编写了以下 Python 代码来围绕一个矩形移动:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class AnimRect(object):
'''Animate a rectangle'''
def __init__(self):
self.fig = plt.figure(figsize = (5,5))
# create the axes
self.ax = plt.axes(xlim=(0,100), ylim=(0,100), aspect='equal')
# create rectangle
self.rect = plt.Rectangle((70,20), 10, 10,
fill=True, color='gold', ec='blue')
self.ax.add_patch(self.rect)
# initialization function
def init(self):
self.rect.set_xy((10.0, 10.0))
return self.rect,
# animation function
def animate(self, i):
self.rect.set_xy((i,i))
return self.rect,
def call_animation(self):
# call the animator function
animation.FuncAnimation(self.fig, self.animate, frames=91,
init_func= self.init,
interval=20, blit=True, repeat=False)
plt.show()
def main():
rect = AnimRect()
rect.call_animation()
main()
当我运行代码时,设置在初始位置 (10.0,10.0) 的矩形始终停留在屏幕上,而动画矩形的行为符合预期。我想不出为什么。我尝试了一些小的更改,但找不到解决方案。我做错了什么?
【问题讨论】:
标签: python animation matplotlib rectangles