Filter löschen
Filter löschen

adding trendline to a plot

189 Ansichten (letzte 30 Tage)
Isa Isa
Isa Isa am 23 Dez. 2012
Kommentiert: Alex Backer am 1 Jun. 2020
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 23 Dez. 2012
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 Kommentare
Isa Isa
Isa Isa am 6 Jan. 2013
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
Ananya Roy am 20 Feb. 2017
Hi Isa,
I have similar problem as you had. Please let me know if you could solve that problem and how.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by