怎么用Python绘制几个动画

  介绍

这期内容当中小编将会给大家带来有关怎么用Python绘制几个动画,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

Python中的matplotlib从1.1.0版本以后就开始支持绘制动画,matplotlib是一个Python的2 d绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。这里介绍几种Python使用matplotlib绘制动画的方法。

下面是几个的示例:

<强>第一个例子使用发电机,每隔两秒,就运行函数data_gen:

#,- *安康;编码:,utf-8  - *的背后,,   import  numpy  as  np    import  matplotlib.pyplot  as  plt    import  matplotlib.animation  as  animation    时间=fig  plt.figure (),   时间=axes1  fig.add_subplot (111),   行,,=,axes1.plot (np.random.rand (10)),   #因为更新的参数是调用函数data_gen,   #所以第一个默认参数不能是framenum    def 更新(数据):   line.set_ydata才能(数据),   时间线,return 才能;   #,每次生成10个随机数据,   def  data_gen ():   事实:while 才能;   ,,,油品收率np.random.rand (10),   时间=ani  animation.FuncAnimation(无花果、,更新,data_gen,,间隔=2 * 1000)   plt.show ()

<强>第二个例子使用列表(度量),每次从度量中取一行数据作为参数送入更新中:

import  numpy  as  np    import  matplotlib.pyplot  as  plt    import  matplotlib.animation  as  animation    start =, (1), 0.18, 0.63,, 0.29, 0.03, 0.24, 0.86, 0.07, 0.58, 0],   metric =[[0.03, 0.86, 0.65, 0.34, 0.34, 0.02, 0.22, 0.74, 0.66, 0.65),,   ,,,,(0.43,0.18,0.63,0.29,0.03,0.24,0.86,0.07,0.58,0.55),,   ,,,,(0.66,0.75,0.01,0.94,0.72,0.77,0.20,0.66,0.81,0.52),   ,,,),   时间=fig  plt.figure (),   时间=window  fig.add_subplot (111),   行,,=,window.plot(开始),   #如果是参数是列表,则默认每次取列表中的一个元素,   #即度量[0],指标[1],……   def 更新(数据):   line.set_ydata才能(数据),   时间线,return 才能;   ani =, animation.FuncAnimation(无花果,更新,度规,,间隔=2 * 1000),   plt.show ()

<强>第三个例子:

import  numpy  as  np    得到matplotlib  import  pyplot  as  plt    得到matplotlib  import  animation    #,First  set  up ,,,,轴,以及从而plot  element  want 用animate 我方表示歉意   时间=fig  plt.figure (),   时间=ax  plt.axes (xlim=(0, 2), ylim=(2, 2)),   行,,=,ax.plot ([], [],, lw=2),   功能:#,initialization  plot 从而background  of  each  frame    def  init ():   line.set_data才能([],[]),   时间线,return 才能;   #,animation 函数只却;能够is  nbsp; sequentially    #,注意:,小姐:is  framenumber    def 动画(我):,   时间=x 才能;np.linspace (0,, 2,, 1000),   y 才能=,np.sin (2, *, np.pi  *, (x 作用;0.01,*,i)),   line.set_data才能(x, y),   时间线,return 才能;   #,call 从而,动画师只位块传输=True  means  only  re-draw 从而parts  that  have 改变了只   时间=anim  animation.FuncAnimation(图,动画,,init_func=init,,   ,,,,,,,,,,,,,,,=200,框架,间隔=20,,位块传输=True),   # anim.save (& # 39; basic_animation.mp4& # 39;,, fps=30, extra_args=[& # 39; -vcodec& # 39;,, & # 39; libx264& # 39;]),   plt.show ()

<强>第四个例子:

#,- *安康;编码:,utf-8  - *安康;   import  numpy  as  np    import  matplotlib.pyplot  as  plt    import  matplotlib.animation  as  animation    #,每次产生一个新的坐标点,   def  data_gen ():   时间=t 才能;data_gen.t    cnt 才能=0   while 才能cnt  & lt;, 1000:,   ,,,问+=1   ,,,t  0.05 +=,   ,,,油品收率t, np.sin (2 * np.pi * t), *, np.exp (- t/10。),   data_gen.t =0   #,绘图,   无花果,ax =, plt.subplots (),   行,,=,ax.plot ([], [],, lw=2),   ax.set_ylim (-1.1, 1.1),   ax.set_xlim (0,, 5),   ax.grid (),   xdata, ydata =, [], [],   #,因为运行的参数是调用函数data_gen,   #,所以第一个参数可以不是framenum:设置线的数据,返回line    def 运行(数据):   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

怎么用Python绘制几个动画