adding trendline to a plot
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
1 개 추천
Good Day,
Please how do I add trendline(s) to a certain straight line portion(s) of a plot and how to extend the trendline to touch y and or x axis and the y and or x axis value determined?
Another related question is that how do I insert horizontal line on a plot and extend it to touch y axis and the y axis value determined?
Thanks
Best regards,
Isa
채택된 답변
Image Analyst
2012년 12월 23일
1 개 추천
I'd fit the portion of the data you're interested in to a line using polyfit. Then use polyval to get the points on the line over the entire range that you're interested in. See this demo:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Define equation.
x = -2:20;
y = (x-2).^2;
% Plot it
plot(x,y, 'bo-', 'LineWidth', 3);
grid on;
title('y = (x-2).^2', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Fit the range fro 10 to 20 with a line.
limitedRange = 10:20;
coeffs = polyfit(x(limitedRange), y(limitedRange), 1)
% Define the range where we want the line
xFitting = 0:19; % Or wherever...
yFitted = polyval(coeffs, xFitting);
% Plot the fitted line over the specified range.
hold on; % Don't blow away prior plot
plot(xFitting, yFitted, 'ro-', 'LineWidth', 2);
legend('Original Data', 'Line Fit');
댓글 수: 12
Image Analyst
2012년 12월 24일
Isa, did that work for you?
Isa Isa
2012년 12월 26일
I am still having problem with it. I tried using your code but Trendline was shown in the legend but not on the plot where I want it. My data as below x=[ 0 0.2762 0.3488 0.4052 0.5242 0.5806 0.6129 0.6431 0.6673 0.6976 0.7238 0.7520 0.7944 0.8448]; y=[ 0 0.0230 0.0370 0.0480 0.0760 0.0900 0.1000 0.1180 0.1380 0.1600 0.1860 0.2130 0.2880 0.4010]; I want the trendline at y range between y(9) to y(14).
Thanks Isa
Image Analyst
2012년 12월 26일
I just put your data into my demo and got this. Is this what you want?
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Define equation.
x=[ 0 0.2762 0.3488 0.4052 0.5242 0.5806 0.6129 0.6431 0.6673 0.6976 0.7238 0.7520 0.7944 0.8448];
y=[ 0 0.0230 0.0370 0.0480 0.0760 0.0900 0.1000 0.1180 0.1380 0.1600 0.1860 0.2130 0.2880 0.4010];
% Plot it
plot(x,y, 'bo-', 'LineWidth', 3);
grid on;
title('y = (x-2).^2', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Fit the range from index 1 to 14 with a line.
limitedRange = 1:14;
coeffs = polyfit(x(limitedRange), y(limitedRange), 1)
% Define the range where we want the line
xFitting = 9:14; % Or wherever...
yFitted = polyval(coeffs, xFitting);
% Plot the fitted line over the specified range.
hold on; % Don't blow away prior plot
plot(xFitting, yFitted, 'ro-', 'LineWidth', 2);
legend('Original Data', 'Line Fit');
Isa Isa
2012년 12월 26일
I run the code and see that trendline/line fit is not passing through any of the y data. Like your original demo, I want the trendline to fit and pass through certain region of the curve. It may be early region, middle region or late region of the curve.
Thanks Isa
Image Analyst
2012년 12월 26일
It was not clear when you said " I want the trendline at y range between y(9) to y(14)." because neither your x values nor your y values go through the range 9-14. See the line that says:
xFitting = 9:14; % Or wherever...
"whatever" means that you are supposed to change it to whatever you want for the x values. For example if you want 50 fitted (estimated) values between x(1) and x(14), you'd change that line to
xFitting = linspace(x(1), x(14), 50);
or if you wanted to make a point every 0.01 from x(1) to x(14), you'd do this:
xFitting = x(1) : 0.01 : x(14);
Isa Isa
2012년 12월 27일
Thanks. I have got it. I really appreciate your assistance. Another issue is that if I click on any point on my original data curve and fit curve, I want to see the x and y data for that point I click on the curve. Please assist.
Thanks Isa
Image Analyst
2012년 12월 27일
Can't you use ginput() to determine which actual data point is closest to the point you clicked at? That's what I'd try. If that didn't work I'd search Answers - I think that has been asked before.
If your original question is solved, go ahead and mark it as Answered.
Isa Isa
2012년 12월 27일
Thanks. ginput works well.
Isa Isa
2012년 12월 30일
Hi, Please if I want the slope of the 'Line Fit' how do I get it?
Thanks
Regards
José-Luis
2012년 12월 30일
doc polyfit
Isa Isa
2013년 1월 6일
Hi, Please assist on this. I try to add trendline to a semilogx plot but didn't succeed. Please check the code below.
% Define equation. x=[ 90868 68151 45434 34076 27261 13631 6816 3408 2273 1948 1705 1137 853 683 569 455 342 274 228 190]; y=[ 3680 3723 3800 3866 3920 4103 4250 4320 4340 4344 4350 4364 4373 4379 4384 4393 4398 4402 4405 4407];
% Plot it semilogx(x,y, 'bo-', 'LineWidth', 3); grid on; % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Give a name to the title bar. set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Fit the y data range with a line (limitedRange). limitedRange = 17:20; coeffs = polyfit(x(limitedRange), y(limitedRange), 1); xFitting = linspace(200, 90000, 50); yFitted = polyval(coeffs, xFitting);
% Plot the fitted line over the specified range. hold on; plot(xFitting, yFitted, 'ro-', 'LineWidth', 2); legend('Original Data', 'Line Fit');
Thanks Isa
Ananya Roy
2017년 2월 20일
Hi Isa,
I have similar problem as you had. Please let me know if you could solve that problem and how.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
