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 + openpyxl处理excel2007文档思路以及心得
Jul 14 Python
python字符串连接的N种方式总结
Sep 17 Python
详解python的几种标准输出重定向方式
Aug 15 Python
Python字符编码与函数的基本使用方法
Sep 30 Python
Python实现的堆排序算法原理与用法实例分析
Nov 22 Python
对django中foreignkey的简单使用详解
Jul 28 Python
python命名空间(namespace)简单介绍
Aug 10 Python
安装多个版本的TensorFlow的方法步骤
Apr 21 Python
基于python实现地址和经纬度转换
May 19 Python
Python flask框架端口失效解决方案
Jun 04 Python
python求numpy中array按列非零元素的平均值案例
Jun 08 Python
解决Pytorch半精度浮点型网络训练的问题
May 24 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/05/04 PHP
PHP json_encode() 函数详解及中文乱码问题
2015/11/05 PHP
Laravel给生产环境添加监听事件(SQL日志监听)
2017/06/19 PHP
PHP实现微信商户支付企业付款到零钱功能
2018/09/30 PHP
php创建类并调用的实例方法
2019/09/25 PHP
个人总结的一些关于String、Function、Array的属性和用法
2007/01/10 Javascript
JavaScript获取GridView中用户点击控件的行号,列号
2009/04/14 Javascript
基于jquery的划词搜索实现(备忘)
2010/09/14 Javascript
jQuery 源码分析笔记(5) jQuery.support
2011/06/19 Javascript
jquery Moblie入门—hello world的示例代码学习
2013/01/08 Javascript
SeaJS 与 RequireJS 的差异对比
2014/12/08 Javascript
javascript使用switch case实现动态改变超级链接文字及地址
2014/12/16 Javascript
node.js中的fs.createReadStream方法使用说明
2014/12/17 Javascript
jQuery EasyUI 菜单与按钮之创建简单的菜单和链接按钮
2015/11/18 Javascript
easyui validatebox验证
2016/04/29 Javascript
JS表单数据验证的正则表达式(常用)
2017/02/18 Javascript
从零开始学习Node.js系列教程之基于connect和express框架的多页面实现数学运算示例
2017/04/13 Javascript
基于jQuery实现图片推拉门动画效果的两种方法
2017/08/26 jQuery
vue实现提示保存后退出的方法
2018/03/15 Javascript
Node.js+Express+Mysql 实现增删改查
2019/04/03 Javascript
用PyQt进行Python图形界面的程序的开发的入门指引
2015/04/14 Python
Python闭包实现计数器的方法
2015/05/05 Python
Python获取文件所在目录和文件名的方法
2017/01/12 Python
详解 Python 与文件对象共事的实例
2017/09/11 Python
Python分析学校四六级过关情况
2017/11/22 Python
Python动态生成多维数组的方法示例
2018/08/09 Python
python实现Zabbix-API监控
2018/09/17 Python
对Python生成汉字字库文字,以及转换为文字图片的实例详解
2019/01/29 Python
PYQT5设置textEdit自动滚屏的方法
2019/06/14 Python
Python实现在线批量美颜功能过程解析
2020/06/10 Python
浅谈TensorFlow之稀疏张量表示
2020/06/30 Python
欧洲最大的球衣网上商店:Kitbag
2017/11/11 全球购物
项目专员岗位职责
2013/12/04 职场文书
六一儿童节开幕词
2015/01/29 职场文书
研究生简历自我评
2015/03/11 职场文书
余世维讲座观后感
2015/06/11 职场文书