Plotting fit and data on the same plot - define endpoint of fit line

3 次查看(过去 30 天)
Hi - I am plotting a data fit equation and the actual data on the same plot. I can get the basic plot but I cannot get it to look quite like I want.
I need to have the data and the corresponding fit line be the same color and I need the fit line to plot across the entire xrange that is plotted. I have included the relevant portions of the code below.
In the plot that is used in this code (p=plot...), I can get it to work with the exception that the fitline only plots from the first datapoint to the last datapoint (x=7.5 to 52) instead of across the entire range (0 to 55).
If I used the commented out version of the code, it plots across the entire range with the colors, but does not plot the actual data.
Does anybody have any suggestions?
Thank you.
vars = who ('RL*');
figure; rainbow=colormap(hsv(length(vars))); axes('FontSize',20,'FontName','Times New Roman');
hold all; grid on;
for i = 1:length(vars)
...
[fiteqn,rsqr] = fit(RL(:,1),RL(:,2),'poly1','Exclude',outliers);
r2=rsqr.rsquare;
p=plot(fiteqn,RL(:,1),RL(:,2),'.');
% p=plot(fiteqn,'.');
xlim([0 55]);
legendstr(2*i-1,1) = vars(i);
legendstr(2*i,1) = strcat(vars(i),' fit');
set(p,'MarkerSize',12,'Color',rainbow(i,:),'LineWidth',1.5);
...
end
legend(legendstr,'Location','NorthEastOutside');

采纳的回答

José-Luis
José-Luis 2012-8-20
Maybe this is what you want:
x=sort(rand(10,1));
y=sort(rand(10,1));
[fiteqn,rsqr] = fit(x,y,'poly1');
plot(x,y);
hold on;
fplot(fiteqn,[-1 2]); %replace [-1 2] by the interval you want
Cheers!
  2 个评论
Becca
Becca 2012-8-20
Thank you. This worked. I'm not sure why fit and cfit do not suggest fplot instead of plot(cfit) as plot(cfit) is hard to separate from plot(x,y) when looking for more help.
I ended up having to a slightly different set line to get the colors. The 'Color', 'k' finds the most recent line (which was set to 'k') and updating that with the color from my custom array. Otherwise everything was set to my last color. (Mostly included code for future users with a similar problem).
plot(RL(:,1),RL(:,2),'.','Color',rainbow(i,:),'MarkerSize',12);
fplot(fiteqn,[0 55],'k');
set(findobj(gca, 'Type', 'Line', 'Color', 'k'), ... 'Color',rainbow(i,:),'LineWidth',1.5);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fit Postprocessing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by