教育行業(yè)A股IPO第一股(股票代碼 003032)

全國咨詢/投訴熱線:400-618-4000

Python大數(shù)據(jù)培訓(xùn):ArtistAnimation類

更新時(shí)間:2022年05月10日11時(shí)07分 來源:傳智教育 瀏覽次數(shù):

  ArtistAnimation是基于一組Artist對(duì)象的動(dòng)畫類,它通過一幀一幀的數(shù)據(jù)制作動(dòng)畫。ArtistAnimation類的構(gòu)造方法的語法格式如下所示:

ArtistAnimation(fig, artists, interval, repeat_delay, repeat,
                blit, *args, **kwargs)

  該方法常用參數(shù)的含義如下。

  .fig:表示動(dòng)畫所在的畫布。

  .artists:表示一組Artist對(duì)象的列表。

  .interval:表示更新動(dòng)畫的頻率,以毫秒為單位,默認(rèn)為200.

  .repeat_delay:表示再次播放動(dòng)畫之前延遲的時(shí)長。

  .repeat:表示是否重復(fù)播放動(dòng)畫。

  下面使用ArtistAnimation類制作與上個(gè)示例相同的動(dòng)畫效果——移動(dòng)的正弦曲線,具體代碼如下。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
x = np.arange(0, 2*np.pi, 0.01)
fig, ax = plt.subplots()
arr = []
for i in range(5):
    line = ax.plot(x, np.sin(x+i))
    arr.append(line)
# 根據(jù)arr存儲(chǔ)的一組圖形創(chuàng)建動(dòng)畫
ani = ArtistAnimation(fig=fig, artists=arr, repeat=True)
plt.show()

  運(yùn)行以上程序,效果如圖7-5所示。

使用AristAnimation類制作移動(dòng)的正弦曲線1
使用AristAnimation類制作移動(dòng)的正弦曲線

  圖7-5 使用AristAnimation類制作移動(dòng)的正弦曲線

  需要說明的是,因?yàn)槊看卫L制的曲線都是一個(gè)新的圖形,所以每次曲線的顏色都是不同的。

  通過比較可以發(fā)現(xiàn),通過FuncAnimation類創(chuàng)建動(dòng)畫的方式更加靈活。

0 分享到:
和我們?cè)诰€交談!