matplotlib动画入门(2):第一个动画

新建一个文件 basic_animation.py,代码如下

源代码来源 https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

'''
引入相应包
'''
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

#创建一个Figure
fig = plt.figure()

#创建坐标,横坐标显示的区间是(0,2),纵坐标显示的空间是(-2,2)
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#创建一个图(plot),初始横坐标纵坐标都是空,linewidth=2
line, = ax.plot([], [], lw=2)


#初始化函数,会被FuncAnimation调用
def init():
    line.set_data([], [])
    return line,

# 动画函数,每一帧都会调用此函数,i为帧号.
def animate(i):
    #返回一个ndarray数组,起始为0,终止为2,100个元素。
    x = np.linspace(0, 2, 100)
    #计算y值
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

'''
执行动画
fig: 
 call the animator.  blit=True means only re-draw the parts that have changed.
'''
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=200, interval=20, blit=True)
plt.show()

 

执行代码

python3 basic_animation.py

 

 

 

转载于:https://my.oschina.net/stanleysun/blog/1807870

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值