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用来获得图片exif信息的库实例分析
Mar 16 Python
Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】
Dec 15 Python
Python中asyncio与aiohttp入门教程
Oct 16 Python
Django中间件拦截未登录url实例详解
Sep 03 Python
Python基于time模块表示时间常用方法
Jun 18 Python
关于Theano和Tensorflow多GPU使用问题
Jun 19 Python
keras 多任务多loss实例
Jun 22 Python
python下载的库包存放路径
Jul 27 Python
Python脚本调试工具安装过程
Jan 11 Python
python 进制转换 int、bin、oct、hex的原理
Jan 13 Python
python编写扎金花小程序的实例代码
Feb 23 Python
Python insert() / append() 用法 Leetcode实战演示
Mar 31 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 file_get_contents 函数超时的几种解决方法
2009/07/30 PHP
谷歌音乐搜索栏的提示功能php修正代码
2011/05/09 PHP
php更新mysql后获取影响的行数发生异常解决方法
2013/03/28 PHP
PHP 中魔术常量的实例详解
2017/10/26 PHP
超级强大的表单验证
2006/06/26 Javascript
使用jquery插件实现图片延迟加载技术详细说明
2011/03/12 Javascript
JS+CSS实现一个气泡提示框
2013/08/18 Javascript
js识别不同浏览器基于userAgent做判断
2014/07/29 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
设置点击文本框或图片弹出日历控件的实现代码
2016/05/12 Javascript
ajax实现加载页面、删除、查看详细信息 bootstrap美化页面!
2017/03/14 Javascript
Angular6 写一个简单的Select组件示例
2018/08/20 Javascript
vue实现动态按钮功能
2019/05/13 Javascript
js实现表单项的全选、反选及删除操作示例
2020/06/05 Javascript
[57:22]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第五场
2018/04/10 DOTA
python备份文件的脚本
2008/08/11 Python
Python编程argparse入门浅析
2018/02/07 Python
Python PyCharm如何进行断点调试
2019/07/05 Python
使用python执行shell脚本 并动态传参 及subprocess的使用详解
2020/03/06 Python
Python类和实例的属性机制原理详解
2020/03/21 Python
python 读取、写入txt文件的示例
2020/09/27 Python
详解向scrapy中的spider传递参数的几种方法(2种)
2020/09/28 Python
英国购买威士忌网站:Master of Malt
2019/09/26 全球购物
如何保障Web服务器安全
2014/05/05 面试题
质量工程师岗位职责
2013/11/16 职场文书
保安的辞职报告怎么写
2014/01/20 职场文书
保安部任务及岗位职责
2014/02/25 职场文书
酒店管理毕业生自荐信
2014/05/25 职场文书
大学生优秀班干部事迹材料
2014/05/26 职场文书
白酒代理协议书范本
2014/10/26 职场文书
公司处罚决定书
2015/06/24 职场文书
军训新闻稿范文
2015/07/17 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
详解Java实践之抽象工厂模式
2021/06/18 Java/Android
关于Python OS模块常用文件/目录函数详解
2021/07/01 Python
基于python定位棋子位置及识别棋子颜色
2021/07/26 Python