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使用PyGame绘制图像并保存为图片文件的方法
Apr 24 Python
Apache如何部署django项目
May 21 Python
python计算日期之间的放假日期
Jun 05 Python
python二维列表一维列表的互相转换实例
Jul 02 Python
python的pstuil模块使用方法总结
Jul 26 Python
Win系统PyQt5安装和使用教程
Dec 25 Python
pytorch加载自定义网络权重的实现
Jan 07 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
Feb 14 Python
Python3.9又更新了:dict内置新功能
Feb 28 Python
python 等差数列末项计算方式
May 03 Python
python使用隐式循环快速求和的实现示例
Sep 11 Python
No module named ‘win32gui‘ 的解决方法(踩坑之旅)
Feb 18 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/10/23 PHP
PHP获取ip对应地区和使用网络类型的方法
2015/03/11 PHP
php字符串按照单词进行反转的方法
2015/03/14 PHP
Smarty变量用法详解
2016/05/11 PHP
php导出csv文件,可导出前导0实例代码
2016/11/16 PHP
JavaScript之自定义类型
2012/05/04 Javascript
js简单实现删除记录时的提示效果
2013/12/05 Javascript
js实现鼠标移到链接文字弹出一个提示层的方法
2015/05/11 Javascript
初识Javascript小结
2015/07/16 Javascript
函数四种调用模式以及其中的this指向
2017/01/16 Javascript
js实现textarea限制输入字数
2017/02/13 Javascript
详解Vue2 SSR 缓存 Api 数据
2017/11/20 Javascript
js实现简单放大镜效果
2020/03/07 Javascript
python筛选出两个文件中重复行的方法
2018/05/31 Python
python实现微信防撤回神器
2019/04/29 Python
Django配置MySQL数据库的完整步骤
2019/09/07 Python
Python 获取项目根路径的代码
2019/09/27 Python
Flask框架搭建虚拟环境的步骤分析
2019/12/21 Python
Python实现i人事自动打卡的示例代码
2020/01/09 Python
Python GUI自动化实现绕过验证码登录
2020/01/10 Python
python中round函数如何使用
2020/06/19 Python
详解PyQt5中textBrowser显示print语句输出的简单方法
2020/08/07 Python
详解Python yaml模块
2020/09/23 Python
意大利网上药房:Farmacia 33
2020/01/27 全球购物
学校七一活动方案
2014/01/19 职场文书
关于梦想的演讲稿
2014/05/05 职场文书
售后服务承诺书模板
2014/05/21 职场文书
党的群众教育实践活动实施方案
2014/06/12 职场文书
公司领导班子对照材料
2014/08/18 职场文书
纺织工程专业推荐信
2014/09/08 职场文书
社区端午节活动总结
2015/02/11 职场文书
2015中秋节晚会主持词
2015/07/01 职场文书
2019入党申请书格式
2019/06/25 职场文书
Django给表单添加honeypot验证增加安全性
2021/05/06 Python
为什么RedisCluster设计成16384个槽
2021/09/25 Redis
如何开启Apache,Nginx和IIS服务器的GZIP压缩功能
2022/04/29 Servers