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类继承与子类实例初始化用法分析
Apr 17 Python
Python设计模式编程中Adapter适配器模式的使用实例
Mar 02 Python
Python基本语法经典教程
Mar 11 Python
Python管理Windows服务小脚本
Mar 12 Python
django与小程序实现登录验证功能的示例代码
Feb 19 Python
搞定这套Python爬虫面试题(面试会so easy)
Apr 03 Python
python分数表示方式和写法
Jun 26 Python
Python实现图像的垂直投影示例
Jan 17 Python
PyCharm设置注释字体颜色以及是否倾斜的操作
Sep 16 Python
详解vscode实现远程linux服务器上Python开发
Nov 10 Python
Pycharm中使用git进行合作开发的教程详解
Nov 17 Python
python 使用csv模块读写csv格式文件的示例
Dec 02 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
去除php注释和去除空格函数分享
2014/03/13 PHP
PHP基于redis计数器类定义与用法示例
2018/02/08 PHP
js运动框架_包括图片的淡入淡出效果
2013/05/11 Javascript
JS实现QQ图片一闪一闪的效果小例子
2013/07/31 Javascript
JavaScript中的prototype和constructor简明总结
2014/04/05 Javascript
JsRender for object语法简介
2014/10/31 Javascript
jQuery中find()方法用法实例
2015/01/07 Javascript
js获取客户端操作系统类型的方法【测试可用】
2016/05/27 Javascript
jquery+css实现侧边导航栏效果
2017/06/12 jQuery
基于JavaScript实现弹幕特效
2020/08/27 Javascript
JS+canvas绘制的动态机械表动画效果
2017/09/12 Javascript
JavaScript图片处理与合成总结
2018/03/04 Javascript
Cocos2d实现刮刮卡效果
2018/12/20 Javascript
[06:09]辉夜杯主赛事开幕式
2015/12/25 DOTA
python和bash统计CPU利用率的方法
2015/07/10 Python
举例讲解Python中的list列表数据结构用法
2016/03/12 Python
使用Python写一个贪吃蛇游戏实例代码
2017/08/21 Python
利用python爬取斗鱼app中照片方法实例
2017/12/03 Python
PyQt5实现暗黑风格的计时器
2019/07/29 Python
Python实现验证码识别
2020/06/15 Python
PyQt5的相对布局管理的实现
2020/08/07 Python
美国最大的农村生活方式零售店:Tractor Supply Company(TSC)
2017/05/15 全球购物
旅游管理实习自我鉴定
2013/09/29 职场文书
人力资源管理专业学生自我评价
2013/11/20 职场文书
应届毕业生求职信范文
2013/12/18 职场文书
培训楼经理岗位责任制
2014/02/10 职场文书
2014年健康教育实施方案
2014/02/17 职场文书
服装采购员岗位职责
2014/03/15 职场文书
项目投资意向书
2014/04/01 职场文书
一年级评语大全
2014/04/23 职场文书
民主评议党员自我评议范文2014
2014/09/26 职场文书
2014个人年度工作总结
2014/12/15 职场文书
写给老师的保证书
2015/05/09 职场文书
学子宴致辞大全
2015/07/27 职场文书
银行安全保卫工作总结
2015/08/10 职场文书
在redisCluster中模糊获取key方式
2021/07/09 Redis