Python通过matplotlib绘制动画简单实例

 更新时间:2017年12月13日 11:33:50   作者:冬木远景  
这篇文章主要介绍了Python通过matplotlib绘制动画简单实例,具有一定借鉴价值,需要的朋友可以参考下。

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() 会一直循环播放动画

结果:

如果要将动画保存为mp4格式的视频文件,则需要先安装FFmpeg。FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。采用LGPL或GPL许可证。它提供了录制、转换以及流化音视频的完整解决方案。

在这里下载windows的版本:DownloadFFmpegforWindows,解压,然后将bin目录加入系统环境变量的路径中。如:C:\ProgramFiles\ffmpeg-3.2.2-win64-static\bin。然后测试是否配置OK:输入ffmpeg-version

总结

以上就是本文关于Python通过matplotlib绘制动画简单实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • python使用flask与js进行前后台交互的例子

    python使用flask与js进行前后台交互的例子

    今天小编就为大家分享一篇python使用flask与js进行前后台交互的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • 使用setup.py安装python包和卸载python包的方法

    使用setup.py安装python包和卸载python包的方法

    这篇文章主要介绍了使用setup.py安装python包和卸载python包的方法,大家参考使用吧
    2013-11-11
  • python中tkinter的应用:修改字体的实例讲解

    python中tkinter的应用:修改字体的实例讲解

    今天小编就为大家分享一篇python中tkinter的应用:修改字体的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-07-07
  • pytorch中的nn.ZeroPad2d()零填充函数实例详解

    pytorch中的nn.ZeroPad2d()零填充函数实例详解

    这篇文章主要介绍了pytorch中的nn.ZeroPad2d()零填充函数实例详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Python中的 sort 和 sorted的用法与区别

    Python中的 sort 和 sorted的用法与区别

    这篇文章主要介绍了Python中的 sort 和 sorted的用法与区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python编程中类与类的关系详解

    Python编程中类与类的关系详解

    在本文里小编给大家整理了关于Python编程中类与类的关系以及相关代码知识点,需要的朋友们可以学习下。
    2019-08-08
  • pyqt5与matplotlib的完美结合实例

    pyqt5与matplotlib的完美结合实例

    今天小编就为大家分享一篇pyqt5与matplotlib的完美结合实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • python刷投票的脚本实现代码

    python刷投票的脚本实现代码

    这篇文章主要介绍了写了个python刷投票的脚本,需要的朋友可以参考下
    2014-11-11
  •  Python 匿名函数lambda 详情

     Python 匿名函数lambda 详情

    这篇文章主要介绍了 Python 匿名函数lambda,​​lambda​​​  函数返回函数本身而不是将其赋值给一个变量名。所以它也被称为匿名函数,下文更多相关内容需要的小伙伴可以参考一下
    2022-03-03
  • Python解决爬虫程序卡死问题

    Python解决爬虫程序卡死问题

    这篇文章主要介绍了Python解决爬虫程序卡死问题,文章围绕主题展开详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05

最新评论