最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年08月12日 11:03:57   作者:初學(xué)小白Lu  
這篇文章主要介紹了Python?Matplotlib繪制動(dòng)圖平滑曲線(xiàn),文章圍繞主題展開(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:布爾類(lèi)型,是否嵌入幀。
    • 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à)文件名稱(chēng),如’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à)。 繪制平滑曲線(xiàn)

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() 高斯核類(lèi)繪制平滑曲線(xiàn)

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() 樣條插值類(lèi)繪制平滑曲線(xiàn)

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() 首先確定花鍵曲線(xiàn)的系數(shù),繪制出一條平滑的花鍵曲線(xiàn)。我們用給定的數(shù)據(jù)來(lái)估計(jì)花樣曲線(xiàn)的系數(shù),然后用系數(shù)來(lái)確定間隔緊密的 x 值的 y 值,使曲線(xiàn)平滑。繪制曲線(xiàn)需要沿 X 軸 1 到 7 之間間隔相等的 500。

使用 scipy.interpolate.interp1d 插值類(lèi)繪制平滑曲線(xiàn)

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()

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

擬合曲線(xià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)圖平滑曲線(xiàn)的文章就介紹到這了,更多相關(guān)Python Matplotlib繪制平滑曲線(xiàn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python3之字節(jié)串bytes與字節(jié)數(shù)組bytearray的使用詳解

    Python3之字節(jié)串bytes與字節(jié)數(shù)組bytearray的使用詳解

    今天小編就為大家分享一篇Python3之字節(jié)串bytes與字節(jié)數(shù)組bytearray的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • 可視化工具PyVista多線(xiàn)程顯示多窗口的實(shí)例代碼

    可視化工具PyVista多線(xiàn)程顯示多窗口的實(shí)例代碼

    這篇文章主要介紹了可視化工具PyVista多線(xiàn)程顯示多窗口,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • python的setattr函數(shù)實(shí)例用法

    python的setattr函數(shù)實(shí)例用法

    在本篇文章里小編給大家整理了一篇關(guān)于python的setattr函數(shù)實(shí)例用法的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)下。
    2020-12-12
  • Python+decimal完成精度計(jì)算的示例詳解

    Python+decimal完成精度計(jì)算的示例詳解

    在進(jìn)行小數(shù)計(jì)算的時(shí)候使用float,經(jīng)常會(huì)出現(xiàn)小數(shù)位不精確的情況。在python編程中,推薦使用decimal來(lái)完成小數(shù)位的精度計(jì)算。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)decimal的使用,需要的可以參考一下
    2022-10-10
  • YOLO?v5引入解耦頭部完整步驟

    YOLO?v5引入解耦頭部完整步驟

    網(wǎng)上有很多添加解耦頭的博客,在此記錄下我使用解耦頭對(duì)YOLOv5改進(jìn),下面這篇文章主要給大家介紹了關(guān)于YOLO?v5引入解耦頭部的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python實(shí)現(xiàn)驗(yàn)證碼識(shí)別功能

    python實(shí)現(xiàn)驗(yàn)證碼識(shí)別功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)驗(yàn)證碼識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • python學(xué)習(xí)與數(shù)據(jù)挖掘應(yīng)知應(yīng)會(huì)的十大終端命令

    python學(xué)習(xí)與數(shù)據(jù)挖掘應(yīng)知應(yīng)會(huì)的十大終端命令

    今天我們將介紹一些基本的數(shù)據(jù)收集、探索和聚合—所有這些都是通過(guò)shell完成的。如果你使用的是Linux或Mac,那么接下來(lái)就不會(huì)有任何問(wèn)題,但是Windows用戶(hù)應(yīng)該在繼續(xù)之前下載一個(gè)終端仿真器
    2021-11-11
  • Python集合set的交集和并集操作方法

    Python集合set的交集和并集操作方法

    這篇文章主要介紹了Python集合set的交集和并集操作方法小,python的set,是一個(gè)無(wú)序不重復(fù)元素集,?基本功能包括關(guān)系測(cè)試和消除重復(fù)元素本文講述了python中set集合的比較方法包括交集,并集,差集,下文更多詳細(xì)資料,需要的小伙伴可以參考一下
    2022-03-03
  • LyScript實(shí)現(xiàn)指令查詢(xún)功能的示例代碼

    LyScript實(shí)現(xiàn)指令查詢(xún)功能的示例代碼

    對(duì)LyScript自動(dòng)化插件進(jìn)行二次封裝,可以實(shí)現(xiàn)從內(nèi)存中讀入目標(biāo)進(jìn)程解碼后的機(jī)器碼。所以本文為大家介紹了如何實(shí)現(xiàn)LyScript指令查詢(xún)功能,需要的可以參考一下
    2022-09-09
  • 使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)

    使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)

    這篇文章介紹了使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

江安县| 辽阳县| 中山市| 九江市| 洛扎县| 武陟县| 山阳县| 雅安市| 揭西县| 博乐市| 茂名市| 荥阳市| 樟树市| 银川市| 凉山| 临西县| 雷州市| 巢湖市| 汉源县| 丘北县| 禄丰县| 桐乡市| 嘉祥县| 兴隆县| 丰宁| 广州市| 石楼县| 南开区| 玉门市| 田林县| 晋中市| 灌南县| 蓝山县| 彰化县| 抚宁县| 城步| 宁都县| 叙永县| 海兴县| 洛宁县| 绍兴县|