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

Python?Matplotlib繪制動圖平滑曲線

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

繪制動圖

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

FuncAnimation

通過反復調(diào)用同一函數(shù)來制作動畫。

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

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

參數(shù):

  • fig:Figure。用于顯示動畫的figure對象
  • func:callable。用于更新每幀動畫的函數(shù)。func函數(shù)的第一個參數(shù)為幀序號。返回被更新后的圖形對象列表。
  • frames:iterable, int, generator function, or None, optional。動畫長度,幀序號組成的列表
  • init_func:callable, optional。自定義開始幀,即繪制初始化圖形的初始化函數(shù)
  • fargs:tuple or None, optional。額外的需要傳遞給func函數(shù)的參數(shù)。
  • save_count:int, default: 100。保存計數(shù)
  • cache_frame_data:bool, default: Trueinterval:int, default: 200。重復調(diào)用功能函數(shù)的間隔時間,單位是毫秒。
  • repeat_delay:int, default: 0。當repeat為True時,動畫延遲多少毫秒再循環(huán)。
  • repeat:bool, default: True。是否是循環(huán)動畫。
  • blit:bool, default: False。選擇更新所有點,還是僅更新產(chǎ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    #直接引用全局變量,也可以通過函數(shù)的frames或fargs參數(shù)傳遞。
    t+=0.1
    y=np.sin(t)
    line[0].set_ydata(y)
    return line

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

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對象實例,或者表示這些對象的字符串(‘ffmpeg’, ‘imagemagick’,‘avconv’)
    • fps:每秒的幀數(shù)
  • to_jshtml([fps, embed_frames, default_mode])返回js動畫,用base64文本編碼。
    • fps:每秒幀數(shù),默認根據(jù)動畫的interval確定。
    • embed_frames:布爾類型,是否嵌入幀。
    • default_mode:‘loop’,‘once’或者’reflect’

ArtistAnimation

通過調(diào)用一個固定的Artist對象來制作動畫,例如給定的系列圖片或者matplotlib的繪圖對象.。

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

參數(shù):

  • fig:Figure
  • artists:list
  • interval:int, default: 200。每一幀之間的間隔
  • repeat_delay:int,
  • default: 0。每顯示一次動畫后間隔多長時間重復
  • repeat:bool, default: True。是否重復動畫
  • 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) #繪制動畫
#ani.save("animate_artists_basic.gif")  #保存動畫
plt.show() #顯示動畫

方法:

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

參數(shù):

  • filename:保存的動畫文件名稱,如’mov.gif’,‘mov.mp4’。
  • writer:保持動畫的庫。MoviewWriter對象或者字符串。默認值’ffmpeg’。

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

  • fps:每秒幀數(shù),默認根據(jù)動畫的interval確定
  • dpi:每英寸點數(shù),默認和figure相同。可以控制動畫大小尺寸。
  • codec:編碼格式,默認’h264’
to_html5_video([embed_limit])

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

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

它通過使用 scipy.interpolate.make_interp_spline() 首先確定花鍵曲線的系數(shù),繪制出一條平滑的花鍵曲線。我們用給定的數(shù)據(jù)來估計花樣曲線的系數(shù),然后用系數(shù)來確定間隔緊密的 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()

繪制曲線時,需要在 X 軸上 1 和 7 之間取間隔相等的 500 個點。

擬合曲線后繪制動圖

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

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

相關文章

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

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

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

    可視化工具PyVista多線程顯示多窗口的實例代碼

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

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

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

    Python+decimal完成精度計算的示例詳解

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

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

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

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

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

    python學習與數(shù)據(jù)挖掘應知應會的十大終端命令

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

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

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

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

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

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

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

最新評論

京山县| 义马市| 高淳县| 那曲县| 乳山市| 姜堰市| 瑞丽市| 灵山县| 明光市| 正定县| 宁陵县| 大渡口区| 诏安县| 福泉市| 焉耆| 拜城县| 高清| 巴林右旗| 扬中市| 周宁县| 济宁市| 绵阳市| 红河县| 威信县| 理塘县| 新民市| 昌平区| 武穴市| 福安市| 长春市| 永清县| 古浪县| 祁阳县| 巴彦县| 神农架林区| 桂林市| 会昌县| 乌鲁木齐县| 延寿县| 苍山县| 乐清市|