我试图在Matlab上使用两项高斯模型绘制两条拟合曲线。在单独绘制拟合曲线的同时,图表显示整个拟合曲线,但当我在单个图中合并两个拟合曲线时,仅显示部分曲线,其余曲线被切断。
代码:
% Data
X1 = [0 1 2 3 4 5 6 7 8 9 10]';
Y1 = [0.004 0.010 0.025 0.036 0.057 0.061 0.044 0.032 0.039 0.024 0.020]';
X2 = X1;
Y2 = [0.012 0.018 0.032 0.046 0.067 0.071 0.054 0.042 0.025 0.019 0.011]';
[p1,p2,p3] = deal(0.04, 3.5, 1.6);
[q1,q2,q3] = deal(0.03, 5.8, 4.02);
% Options
opt = fitoptions('method','NonlinearLeastSquares','Lower',[-Inf -Inf 0]);
ind1 = isfinite(X1) & isfinite(Y1);
ind2 = isfinite(X2) & isfinite(Y2);
set(opt,'Startpoint',[p1 p2 p3 q1 q2 q3]);
typ = fittype('gauss2');
% Fit
cf1 = fit(X1(ind1),Y1(ind1),typ,opt);
cf2 = fit(X2(ind2),Y2(ind2),typ,opt);
figure(1)
plot(cf1,'fit',0.95);
figure(2)
plot(cf2,'fit',0.95);
% Problem lies here while combining the two plots
figure(3)
hold on
plot(cf1,'fit',0.95);
plot(cf2,'fit',0.95);
答案 0 :(得分:4)
替换
figure(3)
hold on
plot(cf1,'fit',0.95);
plot(cf2,'fit',0.95);
通过
figure(3)
plot(cf1,'fit',0.95);
hold on
plot(cf2,'fit',0.95);
发生了什么事? hold on命令已经修复了绘图的轴。似乎拟合图然后使用该轴来确定其绘图范围。如果您在保持之前绘图,拟合图将自动确定其绘图范围。