怎么在Matplotlib中利用动画模块实现一个动态图

  介绍

本篇文章为大家展示了怎么在Matplotlib中利用动画模块实现一个动态图,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强> Matplotlib 画图功能非常强大,目前也只能根据官网提供的例子简单地画几张图。最近学习了能画动态图的动画模块,作个简单地记录。

在Matplotlib作图中,比较常用的是Matplotlib。pyplot模块,这个模块有非常多的属性和方法,简要列举下这次用到的方法:
matplotlib.pyplot。次要情节(nrows=1, ncols=1, sharex=False, sharey=False,挤压=True, subplot_kw=None, gridspec_kw=None, * * fig_kw)
返回无花果和ax对象!

例子1。动态画出罪函数曲线

import  numpy  as  np   import  matplotlib.pyplot  as  plt   得到matplotlib.animation  import  FuncAnimation      无花果,ax =, plt.subplots ()   xdata, ydata =, [], []   ln,,=, ax.plot ([], [],, & # 39; r & # 39;,,动画=False)      def  init ():   ax.set_xlim才能(0,2 * np.pi)   ax.set_ylim才能(1,1)   return 才能;ln,      def 更新(帧):   xdata.append才能(框架)   ydata.append才能(np.sin(帧))   ln.set_data才能(xdata, ydata)   return 才能;ln,      时间=ani  FuncAnimation(无花果、,更新,?np.linspace (0, 2 * np.pi,, 128),   ,,,,,,,,,init_func=init,位块传输=True)   plt.show ()

怎么在Matplotlib中利用动画模块实现一个动态图

画这类图的关键是要给出不断更新的函数,这里就是 更新函数了。注意,<代码>行,=ax。情节([]、[]& # 39;r & # 39;,动画=False) 中的<代码>、表示创建tuple类型。迭代更新的数据<代码> 取帧值从 <代码>帧取得。

例子2。动态显示一个动点,它的轨迹是罪函数。

import  numpy  as  np    import  matplotlib.pyplot  as  plt   得到matplotlib  import 动画      “““   animation  example  2   作者:Kiterun   “““      无花果,ax =, plt.subplots ()   时间=x  np.linspace (0, 2 * np.pi,, 200)   时间=y  np.sin (x)   时间=l  ax.plot (x, y)   点,,=,ax.plot ([], [],, & # 39; ro # 39;)      def  init ():   ax.set_xlim才能(0,2 * np.pi)   ax.set_ylim才能(1,1)   return 才能;l      def  gen_dot ():   for 才能小姐:拷贝np.linspace (0, 2 * np.pi,, 200):   ,,,newdot =,(我,np.sin (i))   ,,,油品收率newdot      def  update_dot (newd):   dot.set_data才能(newd [0],, newd [1])   return 点,才能      ani =, animation.FuncAnimation(无花果,update_dot,, frames =, gen_dot,, interval =, 100年,init_func=init)   ani.save (& # 39; sin_dot.gif& # 39;,,作家=& # 39;imagemagick # 39;,, fps=30)      plt.show ()

这里我们把生成的动态图保存为gif图片,前提要预先安装imagemagic。

怎么在Matplotlib中利用动画模块实现一个动态图

例子3。单摆(没阻尼,有阻尼)

无阻尼的单摆力学公式:

怎么在Matplotlib中利用动画模块实现一个动态图

附加阻尼项:

怎么在Matplotlib中利用动画模块实现一个动态图

这里需要用到scipy.integrate的odeint模块,具体用法找时间再专门写一篇博客吧,动态图代码如下:

#, - *安康;编码:utf-8  - * -      得到math  import 罪恶,因为   import  numpy  as  np   得到scipy.integrate  import  odeint   import  matplotlib.pyplot  as  plt   import  matplotlib.animation  as 动画      g  9.8=,   leng  1.0=,   b_const  0.2=,      #,no  decay 案例:   def  pendulum_equations1 (w,, t, l):   ,,,v =w   dth 才能=v   时间=dv 才能;作用;g/l  *,罪(th)   return 才能;对数,dv      #,从而decay  exist 案例:   def  pendulum_equations2 (w,, t, l, b):   ,,,v =w   dth 才能=v   dv 才能=,- b/l  *, v 作用;g/l  *,罪(th)   return 才能;对数,dv      时间=t  np.arange(0,, 20日,0.1)   时间=track  odeint (pendulum_equations1, (1.0, 0),, t, arg游戏=(愣,))   # track =, odeint (pendulum_equations2, (1.0, 0),, t, arg游戏=(愣,,b_const))   时间=xdata [愣* sin(跟踪[0]我也),for 小姐:拷贝范围(len(跟踪)))   时间=ydata [愣* cos(跟踪[0]我也),for 小姐:拷贝范围(len(跟踪)))      无花果,ax =, plt.subplots ()   ax.grid ()   行,,=,ax.plot ([], [],, & # 39; o & # 39;,, lw=2)   时间=time_template  & # 39; time =, % .1fs& # 39;   time_text =, ax.text(0.05, 0.9, & # 39; & # 39;,,变换=ax.transAxes)      def  init ():   ax.set_xlim才能(2,2)   ax.set_ylim才能(2,2)   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Matplotlib中利用动画模块实现一个动态图