国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內容

python如何制作動態(tài)畫面?

極客小將2020-12-20-
簡介python動圖的核心函數(shù)是matplotlib.animation.FuncAnimation,基本用法是:anim = animation.funcanimation(fig, animate, init_func=init, frames=10

bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

python動圖的核心函數(shù)是matplotlib.animation.FuncAnimation,基本用法是:bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

anim = animation.funcanimation(fig, animate, init_func=init, frames=100, interval=20, blit=true) # fig: 是我們創(chuàng)建的畫布 # animat: 是重點,是我們每個時刻要更新圖形對象的函數(shù),返回值和init_func相同 # init_func: 初始化函數(shù),其返回值就是每次都要更新的對象, #    告訴FuncAnimation在不同時刻要更新哪些圖形對象 # frames: 相當于時刻t,要模擬多少幀圖畫,不同時刻的t相當于animat的參數(shù) # interval: 刷新頻率,毫秒 # blit: blit是一個非常重要的關鍵字,它告訴動畫只重繪修改的部分,結合上面保存的時間, #    blit=true會使動畫顯示得會非常非常快

示例:
bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation  # 動圖的核心函數(shù) import seaborn as sns  # 美化圖形的一個繪圖包 sns.set_style("whitegrid")  # 設置圖形主圖 # 創(chuàng)建畫布 fig, ax = plt.subplots() fig.set_tight_layout(True) # 畫出一個維持不變(不會被重畫)的散點圖和一開始的那條直線。 x = np.arange(0, 20, 0.1) ax.scatter(x, x + np.random.normal(0, 3.0, len(x))) line, = ax.plot(x, x - 5, 'r-', linewidth=2) def update(i):     label = 'timestep {0}'.format(i)     print(label)     # 更新直線和x軸(用一個新的x軸的標簽)。     # 用元組(Tuple)的形式返回在這一幀要被重新繪圖的物體     line.set_ydata(x - 5 + i)  # 這里是重點,更新y軸的數(shù)據(jù)     ax.set_xlabel(label)    # 這里是重點,更新x軸的標簽     return line, ax # FuncAnimation 會在每一幀都調用“update” 函數(shù)。 # 在這里設置一個10幀的動畫,每幀之間間隔200毫秒 anim = FuncAnimation(fig, update, frames=np.arange(0, 10), interval=200)

生成GIF動圖:bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

安裝imageio庫;接著如下代碼中的注釋,首先讀取靜態(tài)圖到列表,作為GIF的每一幀;接著設置輸入(靜態(tài)圖)、輸出(動態(tài)圖)和一些必要參數(shù),我們這里設置每一幀間隔時間為1.5秒,默認是1秒,之后調用miageio.mimsave函數(shù)去保存結果。bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

''' Author: Solarzhou Email: tzhou@foxmail.com 公眾號:Solarzhou ''' import imageio def create_gif(image_list, gif_name, duration = 1.0):     '''     :param image_list: 這個列表用于存放生成動圖的圖片     :param gif_name: 字符串,所生成gif文件名,帶.gif后綴     :param duration: 圖像間隔時間     :return:     '''     frames = []     for image_name in image_list:         frames.append(imageio.imread(image_name))     imageio.mimsave(gif_name, frames, 'GIF', duration=duration)     return def main():     #這里放上自己所需要合成的圖片     image_list = ['1.jpg', '2.jpg', '3.jpg']     gif_name = 'new.gif'     duration = 1.5     create_gif(image_list, gif_name, duration) if __name__ == '__main__':     main()

更多Python知識請關注Python視頻教程欄目。bl5少兒編程網(wǎng)-Scratch_Python_教程_免費兒童編程學習平臺

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 新昌县| 包头市| 沙田区| 突泉县| 南汇区| 海城市| 兰州市| 济源市| 滁州市| 亚东县| 洪湖市| 宣城市| 台前县| 新泰市| 青神县| 志丹县| 天台县| 横山县| 舟山市| 平度市| 大埔区| 正蓝旗| 河源市| 天等县| 东至县| 锦州市| 姜堰市| 潮安县| 轮台县| 普兰县| 北宁市| 喀喇沁旗| 桦南县| 和林格尔县| 襄城县| 乐山市| 萨迦县| 蕉岭县| 新乡市| 全南县| 河池市|