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使用reportlab画图示例(含中文汉字)
Dec 03 Python
python调用API实现智能回复机器人
Apr 10 Python
Python实现对文件进行单词划分并去重排序操作示例
Jul 10 Python
python实现贪吃蛇游戏
Mar 21 Python
Python和Go语言的区别总结
Feb 20 Python
Python基于OpenCV实现人脸检测并保存
Jul 23 Python
python实现网站用户名密码自动登录功能
Aug 09 Python
用python画一只可爱的皮卡丘实例
Nov 21 Python
python中os包的用法
Jun 01 Python
将pycharm配置为matlab或者spyder的用法说明
Jun 08 Python
教你如何用python开发一款数字推盘小游戏
Apr 14 Python
python中出现invalid syntax报错的几种原因分析
Feb 12 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
世界上第一台立体声收音机
2021/03/01 无线电
PHP通过内置函数memory_get_usage()获取内存使用情况
2014/11/20 PHP
tp5(thinkPHP5框架)时间查询操作实例分析
2019/05/29 PHP
laravel 关联关系遍历数组的例子
2019/10/10 PHP
js css样式操作代码(批量操作)
2009/10/09 Javascript
Jquery替换已存在于element上的event的方法
2010/03/09 Javascript
JQuery 选择和过滤方法代码总结
2010/11/19 Javascript
jQuery代码优化 事件委托篇
2011/11/01 Javascript
javascript中的事件代理初探
2014/03/08 Javascript
jQuery中index()方法用法实例
2014/12/27 Javascript
jQuery解析XML文件同时动态增加js文件的方法
2015/06/01 Javascript
jQuery支持添加事件的日历特效代码分享(3种样式)
2015/08/24 Javascript
Vuejs第十一篇组件之slot内容分发实例详解
2016/09/09 Javascript
Bootstrap幻灯片轮播图支持触屏左右手势滑动的实现方法
2016/10/13 Javascript
jQuery中弹出iframe内嵌页面元素到父页面并全屏化的实例代码
2016/12/27 Javascript
JS实现的Unicode编码转换操作示例
2017/04/28 Javascript
vue中计算属性(computed)、methods和watched之间的区别
2017/07/27 Javascript
微信小程序使用swiper组件实现层叠轮播图
2018/11/04 Javascript
python编程实现12306的一个小爬虫实例
2017/12/27 Python
解决Tensorflow安装成功,但在导入时报错的问题
2018/06/13 Python
numpy返回array中元素的index方法
2018/06/27 Python
Python socket实现的简单通信功能示例
2018/08/21 Python
pandas实现DataFrame显示最大行列,不省略显示实例
2019/12/26 Python
python中id函数运行方式
2020/07/03 Python
阿玛尼意大利官网:Armani意大利
2018/10/30 全球购物
Bulk Powders意大利:运动补充在线商店
2019/02/09 全球购物
Java 中访问数据库的步骤?Statement 和PreparedStatement 之间的区别?
2012/06/05 面试题
养殖行业的创业计划书
2014/01/05 职场文书
学生会部长竞聘书
2014/03/31 职场文书
《鸿门宴》教学反思
2014/04/22 职场文书
孝敬父母的活动方案
2014/08/31 职场文书
义诊活动总结
2015/02/04 职场文书
男方家长婚礼答谢词
2015/09/29 职场文书
2019年教师入党申请书
2019/06/27 职场文书
Django使用channels + websocket打造在线聊天室
2021/05/20 Python
Python中re模块的元字符使用小结
2022/04/07 Python