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

詳解如何在Matplotlib中繪制平滑曲線

 更新時間:2024年04月22日 09:19:10   作者:python收藏家  
這篇文章主要為大家詳細介紹了如何在Matplotlib中繪制平滑曲線,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下

很多時候,我們有從非常分散的數(shù)據(jù)列表中生成的線圖,這使得圖形看起來像連接點的直線,或者非常密集,這導(dǎo)致數(shù)據(jù)點彼此非常接近,因此圖看起來很混亂。

默認情況下,matplotlib.pyplot.plot()函數(shù)通過用直線連接數(shù)據(jù)中的兩個相鄰點來生成曲線,因此matplotlib.pyplot.plot()函數(shù)不會為小范圍的數(shù)據(jù)點生成平滑曲線。

示例:

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ù)不遵循平滑的直線,因此該圖一點也不平滑。為了繪制一條平滑的曲線,我們首先將一條樣條曲線擬合到曲線上,并使用該曲線來找到x值的y值,x值被一個無限小的間隙隔開。我們可以通過用一個非常小的間隙畫出這些點來得到一條光滑的曲線。

我們可以使用以下方法來創(chuàng)建此數(shù)據(jù)集的平滑曲線:

1.使用PyPlot繪制平滑曲線:

它通過首先使用scipy.interpolate.make_interp_spline()確定樣條曲線的系數(shù)來繪制平滑的樣條曲線。我們使用給定的數(shù)據(jù)點來估計樣條曲線的系數(shù),然后使用這些系數(shù)來確定非常接近的x值的y值,以使曲線看起來平滑。這里我們將使用np.linspace()方法,它返回在指定時間間隔內(nèi)計算的均勻間隔的樣本??蛇x參數(shù)num是在start和stop范圍內(nèi)生成的樣本數(shù)。默認值為50,且必須為非負數(shù)。我們希望該參數(shù)具有足夠高的值以生成平滑曲線。讓我們在最小值和最大值之間沿X軸沿著取500個等距樣本來繪制曲線。

語法:

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類生成一條三次插值曲線,然后我們使用該曲線來確定平滑曲線的密集x值的y值。這里我們也將使用np.linspace()方法,該方法返回在指定時間間隔內(nèi)計算的均勻間隔的樣本。讓我們在最小值和最大值之間沿X軸沿著取500個等距樣本來繪制曲線。根據(jù)您希望直線彎曲的程度,可以修改第三個參數(shù)(num)的值。

語法:

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

浦城县| 响水县| 阿坝县| 库尔勒市| 定日县| 南江县| 慈利县| 贵溪市| 嵩明县| 介休市| 当阳市| 博罗县| 凯里市| 淮安市| 黄浦区| 周宁县| 河池市| 巴青县| 四会市| 江川县| 同德县| 娄烦县| 化隆| 涡阳县| 崇明县| 伊金霍洛旗| 大埔县| 永济市| 扎兰屯市| 调兵山市| 满城县| 衢州市| 建湖县| 佛教| 甘泉县| 彭州市| 沅江市| 兴和县| 太康县| 彭水| 泰兴市|