做动画animation--matplotlib--python2和3通用代码

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42053726/article/details/90105798
官方网址的例子:

https://matplotlib.org/gallery/index.html#animation

制作动画:

https://www.cnblogs.com/endlesscoding/p/10308111.html

FuncAnimation类的说明:注意这是一个类不是函数(官方文档)

https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html

1. sin曲线动的小球。注意,动画效果的框架不全是这样的,看官方的例子就知道了

# coding: utf-8
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def update_points(num):
'''
更新数据点,num代表当前帧的帧数,一定为整数,从0开始,FuncAnimation传入一个np.arange(0, 100),就是100帧,虽然num没有显示自动加1,但是确实加1了,可以打印num看看,真的。
'''

if num%5==0:
point_ani.set_marker("*")
point_ani.set_markersize(12)
else:
point_ani.set_marker("o")
point_ani.set_markersize(8)

point_ani.set_data(x[num], y[num])
text_pt.set_text("x=%.3f, y=%.3f"%(x[num], y[num])) # num 代表第几个索引,一定是整数。
text_pt.set_position((x[num], y[num])) # 设置文本位置。
return point_ani,text_pt, # 返回的对象的内容是下一个帧的数据内容。这里是下一帧的点的位置,和下一帧文本的位置

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

fig = plt.figure(tight_layout=True)
plt.plot(x,y) # 这个图像曲线不画出来还不好使呢,不能正确呈现动态图。
point_ani, = plt.plot(x[0], y[0], "ro") # 先画一个点,这个点不管比例尺多大都能看清。返回一个对象,这个对象可以设置下一个点的位置。
plt.grid(ls="--")
text_pt = plt.text(4, 0.8, '', fontsize=16)
'''
第1个参数fig:即为我们的绘图对象.
第2个参数update_points:更新动画的函数.
第3个参数np.arrange(0, 100):动画帧数,这需要是一个可迭代的对象。
interval参数:动画的时间间隔。
blit参数:是否开启某种动画的渲染。
'''
ani = animation.FuncAnimation(fig, func=update_points, frames=np.arange(0, 100), interval=100, blit=True)
'''
np.arange(0, 100) 这个表示 动画的帧数,这里是100帧,为什么设置成100呢?因为x总共有100个点。
假设设置成2,代表有两帧,分别是x=0和x=下一个点的坐标。很不好看也没有意义。
frames=100效果一样
interval=100 # 前面说了一共有100帧,这里的100 代表每一帧和每一帧的间隔是100ms,越小则越快。越大跑的越慢。
设置成1000 就是间隔是1s走一下。
'''

# ani.save('sin_test2.gif', writer='imagemagick', fps=10)
plt.show()


下一个例子:来自莫凡:
'''
这个例子只是演示FuncAnimation这个方程的参数使用方法,于前面的使用方法做比较。
'''
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))


def animate(i):
line.set_ydata(np.sin(x + i/10.0)) # update the data
return line,


# Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.sin(x))
return line,

# call the animator. blit=True means only re-draw the parts that have changed.
# blit=True dose not work on Mac, set blit=False
# interval= update frequency
ani = animation.FuncAnimation(fig=fig, func=animate, frames=100, init_func=init,
interval=100, blit=False)
'''
frames=100 就是100帧的意思,与前面的np列表一个道理,
func=animate 下一帧的点的信息,xy坐标,
init_func=init 当前帧的点的信息,xy坐标等
blit=False #只更新当前点,不是全部,True则是更新全部,不太懂。
'''

# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
ani.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
ani.save('sin_test2.gif', writer='imagemagick', fps=10)
plt.show()
————————————————
版权声明:本文为CSDN博主「weixin_42053726」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42053726/article/details/90105798

猜你喜欢

转载自www.cnblogs.com/gisoracle/p/11997911.html