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

Python?Matplotlib繪制動(dòng)圖平滑曲線

 更新時(shí)間:2022年08月12日 11:03:57   作者:初學(xué)小白Lu  
這篇文章主要介紹了Python?Matplotlib繪制動(dòng)圖平滑曲線,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考一下,需要的小伙伴可以參考一下

繪制動(dòng)圖

FuncAnimation,它的使用要求簡(jiǎn)潔且定制化程度較高。如果想將很多圖片合并為一個(gè)動(dòng)圖,那么ArtistAnimation是最合適的選擇。

FuncAnimation

通過(guò)反復(fù)調(diào)用同一函數(shù)來(lái)制作動(dòng)畫(huà)。

注意:創(chuàng)建FuncAnimation對(duì)象后一定要將其賦值給某個(gè)變量,否則系統(tǒng)會(huì)將其進(jìn)行垃圾回收。

class matplotlib.animation.FuncAnimation(fig, 
										func, 
										frames=None, 
										init_func=None, 
										fargs=None, 
										save_count=None, *, 
										cache_frame_data=True, 
										**kwargs)

參數(shù):

  • fig:Figure。用于顯示動(dòng)畫(huà)的figure對(duì)象
  • func:callable。用于更新每幀動(dòng)畫(huà)的函數(shù)。func函數(shù)的第一個(gè)參數(shù)為幀序號(hào)。返回被更新后的圖形對(duì)象列表。
  • frames:iterable, int, generator function, or None, optional。動(dòng)畫(huà)長(zhǎng)度,幀序號(hào)組成的列表
  • init_func:callable, optional。自定義開(kāi)始幀,即繪制初始化圖形的初始化函數(shù)
  • fargs:tuple or None, optional。額外的需要傳遞給func函數(shù)的參數(shù)。
  • save_count:int, default: 100。保存計(jì)數(shù)
  • cache_frame_data:bool, default: Trueinterval:int, default: 200。重復(fù)調(diào)用功能函數(shù)的間隔時(shí)間,單位是毫秒。
  • repeat_delay:int, default: 0。當(dāng)repeat為T(mén)rue時(shí),動(dòng)畫(huà)延遲多少毫秒再循環(huán)。
  • repeat:bool, default: True。是否是循環(huán)動(dòng)畫(huà)。
  • blit:bool, default: False。選擇更新所有點(diǎn),還是僅更新產(chǎn)生變化的點(diǎn)。

示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig = plt.figure()
ax = fig.subplots()

t=np.linspace(0,10,100)
y=np.sin(t)
ax.set_aspect(3)
ax.plot(t,y,'--',c='gray')
line=ax.plot(t,y,c='C2')

def update(i):  #幀更新函數(shù)
    global t    #直接引用全局變量,也可以通過(guò)函數(shù)的frames或fargs參數(shù)傳遞。
    t+=0.1
    y=np.sin(t)
    line[0].set_ydata(y)
    return line

ani=FuncAnimation(fig,update,interval=100) #繪制動(dòng)畫(huà)
plt.show() #顯示動(dòng)畫(huà)

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

fig = plt.figure(figsize=(7, 2), dpi=100)
ax = plt.subplot()
X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C, S = np.cos(X), np.sin(X)
line1, = ax.plot(X, C, marker="o", markevery=[-1], markeredgecolor="white")
line2, = ax.plot(X, S, marker="o", markevery=[-1], markeredgecolor="white")

def update(frame):
    line1.set_data(X[:frame], C[:frame])
    line2.set_data(X[:frame], S[:frame])

ani = animation.FuncAnimation(fig, update, interval=10)
plt.show()

方法 init(fig, func[, frames, init_func, …])

  • new_frame_seq()
  • new_saved_frame_seq()
  • pause()
  • resume()
  • save(self, filename, writer=None, fps=None, dpi=None, codec=None, bitrate=None, extra_args=None, metadata=None, extra_anim=None, savefig_kwargs=None)
    • filename:保存的文件名
    • writer:FFMpegFileWriter,ImageMagickFileWriter, AVConvFileWriter對(duì)象實(shí)例,或者表示這些對(duì)象的字符串(‘ffmpeg’, ‘imagemagick’,‘avconv’)
    • fps:每秒的幀數(shù)
  • to_jshtml([fps, embed_frames, default_mode])返回js動(dòng)畫(huà),用base64文本編碼。
    • fps:每秒幀數(shù),默認(rèn)根據(jù)動(dòng)畫(huà)的interval確定。
    • embed_frames:布爾類型,是否嵌入幀。
    • default_mode:‘loop’,‘once’或者’reflect’

ArtistAnimation

通過(guò)調(diào)用一個(gè)固定的Artist對(duì)象來(lái)制作動(dòng)畫(huà),例如給定的系列圖片或者matplotlib的繪圖對(duì)象.。

class matplotlib.animation.ArtistAnimation(fig, artists, *args, **kwargs)

參數(shù):

  • fig:Figure
  • artists:list
  • interval:int, default: 200。每一幀之間的間隔
  • repeat_delay:int,
  • default: 0。每顯示一次動(dòng)畫(huà)后間隔多長(zhǎng)時(shí)間重復(fù)
  • repeat:bool, default: True。是否重復(fù)動(dòng)畫(huà)
  • blit:bool, default: False

示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import ArtistAnimation
fig = plt.figure()
ax = fig.subplots()

arts=[]
t=np.linspace(0,np.pi*2,20)
for i in range(20):
    t+=np.pi*2/20
    y=np.sin(t)
    lines=ax.plot(y,'--',c='gray')  #繪制一幀圖形
    arts.append(lines)              #每幀圖形都保存到列表中

ani=ArtistAnimation(fig,arts,interval=200) #繪制動(dòng)畫(huà)
#ani.save("animate_artists_basic.gif")  #保存動(dòng)畫(huà)
plt.show() #顯示動(dòng)畫(huà)

方法:

__init__(fig, artists, *args, **kwargs)
new_frame_seq()
new_saved_frame_seq()
pause()
resume()
save(filename[, writer, fps, dpi, codec, ...])

參數(shù):

  • filename:保存的動(dòng)畫(huà)文件名稱,如’mov.gif’,‘mov.mp4’。
  • writer:保持動(dòng)畫(huà)的庫(kù)。MoviewWriter對(duì)象或者字符串。默認(rèn)值’ffmpeg’。

“pillow”:PillowWriter,用pillow庫(kù)寫(xiě)如動(dòng)畫(huà)文件。
“ffmpeg”:FFMpegWriter,‎基于ffmpeg庫(kù)寫(xiě)動(dòng)畫(huà)。
“ffmpeg_file”:FFMpegFileWriter,基于文件的FFMpegWriter,用ffmpeg庫(kù)把幀寫(xiě)入臨時(shí)文件,然后拼接成動(dòng)畫(huà)。
“imagemagick”:ImageMagickWriter,‎基于管道的動(dòng)畫(huà)GIF。‎幀通過(guò)管道傳輸?shù)絀mageMagick并寫(xiě)入文件。
“imagemagick_file”:基于文件的imagemagick寫(xiě)動(dòng)畫(huà)。
“hmtl”:HTMLWriter,基于javascript html的動(dòng)畫(huà)。

  • fps:每秒幀數(shù),默認(rèn)根據(jù)動(dòng)畫(huà)的interval確定
  • dpi:每英寸點(diǎn)數(shù),默認(rèn)和figure相同??梢钥刂苿?dòng)畫(huà)大小尺寸。
  • codec:編碼格式,默認(rèn)’h264’
to_html5_video([embed_limit])

embed_limit:動(dòng)畫(huà)文件大小限制,單位為MB。默認(rèn)為20MB,超出限制則不創(chuàng)建動(dòng)畫(huà)。 繪制平滑曲線

import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([100, 50, 25, 12.5, 6.25, 3.125, 1.5625])

plt.plot(x, y)
plt.title("Spline Curve")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

使用 scipy.ndimage.gaussian_filter1d() 高斯核類繪制平滑曲線

import numpy as np
import matplotlib.pyplot as plt 
from scipy.ndimage import gaussian_filter1d

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])
y_smoothed = gaussian_filter1d(y, sigma=5)

plt.plot(x, y_smoothed)
plt.title("Spline Curve Using the Gaussian Smoothing")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

使用 scipy.interpolate.make_interp_spline() 樣條插值類繪制平滑曲線

import numpy as np
from scipy.interpolate import make_interp_spline
import matplotlib.pyplot as plt 
x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])
model=make_interp_spline(x, y)
xs=np.linspace(1,7,500)
ys=model(xs)
plt.plot(xs, ys)
plt.title("Smooth Spline Curve")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

它通過(guò)使用 scipy.interpolate.make_interp_spline() 首先確定花鍵曲線的系數(shù),繪制出一條平滑的花鍵曲線。我們用給定的數(shù)據(jù)來(lái)估計(jì)花樣曲線的系數(shù),然后用系數(shù)來(lái)確定間隔緊密的 x 值的 y 值,使曲線平滑。繪制曲線需要沿 X 軸 1 到 7 之間間隔相等的 500。

使用 scipy.interpolate.interp1d 插值類繪制平滑曲線

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

x=np.array([1,2,3,4,5,6,7])
y=np.array([100,50,25,12.5,6.25,3.125,1.5625])

cubic_interploation_model=interp1d(x,y,kind="cubic")
xs=np.linspace(1,7,500)
ys=cubic_interploation_model(xs)
plt.plot(xs, ys)
plt.title("Spline Curve Using Cubic Interpolation")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()

繪制曲線時(shí),需要在 X 軸上 1 和 7 之間取間隔相等的 500 個(gè)點(diǎn)。

擬合曲線后繪制動(dòng)圖

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy.interpolate import interp1d

fig = plt.figure(figsize=(7, 2), dpi=100)
ax = plt.subplot()
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([100, 50, 25, 12.5, 6.25, 3.125, 1.5625])
cubic_interploation_model = interp1d(x, y, kind="cubic")
xs = np.linspace(1, 7, 500)
ys = cubic_interploation_model(xs)
line3 = ax.plot(xs, ys)
def update(frame):
    line3[0].set_data(xs[:frame], ys[:frame])
ani = animation.FuncAnimation(fig, update, interval=10)
plt.show()

到此這篇關(guān)于Python Matplotlib繪制動(dòng)圖平滑曲線的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制平滑曲線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python全面分析系統(tǒng)的時(shí)域特性和頻率域特性

    Python全面分析系統(tǒng)的時(shí)域特性和頻率域特性

    今天小編就為大家分享一篇Python全面分析系統(tǒng)的時(shí)域特性和頻率域特性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python爬蟲(chóng)爬取快手視頻多線程下載功能

    python爬蟲(chóng)爬取快手視頻多線程下載功能

    這篇文章主要介紹了python爬蟲(chóng)爬取快手視頻多線程下載功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Python自動(dòng)化辦公之文件整理腳本分享

    Python自動(dòng)化辦公之文件整理腳本分享

    這篇文章主要為大家分享了一個(gè)Python自動(dòng)化辦公腳本,可以實(shí)現(xiàn)文件整理,這是一個(gè)很有用的技能,可以幫助你管理你的電腦上的各種文件,需要的可以收藏一下
    2023-08-08
  • python 實(shí)現(xiàn)自動(dòng)遠(yuǎn)程登陸scp文件實(shí)例代碼

    python 實(shí)現(xiàn)自動(dòng)遠(yuǎn)程登陸scp文件實(shí)例代碼

    這篇文章主要介紹了python 實(shí)現(xiàn)自動(dòng)遠(yuǎn)程登陸scp文件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • python excel和yaml文件的讀取封裝

    python excel和yaml文件的讀取封裝

    這篇文章主要介紹了python excel和yaml文件的讀取封裝,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2021-01-01
  • 基于Python實(shí)現(xiàn)骰子小游戲

    基于Python實(shí)現(xiàn)骰子小游戲

    骰子,是現(xiàn)在娛樂(lè)場(chǎng)所最常見(jiàn)的一種玩樂(lè)項(xiàng)目。一般骰子分兩人和兩人以上玩,而玩法有很多。本文就來(lái)用Python實(shí)現(xiàn)個(gè)骰子小游戲,感興趣的可以了解一下
    2023-02-02
  • Python中使用PDB庫(kù)調(diào)試程序

    Python中使用PDB庫(kù)調(diào)試程序

    這篇文章主要介紹了Python中使用PDB庫(kù)調(diào)試程序,本文講解了使用PDB的二種模式以及PDB模式下的常用調(diào)試命令,需要的朋友可以參考下
    2015-04-04
  • python讀取json數(shù)據(jù)還原表格批量轉(zhuǎn)換成html

    python讀取json數(shù)據(jù)還原表格批量轉(zhuǎn)換成html

    這篇文章主要介紹了python讀取json數(shù)據(jù)還原表格批量轉(zhuǎn)換成html,由于需要對(duì)ocr識(shí)別系統(tǒng)的表格識(shí)別結(jié)果做驗(yàn)證,通過(guò)返回的json文件結(jié)果對(duì)比比較麻煩,故需要將json文件里面的識(shí)別結(jié)果還原為表格做驗(yàn)證,下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下
    2022-03-03
  • Python爬蟲(chóng)基礎(chǔ)之爬蟲(chóng)的分類知識(shí)總結(jié)

    Python爬蟲(chóng)基礎(chǔ)之爬蟲(chóng)的分類知識(shí)總結(jié)

    來(lái)給大家講python爬蟲(chóng)的基礎(chǔ)啦,首先我們從爬蟲(chóng)的分類開(kāi)始講起,下文有非常詳細(xì)的知識(shí)總結(jié),對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Python編程中的異常處理教程

    Python編程中的異常處理教程

    這篇文章主要介紹了Python編程中的異常處理教程,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-08-08

最新評(píng)論