Python 使用matplotlib进行曲线拟合
我有两个1d数组Python 使用matplotlib进行曲线拟合,python,numpy,matplotlib,scipy,Python,Numpy,Matplotlib,Scipy,我有两个1d数组shape.x=[701,]和shape.y=[701,]。这给了我下图所示的曲线。我怎样才能使曲线适合这一点 看一看 , 下面有一个例子,基本上就是你想要的 编辑:回复评论 import matplotlib.pyplot as plt; import numpy as np; import scipy.optimize as opt; # This is the function we are trying to fit to the data. def func(x, a
shape.x=[701,]
和shape.y=[701,]
。这给了我下图所示的曲线。我怎样才能使曲线适合这一点
看一看
,
下面有一个例子,基本上就是你想要的
编辑:回复评论
import matplotlib.pyplot as plt;
import numpy as np;
import scipy.optimize as opt;
# This is the function we are trying to fit to the data.
def func(x, a, b, c):
return a * np.exp(-b * x) + c
# Generate some data, you don't have to do this, as you already have your data
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
y_noise = 0.2 * np.random.normal(size=xdata.size)
ydata = y + y_noise
# Plot the actual data
plt.plot(xdata, ydata, ".", label="Data");
# The actual curve fitting happens here
optimizedParameters, pcov = opt.curve_fit(func, xdata, ydata);
# Use the optimized parameters to plot the best fit
plt.plot(xdata, func(xdata, *optimizedParameters), label="fit");
# Show the graph
plt.legend();
plt.show();
x、y数据是扩展数据和ydata变量
因此,如果您想使用这段代码,只需去掉生成数据的位,然后将x,y数据数组定义为“xdata”和“ydata”。这个示例对我来说相当复杂,因为我是python新手。如果我使用给定示例的代码,你能告诉我x和y值应该放在哪里吗。