zoukankan      html  css  js  c++  java
  • Python学习-使用matplotlib画动态多图

    最近常常使用matplotlib进行数学函数图的绘制,可是怎样使用matplotlib绘制动态图,以及绘制动态多图。直到今天才学会。

    1.參考文字 

    首先感谢几篇文字的作者。帮我学会了怎样绘制。大家也能够參考他们的文字。

    1. http://blog.csdn.net/rumswell/article/details/11731003:文字作者给出了数个演示样例的源代码。可是没有非常具体的解说。源代码面前无秘密。自己看吧。

    2. http://mytrix.me/2013/08/matplotlib-animation-tutorial/:这位作者的解说非常具体,主要讲了matplotlib官方演示样例。大家能够參阅。


    3. http://blog.yangyu.me/2014/08/06/matplotlib-graphing-series/:这位作者,给出了不同的演示样例,并且非常具体,告诉了大家怎样一步步学习Matplotlib画图
    4. http://sebug.net/paper/books/scipydoc/matplotlib_intro.html#id4:用Python做科学计算,非常好非常详实的书。

    2.程序源代码

    先贴出程序源代码,在一步步做解释。

    <span style="font-family:SimSun;font-size:10px;">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() 
    ax1 = fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))
    ax2 = fig.add_subplot(2,1,2,xlim=(0, 2), ylim=(-4, 4))
    line, = ax1.plot([], [], lw=2)  
    line2, = ax2.plot([], [], lw=2)  
    def init():  
        line.set_data([], [])  
        line2.set_data([], [])  
        return line,line2
    
    # animation function.  this is called sequentially   
    def animate(i):
    
        x = np.linspace(0, 2, 100)   
        y = np.sin(2 * np.pi * (x - 0.01 * i))  
        line.set_data(x, y)      
    
    
        x2 = np.linspace(0, 2, 100)   
        y2 = np.cos(2 * np.pi * (x2 - 0.01 * i))* np.sin(2 * np.pi * (x - 0.01 * i))  
        line2.set_data(x2, y2)   
        return line,line2
    
    anim1=animation.FuncAnimation(fig, animate, init_func=init,  frames=50, interval=10)  
    plt.show()  </span>

    3.解释

    如今就来解释下。这个程序我到底干了啥

    3.1建立子图、空白线

    fig = plt.figure() 
    ax1 = fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))
    ax2 = fig.add_subplot(2,1,2,xlim=(0, 2), ylim=(-4, 4))
    line, = ax1.plot([], [], lw=2)  
    line2, = ax2.plot([], [], lw=2)  
    在上面的程序能够看到,先建立了一个figure对象。之后fig.add_subplot(2,1,1,xlim=(0, 2), ylim=(-4, 4))就是建立子图,关于子图的概念和做法,大家能够參阅下文字【4】“用Python做科学计算”关于子图的介绍。

    3.2创建动画发生时调用的函数

    Init()是我们的动画在在创建动画基础框架(base frame)时调用的函数。这里我们们用一个非常easy的对line什么都不做的函数。这个函数一定要返回line对象,这个非常重要。由于这样就能告诉动画之后要更新的内容,也就是动作的内容是line。--来自(http://mytrix.me/2013/08/matplotlib-animation-tutorial/
    上面的这段话,解释了Init()这个函数是干嘛的,由于我的程序比較特殊,希望可以在一张图中显示两个子图,如图3.1。所以我必须在两个坐标轴ax1和ax2中创建两个空白的线line,line2且在Init()中返回这两个Line。


    图3.1

    def init():  
        line.set_data([], [])  
        line2.set_data([], [])  
        return line,line2

    3.3动画函数

    接下来你须要一个动画函数。在这个动画函数中改动你的图。相同的我须要一张图中显示两个东西,所以在动画函数中,我更新了两个图,且返回了line和line2
    def animate(i):
    
        x = np.linspace(0, 2, 100)   
        y = np.sin(2 * np.pi * (x - 0.01 * i))  
        line.set_data(x, y)      
    
    
        x2 = np.linspace(0, 2, 100)   
        y2 = np.cos(2 * np.pi * (x2 - 0.01 * i))* np.sin(2 * np.pi * (x - 0.01 * i))  
        line2.set_data(x2, y2)   
        return line,line2

    3.4显示动画

    最后你须要用例如以下的两个语句来显示动画,这里有个注意的地方。须要调整interval參数(这个參数指明了时间间隔)的大小,不然会出现如图3.2一样的情况(当你使用了,blit=True这个选项)。
    同一时候http://mytrix.me/2013/08/matplotlib-animation-tutorial/)给我们说明了几个參数的作用,我在不在复述:

    这个对象须要持续存在,全部我们要将它赋给一个变量。我们选择了一个100帧的动画(译者注:你上边的代码还是200帧,怎么到这儿就变成100帧了……。另外。这里也不一定一定要是个数字,能够是个generator 或iterable,详见API说明)而且帧与帧之间间隔20ms,blit是一个很重要的keyword。它告诉动画仅仅重绘改动的部分。结合上面保存的时间, blit=true会使动画显示得会很很快。


    图3.2

    anim1=animation.FuncAnimation(fig, animate, init_func=init,  frames=50, interval=10)  
    plt.show()  

    3.5结束

    上面的工作解释完了,来看看成果。程序写的不好。我也是才初学。希望看到博客的人。能多多给我不吝赐教,不胜感激。



  • 相关阅读:
    hdoj2187:悼念512汶川大地震遇难同胞 (贪心)
    2.0其它之Transform详解,以及UIElement和FrameworkElement的常用属性
    2.0外观之样式, 模板, 视觉状态和视觉状态管理器
    2.0图形之Ellipse, Line, Path, Polygon, Polyline, Rectangle
    2.0控件之ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton
    2.0画笔之SolidColorBrush, ImageBrush, VideoBrush, LinearGradientBrush, RadialGradientBrush
    2.0图形之基类System.Windows.Shapes.Shape
    2.0交互之鼠标事件和键盘事件
    2.0控件之ScrollViewer, Slider, StackPanel, TabControl, TextBlock, TextBox, ToggleButton
    2.0交互之InkPresenter(涂鸦板)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5115410.html
Copyright © 2011-2022 走看看