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 相关文章推荐
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
Jun 18 Python
python 2.7.13 安装配置方法图文教程
Sep 18 Python
Python中应该使用%还是format来格式化字符串
Sep 25 Python
Python读取excel指定列生成指定sql脚本的方法
Nov 28 Python
对python opencv 添加文字 cv2.putText 的各参数介绍
Dec 05 Python
django 外键model的互相读取方法
Dec 15 Python
Python中模块(Module)和包(Package)的区别详解
Aug 07 Python
Python中*args和**kwargs的区别详解
Sep 17 Python
Python实现将元组中的元素作为参数传入函数的操作
Jun 05 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 Python
Django contrib auth authenticate函数源码解析
Nov 12 Python
python读取pdf格式文档的实现代码
Apr 01 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通过Chianz.com获取IP地址与地区的方法
2015/01/14 PHP
最新最全PHP生成制作验证码代码详解(推荐)
2016/06/12 PHP
php中preg_replace正则替换用法分析【一次替换多个值】
2017/01/17 PHP
使用composer 安装 laravel框架的方法图文详解
2019/08/02 PHP
phpfpm的作用和用法
2019/10/10 PHP
Aster vs Newbee BO5 第一场2.19
2021/03/10 DOTA
jQuery之网页换肤实现代码
2011/04/30 Javascript
jQuery 1.8 Release版本发布了
2012/08/14 Javascript
高效率JavaScript编写技巧整理
2013/08/23 Javascript
javascript使用isNaN()函数判断变量是否为数字
2013/09/21 Javascript
js中判断对象是否为空的三种实现方法
2013/12/23 Javascript
jQuery中使用each处理json数据
2015/04/23 Javascript
详解Angular.js指令中scope类型的几种特殊情况
2017/02/21 Javascript
vue + socket.io实现一个简易聊天室示例代码
2017/03/06 Javascript
基于vue2框架的机器人自动回复mini-project实例代码
2017/06/13 Javascript
利用C/C++编写node.js原生模块的方法教程
2017/07/07 Javascript
Angular4集成ng2-file-upload的上传组件
2018/03/14 Javascript
JS实现键值对遍历json数组功能示例
2018/05/30 Javascript
json字符串传到前台input的方法
2018/08/06 Javascript
解决element UI 自定义传参的问题
2018/08/22 Javascript
微信小程序实现带放大效果的轮播图
2020/05/26 Javascript
js仿京东放大镜效果
2020/08/09 Javascript
深度剖析使用python抓取网页正文的源码
2014/06/11 Python
Python2与python3中 for 循环语句基础与实例分析
2017/11/20 Python
HTML+CSS3+JS 实现的下拉菜单
2020/11/25 HTML / CSS
Belstaff英国官方在线商店:Belstaff.co.uk
2021/02/09 全球购物
大学生军训自我评价分享
2013/11/09 职场文书
平面设计师的工作职责
2013/11/21 职场文书
国际金融专业大学生职业生涯规划书
2013/12/28 职场文书
任命书格式
2014/06/05 职场文书
项目经理任命书内容
2014/06/06 职场文书
土木工程专业本科生求职信
2014/10/01 职场文书
新闻人物通讯稿
2014/10/09 职场文书
房屋产权证明书
2014/10/15 职场文书
就业推荐表院系意见
2015/06/05 职场文书
Python中的pprint模块
2021/11/27 Python