詳解如何在Matplotlib中繪制平滑曲線
很多時(shí)候,我們有從非常分散的數(shù)據(jù)列表中生成的線圖,這使得圖形看起來(lái)像連接點(diǎn)的直線,或者非常密集,這導(dǎo)致數(shù)據(jù)點(diǎn)彼此非常接近,因此圖看起來(lái)很混亂。
默認(rèn)情況下,matplotlib.pyplot.plot()函數(shù)通過(guò)用直線連接數(shù)據(jù)中的兩個(gè)相鄰點(diǎn)來(lái)生成曲線,因此matplotlib.pyplot.plot()函數(shù)不會(huì)為小范圍的數(shù)據(jù)點(diǎn)生成平滑曲線。
示例:
import numpy as np import matplotlib.pyplot as plt # Dataset x = np.array([ 1, 2, 3, 4, 5, 6, 7, 8 ]) y = np.array([ 20, 30, 5, 12, 39, 48, 50, 3 ]) # Plotting the Graph plt.plot(x, y) plt.title("Curve plotted using the given points") plt.xlabel("X") plt.ylabel("Y") plt.show()
可以看到,由于底層數(shù)據(jù)不遵循平滑的直線,因此該圖一點(diǎn)也不平滑。為了繪制一條平滑的曲線,我們首先將一條樣條曲線擬合到曲線上,并使用該曲線來(lái)找到x值的y值,x值被一個(gè)無(wú)限小的間隙隔開(kāi)。我們可以通過(guò)用一個(gè)非常小的間隙畫(huà)出這些點(diǎn)來(lái)得到一條光滑的曲線。
我們可以使用以下方法來(lái)創(chuàng)建此數(shù)據(jù)集的平滑曲線:
1.使用PyPlot繪制平滑曲線:
它通過(guò)首先使用scipy.interpolate.make_interp_spline()確定樣條曲線的系數(shù)來(lái)繪制平滑的樣條曲線。我們使用給定的數(shù)據(jù)點(diǎn)來(lái)估計(jì)樣條曲線的系數(shù),然后使用這些系數(shù)來(lái)確定非常接近的x值的y值,以使曲線看起來(lái)平滑。這里我們將使用np.linspace()方法,它返回在指定時(shí)間間隔內(nèi)計(jì)算的均勻間隔的樣本。可選參數(shù)num是在start和stop范圍內(nèi)生成的樣本數(shù)。默認(rèn)值為50,且必須為非負(fù)數(shù)。我們希望該參數(shù)具有足夠高的值以生成平滑曲線。讓我們?cè)谧钚≈岛妥畲笾抵g沿X軸沿著取500個(gè)等距樣本來(lái)繪制曲線。
語(yǔ)法:
numpy.linspace(start, stop, num = 50, endpoint = True, retstep =
False, dtype = None, axis = 0)
X_Y_Spline = scipy.interpolate.make_interp_spline(x, y)
import numpy as np import numpy as np from scipy.interpolate import make_interp_spline import matplotlib.pyplot as plt # Dataset x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) y = np.array([20, 30, 5, 12, 39, 48, 50, 3]) X_Y_Spline = make_interp_spline(x, y) # Returns evenly spaced numbers # over a specified interval. X_ = np.linspace(x.min(), x.max(), 500) Y_ = X_Y_Spline(X_) # Plotting the Graph plt.plot(X_, Y_) plt.title("Plot Smooth Curve Using the scipy.interpolate.make_interp_spline() Class") plt.xlabel("X") plt.ylabel("Y") plt.show()
2. 三次插值樣條曲線
它使用scipy.interpolate.interp1d類(lèi)生成一條三次插值曲線,然后我們使用該曲線來(lái)確定平滑曲線的密集x值的y值。這里我們也將使用np.linspace()方法,該方法返回在指定時(shí)間間隔內(nèi)計(jì)算的均勻間隔的樣本。讓我們?cè)谧钚≈岛妥畲笾抵g沿X軸沿著取500個(gè)等距樣本來(lái)繪制曲線。根據(jù)您希望直線彎曲的程度,可以修改第三個(gè)參數(shù)(num)的值。
語(yǔ)法:
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False,
dtype=None, axis=0)
cubic_interpolation_model=scipy.interpolate.interp1d(x,y,kind=”cubic”)
import numpy as np from scipy.interpolate import interp1d import matplotlib.pyplot as plt # Dataset x=np.array([1, 2, 3, 4, 5, 6, 7, 8]) y=np.array([20, 30, 5, 12, 39, 48, 50, 3]) cubic_interpolation_model = interp1d(x, y, kind = "cubic") # Plotting the Graph X_=np.linspace(x.min(), x.max(), 500) Y_=cubic_interpolation_model(X_) plt.plot(X_, Y_) plt.title("Plot Smooth Curve Using the scipy.interpolate.interp1d Class") plt.xlabel("X") plt.ylabel("Y") plt.show()
到此這篇關(guān)于詳解如何在Matplotlib中繪制平滑曲線的文章就介紹到這了,更多相關(guān)Matplotlib繪制平滑曲線內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python tools實(shí)現(xiàn)視頻的每一幀提取并保存
這篇文章主要為大家詳細(xì)介紹了python tools實(shí)現(xiàn)視頻的每一幀提取并保存,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Python多進(jìn)程通信Queue、Pipe、Value、Array實(shí)例
這篇文章主要介紹了Python多進(jìn)程通信Queue、Pipe、Value、Array實(shí)例,queue和pipe用來(lái)在進(jìn)程間傳遞消息、Value + Array 是python中共享內(nèi)存映射文件的方法,需要的朋友可以參考下2014-11-11python實(shí)現(xiàn)人機(jī)對(duì)戰(zhàn)的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)人機(jī)對(duì)戰(zhàn)的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04