在python中,如何设置fit函数范围以仅在几个数据点之间拟合函数

在python中,如何设置fit函数范围以仅在几个数据点之间拟合函数,python,matplotlib,scipy,Python,Matplotlib,Scipy,我有一个1-100之间的数据点,我只想拟合数据点30-50之间的高斯函数。那么如何在python中设置拟合范围30-50。可以使用切片表示法 l_ = list(range(1, 100)) start = 30 stop = 50 print (l_[start-1:stop]) 切片Python列表/数组 a[start:stop] # items start through stop-1 a[start:] # items start through the rest of

我有一个1-100之间的数据点,我只想拟合数据点30-50之间的高斯函数。那么如何在python中设置拟合范围30-50。

可以使用切片表示法

l_ = list(range(1, 100))

start = 30
stop = 50
print (l_[start-1:stop])
切片Python列表/数组

a[start:stop]  # items start through stop-1
a[start:]      # items start through the rest of the array
a[:stop]       # items from the beginning through stop-1
a[:]           # a copy of the whole array
还有一个步长值,可与上述任何一项一起使用:

a[start:stop:step] # start through not past stop, by step
要记住的关键点是:stop值表示不在所选切片中的第一个值。因此,“停止”和“开始”之间的差异是所选元素的数量(如果“步骤”为1,则为默认值)

另一个特性是start或stop可能是负数,这意味着它从数组的末尾开始计数,而不是从数组的开头开始计数。因此:

a[-1]    # last item in the array
a[-2:]   # last two items in the array
a[:-2]   # everything except the last two items
类似地,步骤可以是负数:

a[::-1]    # all items in the array, reversed
a[1::-1]   # the first two items, reversed
a[:-3:-1]  # the last two items, reversed
a[-3::-1]  # everything except the last two items, reversed
例如:

l_ = list(range(10)) #  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print (l_)
print (l_[2:5])
输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 3, 4]

这将有助于理解您已经尝试过的内容。我在这里附加了一个代码和数据链接,请看一看,并让我知道如何仅在X轴数据点的160-205范围内拟合数据。1.python代码和数据文件的链接是