Scipy curve_fit: 如何绘制超出数据点的拟合曲线?
Scipy curve_fit: how to plot the fitted curve beyond the data points?
我有许多数据点,我使用 Scipy curve_fit
为该数据集拟合曲线。我现在想绘制 超出 范围的数据点,但我不知道该怎么做。
这是一个基于指数拟合的简单示例:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def exponential_fit(x, a, b, c):
return a*np.exp(-b*x) + c
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([30, 50, 80, 160, 300, 580])
fitting_parameters, covariance = curve_fit(exponential_fit, x, y)
a, b, c = fitting_parameters
plt.plot(x, y, 'o', label='data')
plt.plot(x, exponential_fit(x, *fitting_parameters), '-', label='Fit')
plt.axis([0, 8, 0, 2000])
plt.legend()
plt.show()
本returns以下剧情:
现在如何扩展拟合(橙色)曲线使其上升到 x = 8?请注意,我不想创建额外的数据点,我只想扩大拟合曲线的范围。
非常感谢。
您必须为 x 定义一个额外的数据范围,以将其扩展到您的数据点给定的数据范围之外。您甚至可以改进表示并为拟合函数计算更多 x 值:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def exponential_fit(x, a, b, c):
return a*np.exp(-b*x) + c
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([30, 50, 80, 160, 300, 580])
fitting_parameters, covariance = curve_fit(exponential_fit, x, y)
a, b, c = fitting_parameters
x_min = -4
x_max = 8 #min/max values for x axis
x_fit = np.linspace(x_min, x_max, 100) #range of x values used for the fit function
plt.plot(x, y, 'o', label='data')
plt.plot(x_fit, exponential_fit(x_fit, *fitting_parameters), '-', label='Fit')
plt.axis([x_min, x_max, 0, 2000])
plt.legend()
plt.show()
为了增加灵活性,我引入了 x_min, x_max
,因为相同的值用于计算拟合函数使用的 x 值的范围,并用于缩放绘图的轴。 numpy.linspace
在起始值和终止值之间创建一个均匀间隔的样本,用作 x 值以计算拟合函数中相应的 y 值。
x
范围从 0 到 5。如果您希望曲线上升到 8 (or up to eleven),您需要提供一个范围为 11 的数组...抱歉 8.
x_new = np.linspace(0,11)
plt.plot(x_new, exponential_fit(x_new, *fitting_parameters), '-', label='Fit')
我有许多数据点,我使用 Scipy curve_fit
为该数据集拟合曲线。我现在想绘制 超出 范围的数据点,但我不知道该怎么做。
这是一个基于指数拟合的简单示例:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def exponential_fit(x, a, b, c):
return a*np.exp(-b*x) + c
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([30, 50, 80, 160, 300, 580])
fitting_parameters, covariance = curve_fit(exponential_fit, x, y)
a, b, c = fitting_parameters
plt.plot(x, y, 'o', label='data')
plt.plot(x, exponential_fit(x, *fitting_parameters), '-', label='Fit')
plt.axis([0, 8, 0, 2000])
plt.legend()
plt.show()
本returns以下剧情:
现在如何扩展拟合(橙色)曲线使其上升到 x = 8?请注意,我不想创建额外的数据点,我只想扩大拟合曲线的范围。
非常感谢。
您必须为 x 定义一个额外的数据范围,以将其扩展到您的数据点给定的数据范围之外。您甚至可以改进表示并为拟合函数计算更多 x 值:
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def exponential_fit(x, a, b, c):
return a*np.exp(-b*x) + c
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([30, 50, 80, 160, 300, 580])
fitting_parameters, covariance = curve_fit(exponential_fit, x, y)
a, b, c = fitting_parameters
x_min = -4
x_max = 8 #min/max values for x axis
x_fit = np.linspace(x_min, x_max, 100) #range of x values used for the fit function
plt.plot(x, y, 'o', label='data')
plt.plot(x_fit, exponential_fit(x_fit, *fitting_parameters), '-', label='Fit')
plt.axis([x_min, x_max, 0, 2000])
plt.legend()
plt.show()
为了增加灵活性,我引入了 x_min, x_max
,因为相同的值用于计算拟合函数使用的 x 值的范围,并用于缩放绘图的轴。 numpy.linspace
在起始值和终止值之间创建一个均匀间隔的样本,用作 x 值以计算拟合函数中相应的 y 值。
x
范围从 0 到 5。如果您希望曲线上升到 8 (or up to eleven),您需要提供一个范围为 11 的数组...抱歉 8.
x_new = np.linspace(0,11)
plt.plot(x_new, exponential_fit(x_new, *fitting_parameters), '-', label='Fit')