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