Python通过matplotlib绘制动画简单实例


Posted in Python onDecember 13, 2017

Matplotlib是一个Python的2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。

通过Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等。

matplotlib从1.1.0版本以后就开始支持绘制动画,具体使用可以参考官方帮助文档。下面是一个很基本的例子:

"""
A simple example of an animated plot
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
# create our line object which will be modified in the animation
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
# we simply plot an empty line: we'll add data to the line later
line, = ax.plot([], [], lw=2) 
# initialization function: plot the background of each frame
def init():
 line.set_data([], [])
 return line,
# animation function. This is called sequentially
# It takes a single parameter, the frame number i 
def animate(i):
 x = np.linspace(0, 2, 1000)
 y = np.sin(2 * np.pi * (x - 0.01 * i)) # update the data
 line.set_data(x, y)
 return line,
# Makes an animation by repeatedly calling a function func
# frames can be a generator, an iterable, or a number of frames.
# interval draws a new frame every interval milliseconds.
# blit=True means only re-draw the parts that have changed.
# 在这里设置一个200帧的动画,每帧之间间隔20毫秒
anim = animation.FuncAnimation(fig, animate, init_func=init,
        frames=200, interval=20, blit=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
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show() # plt.show() 会一直循环播放动画

结果:

Python通过matplotlib绘制动画简单实例

如果要将动画保存为mp4格式的视频文件,则需要先安装FFmpeg。FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

在这里下载windows的版本:DownloadFFmpegforWindows,解压,然后将bin目录加入系统环境变量的路径中。如:C:\ProgramFiles\ffmpeg-3.2.2-win64-static\bin。然后测试是否配置OK:输入ffmpeg-version

Python通过matplotlib绘制动画简单实例

总结

以上就是本文关于Python通过matplotlib绘制动画简单实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python模拟新浪微博登陆功能(新浪微博爬虫)
Dec 24 Python
详解Django中的过滤器
Jul 16 Python
Python用imghdr模块识别图片格式实例解析
Jan 11 Python
Python实现带参数与不带参数的多重继承示例
Jan 30 Python
使用numpy和PIL进行简单的图像处理方法
Jul 02 Python
利用pandas进行大文件计数处理的方法
Jul 25 Python
python实现动态创建类的方法分析
Jun 25 Python
python实现两个dict合并与计算操作示例
Jul 01 Python
Python Pandas 箱线图的实现
Jul 23 Python
tensorflow指定GPU与动态分配GPU memory设置
Feb 03 Python
Python Flask上下文管理机制实例解析
Mar 16 Python
详解pytorch创建tensor函数
Mar 22 Python
Python数据结构与算法之字典树实现方法示例
Dec 13 #Python
Python数据结构与算法之完全树与最小堆实例
Dec 13 #Python
python+VTK环境搭建及第一个简单程序代码
Dec 13 #Python
VTK与Python实现机械臂三维模型可视化详解
Dec 13 #Python
python+pygame简单画板实现代码实例
Dec 13 #Python
Python实现简单的语音识别系统
Dec 13 #Python
关于反爬虫的一些简单总结
Dec 13 #Python
You might like
ThinkPHP入口文件设置及相关注意事项分析
2014/12/05 PHP
两款万能的php分页类
2015/11/12 PHP
PHP判断手机是IOS还是Android
2015/12/09 PHP
PHP指定截取字符串中的中英文或数字字符的实例分享
2016/03/18 PHP
PHP实现Unicode编码相互转换的方法示例
2020/11/17 PHP
PHP静态延迟绑定和普通静态效率的对比
2017/10/20 PHP
ThinkPHP3.2.3框架实现执行原生SQL语句的方法示例
2019/04/03 PHP
ThinkPHP框架实现的微信支付接口开发完整示例
2019/04/10 PHP
PHP基于ip2long实现IP转换整形
2020/12/11 PHP
jQuery实现不断闪烁文字的方法
2015/05/15 Javascript
SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
2015/12/10 Javascript
JS实现1000以内被3或5整除的数字之和
2016/02/18 Javascript
JavaScript中的Array 对象(数组对象)
2016/06/02 Javascript
三种带箭头提示框总结实例
2016/06/14 Javascript
内容滑动切换效果jquery.hwSlide.js插件封装
2016/07/07 Javascript
js实现模糊匹配功能
2017/02/15 Javascript
基于node.js制作简单爬虫教程
2017/06/29 Javascript
Angular2使用vscode断点调试ts文件的方法
2017/12/13 Javascript
React数据传递之组件内部通信的方法
2017/12/31 Javascript
vue将时间戳转换成自定义时间格式的方法
2018/03/02 Javascript
vue单文件组件无法获取$refs的问题
2020/06/24 Javascript
[23:18]Spirit vs Liquid Supermajor小组赛A组 BO3 第二场 6.2
2018/06/03 DOTA
Python中的引用和拷贝浅析
2014/11/22 Python
Python连接数据库学习之DB-API详解
2017/02/07 Python
python编程羊车门问题代码示例
2017/10/25 Python
对Python使用mfcc的两种方式详解
2019/01/09 Python
python代码如何实现余弦相似性计算
2020/02/09 Python
python程序如何进行保存
2020/07/03 Python
python中的测试框架
2020/11/13 Python
css3实现背景模糊的三种方式(小结)
2020/05/15 HTML / CSS
详解CSS3弹性伸缩盒
2020/09/21 HTML / CSS
Zavvi荷兰:英国大型音像制品和图书游戏零售商
2018/03/22 全球购物
任意存:BOXFUL
2018/05/21 全球购物
Wiggle美国:英国骑行、跑步、游泳、铁人三项商店
2018/10/27 全球购物
美国购买体育、音乐会和剧院门票网站:SelectATicket
2019/09/08 全球购物
团队执行力培训心得体会
2015/08/15 职场文书