1.帧动画类FuncAnimation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from matplotlib.pyplot import MultipleLocator fig = plt.figure() ax = fig.gca() # ax.set_axis_off() # 取消坐标轴的显示 ax.grid() ax.set_xlim(1, 5) ax.set_ylim(1, 5) ax.xaxis.set_major_locator(MultipleLocator(0.5)) # x轴刻度间隔 ax.yaxis.set_major_locator(MultipleLocator(0.5)) # y轴刻度间隔 ln, = ax.plot([], [], "*", markersize=20) x_list = [2] y_list = [2] # 图像初始化 def init(): x_list[0] = 2 y_list[0] = 2 ln.set_data(x_list, y_list) return ln, # 图像更新 def update(frame): x = x_list[0] y = y_list[0] if x < 4 and y == 2: # 在y=2轴上右移 x += 0.5 elif x == 4 and y < 4: # 在x=4轴上移 y += 0.5 elif x > 2 and y == 4: # 在y=4轴左移 x -= 0.5 elif x == 2 and y > 2: # 在x=2轴下移 y -= 0.5 x_list[0] = x y_list[0] = y ln.set_data(x_list, y_list) return ln, ani = FuncAnimation( # fig=fig, v # figure对象 init_func=init, # 初始化函数。程序开始,先执行init函数,再执行update函数。 func=update, # 更新函数。update函数执行frames次,之后重新调用init函数。 frames=16, # 执行多少次update函数。往复执行。update次数影响动画细腻度-关键是函数内逻辑。 interval=100, # 每帧动画时长毫秒ms。即每次update/init函数耗时interval毫秒。 blit=True, # true更新所有点,false仅更新产生变化的点 ) plt.show() |
2.重绘动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import matplotlib.pyplot as plt ax = plt.subplot(1, 1, 1) ax.set_ylim(-5, 5) ax.grid() # ax.ion() # interactive mode on # ax.ioff() # interactive mode off x = [0, ] y = [0, ] isUp = True line, = ax.plot(x, y, '-g', marker='*') # 返回的是线条列表 for i in range(1000): tmp = x[-1] tmp += 0.5 x.append(tmp) ax.set_xlim(tmp - 25, tmp + 15) tmp = y[-1] if isUp: tmp += 0.5 if tmp == 4: isUp = False else: tmp -= 0.5 if tmp == -4: isUp = True y.append(tmp) line.set_xdata(x) line.set_ydata(y) plt.pause(0.001) # 每次for执行间隔。不设置会瞬间完毕 # 同时plot出的line图形会显示 |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3004.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设