亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?matplotlib包和gif包生成gif動畫實(shí)戰(zhàn)對比

 更新時(shí)間:2022年05月07日 11:57:07   作者:mighty13  
使用matplotlib生成gif動畫的方法相信大家應(yīng)該都看到過,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib包和gif包生成gif動畫對比的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

使用matplotlib生成gif動畫的方法有很多,一般常規(guī)使用matplotlib的animation模塊的FuncAnimation函數(shù)實(shí)現(xiàn)。

在matplotlib官網(wǎng)看到了第三方動畫包gif的介紹。

gif包概述

gif包是支持 Altair, matplotlib和Plotly的動畫擴(kuò)展。

gif依賴PIL,即pillow,要求Pillow>=7.1.2。

安裝gif包,pip install gif

動畫原理

所有動畫都是由幀(frame)構(gòu)成的,一幀就是一幅靜止的畫面,連續(xù)的幀就形成動畫。我們通常說幀數(shù),簡單地說,就是在1秒鐘時(shí)間里傳輸?shù)膱D片的幀數(shù),也可以理解為圖形處理器每秒鐘能夠刷新幾次,通常用fps(Frames Per Second)表示。

制作動畫的關(guān)鍵:如何生成幀,每秒多少幀。

gif包解讀

gif包非常簡潔,只有一個(gè)單獨(dú)的文件gif.py,文件主要包含options類、frames和save兩個(gè)函數(shù)。

options類

提供精簡版 的Altair, matplotlib和Plotly的保存或輸出設(shè)置。以matplotlib為例,提供以下設(shè)置。

  • dpi (int): The resolution in dots per inch
  • facecolor (colorspec): The facecolor of the figure
  • edgecolor (colorspec): The edgecolor of the figure
  • transparent (bool): If True, the axes patches will all be transparent

設(shè)置方法:gif.options.matplotlib["dpi"] = 300

原理:options在構(gòu)造函數(shù)中創(chuàng)建matplotlib字典保存配置,隨后傳遞給底層的matplotlib包。

frames函數(shù)

裝飾器函數(shù),通過對應(yīng)包編寫自定義繪圖函數(shù)生成單幀圖像。

save函數(shù)

根據(jù)幀序列生成動畫。

def save(frames, path, duration=100, unit="milliseconds", between="frames", loop=True):
    """Save decorated frames to an animated gif.
    - frames (list): collection of frames built with the gif.frame decorator
    - path (str): filename with relative/absolute path
    - duration (int/float): time (with reference to unit and between)
    - unit {"ms" or "milliseconds", "s" or "seconds"}: time unit value
    - between {"frames", "startend"}: duration between "frames" or the entire gif ("startend")
    - loop (bool): infinitely loop the animation

frames即根據(jù)@gif.frame裝飾的繪圖函數(shù)生成的幀的序列,此處根據(jù)需要自定義。

duration即持續(xù)時(shí)間,由單位unit和模式between決定,默認(rèn)為frames為幀間的時(shí)間間隔。

unit即持續(xù)時(shí)間單位,支持毫秒和秒,默認(rèn)為毫秒。

between即持續(xù)時(shí)間計(jì)算模式,默認(rèn)frames即duration為幀之間的時(shí)間間隔,startend模式時(shí)duration=duration /len(frames),即duration為所有幀—整個(gè)動畫的持續(xù)時(shí)間。

gif包生成gif動畫實(shí)踐

import random
from matplotlib import pyplot as plt
import gif

# 構(gòu)造數(shù)據(jù)
x = [random.randint(0, 100) for _ in range(100)]
y = [random.randint(0, 100) for _ in range(100)]
#設(shè)置選項(xiàng)
gif.options.matplotlib["dpi"] = 300
#使用gif.frame裝飾器構(gòu)造繪圖函數(shù),即如何生成靜態(tài)的幀
@gif.frame
def plot(i):
    xi = x[i*10:(i+1)*10]
    yi = y[i*10:(i+1)*10]
    plt.scatter(xi, yi)
    plt.xlim((0, 100))
    plt.ylim((0, 100))
# 構(gòu)造幀序列frames,即把生成動畫的所有幀按順序放在列表中
frames = []
for i in range(10):
    frame = plot(i)
    frames.append(frame)
# 根據(jù)幀序列frames,動畫持續(xù)時(shí)間duration,生成gif動畫
gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")

以心形曲線為例比較gif包和animation模塊實(shí)現(xiàn)動畫的差異

gif包的實(shí)現(xiàn)方式

import numpy as np
import gif
from matplotlib import pyplot as plt

t = np.linspace(0, 6, 100)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)

@gif.frame
def plot_love(x, y):
    plt.figure(figsize=(5, 3), dpi=100)
    plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$")
    plt.axis("off")
    
frames = []
for i in range(1, len(x)):
    of = plot_love(x[:i], y[:i])
    frames.append(of)

gif.save(frames, "love.gif", duration=80)

matplotlib 常規(guī)FuncAnimation函數(shù)實(shí)現(xiàn)方式

from matplotlib import pyplot as plt
import matplotlib.animation as animation
import numpy as np

t = np.linspace(0, 6, 100)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
data=[i for i in zip(x,y)]

def plot_love(data):
    x, y = data
    plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$")

fig=plt.figure(figsize=(5, 3), dpi=100)
plt.axis("off")
animator = animation.FuncAnimation(fig, plot_love, frames=data, interval=80)
animator.save("love.gif", writer='pillow')

matplotlib底層PillowWriter類實(shí)現(xiàn)方式

from matplotlib import pyplot as plt
import matplotlib.animation as animation
import numpy as np

t = np.linspace(0, 6, 100)
x = 16 * np.sin(t) ** 3
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)

def plot_love(x,y):
    plt.scatter(x, y, 60, c="r", alpha=0.7, marker=r"$\heartsuit$")

fig=plt.figure(figsize=(5, 3), dpi=100)
plt.axis("off")

writer = animation.PillowWriter(fps=15)
with writer.saving(fig, "love1.gif"):
    for i in range(1, len(x)):
        of = plot_love(x[i], y[i])
        writer.grab_frame()

比較結(jié)果

通過比較可知gif包的實(shí)現(xiàn)方式和matplotlib中利用PillowWriter實(shí)現(xiàn)方式類似,更偏底層一些,這樣遇到比較復(fù)雜的繪圖時(shí)更靈活。

總結(jié)

到此這篇關(guān)于Python matplotlib包和gif包生成gif動畫實(shí)戰(zhàn)對比的文章就介紹到這了,更多相關(guān)matplotlib包和gif包生成gif內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程

    Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程

    這篇文章主要介紹了Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 詳解Flask框架中Flask-Login模塊的使用

    詳解Flask框架中Flask-Login模塊的使用

    Flask-Login 是一個(gè) Flask 模塊,可以為 Flask 應(yīng)用程序提供用戶登錄功能。這篇文章將通過一些示例為大家介紹一下Flask-Login模塊的使用,需要的可以參考一下
    2023-01-01
  • python動態(tài)加載變量示例分享

    python動態(tài)加載變量示例分享

    這篇文章主要介紹了python動態(tài)加載變量示例,需要的朋友可以參考下
    2014-02-02
  • Python中的logging模塊實(shí)現(xiàn)日志打印

    Python中的logging模塊實(shí)現(xiàn)日志打印

    這篇文章主要介紹了Python中的logging模塊實(shí)現(xiàn)日志打印,其實(shí)不止print打印日志方便排查問題,Python自帶的logging模塊,也可以很簡單就能實(shí)現(xiàn)日志的配置和打印,下面來看看具體的實(shí)現(xiàn)過程吧,需要的朋友可以參考一下
    2022-03-03
  • django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解

    django文檔學(xué)習(xí)之a(chǎn)pplications使用詳解

    這篇文章主要介紹了Python文檔學(xué)習(xí)之a(chǎn)pplications使用詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • pygame實(shí)現(xiàn)貪吃蛇游戲

    pygame實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了pygame實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python用pyecharts畫矩形樹圖實(shí)例

    python用pyecharts畫矩形樹圖實(shí)例

    大家好,本篇文章主要講的是python用pyecharts畫矩形樹圖實(shí)例,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Python詳細(xì)講解淺拷貝與深拷貝的使用

    Python詳細(xì)講解淺拷貝與深拷貝的使用

    這篇文章主要介紹了Python中的深拷貝和淺拷貝,通過講解Python中的淺拷貝和深拷貝的概念和背后的原理展開全文,需要的小伙伴可以參考一下
    2022-07-07
  • python爬蟲基本知識

    python爬蟲基本知識

    最近在做一個(gè)項(xiàng)目,這個(gè)項(xiàng)目需要使用網(wǎng)絡(luò)爬蟲從特定網(wǎng)站上爬取數(shù)據(jù),于是乎,我打算寫一個(gè)爬蟲系列的文章,與大家分享如何編寫一個(gè)爬蟲。下面這篇文章給大家介紹了python爬蟲基本知識,感興趣的朋友一起看看吧
    2018-03-03
  • python中numpy 數(shù)組過濾詳解

    python中numpy 數(shù)組過濾詳解

    這篇文章主要介紹了python中numpy 數(shù)組過濾詳解的相關(guān)資料,需要的朋友可以參考下
    2023-06-06

最新評論