How can I use the Basic Fitting tool to create a trendline on different scatterplots within the same figure?

15 visualizzazioni (ultimi 30 giorni)
I have previously plotted multiple scatterplots in the same figure (example picture attached) and saved a .fig as well as a.png copy. Now i want to add a line of best fit to each of the scatter plots. When using the basic fitting tool I am able to add a trendline to any one of my scatterplots. The problem arises when I go to select the next set of data (data tab on the top) in order to add a new trendline to a different scatterplot. A trendline is added to the newly selected scatterplot but the trendline that was previously added to the other scatterplot now disappears. So my question is, is there a way how to add trendlines to multiple scatterplots using the basic fitting tool? I would each of the scatterplots included in my figure to have the line of best fit plotted on them.
Thank you in advance!

Risposte (2)

Suraj Kumar
Suraj Kumar il 28 Mar 2024
Hi Chanelle,
To add trendlines to multiple plots simultaneously you can use Curve Fitter App using which you can interactively fit curves and surfaces to data and view plots. You can also compare multiple fits simultaneously using regression, interpolation, smoothing, and custom equations.
You can refer the following image for better understanding:
Alternatively, you can also add trendlines to multiple plots simultaneously using polyfit” and “polyval” functions. "polyfit" fits a polynomial to your data points, while "polyval" evaluates this polynomial across a specified range for plotting purposes. This produces a visual representation that showcases both the original data points and the corresponding fitted polynomial curve, facilitating straightforward comparison.
You can refer the following code snippet:
subplot(2, 2, 1);
scatter(x1, y1, 'b');
hold on;
p1 = polyfit(x1, y1, 1);
yfit1 = polyval(p1, x1);
plot(x1, yfit1, 'k-', 'LineWidth', 2);
title('Linear Trend');
Similarly, this can be done for other subplots.
You can refer the below attached output obtained from an example data:
To know more about Curve Fitter App,” polyfit” and “polyval” , you can go through the following documentations:
  1. https://www.mathworks.com/help/curvefit/curvefitter-app.html
  2. https://www.mathworks.com/help/curvefit/interactive-fit-comparison.html
  3. https://www.mathworks.com/help/matlab/ref/polyfit.html
  4. https://www.mathworks.com/help/matlab/ref/polyval.html
Hope this helps!

Star Strider
Star Strider il 28 Mar 2024
An easy way to do this is to use the lsline function. If you need the parameters of the line, they are relatively straightforward to calculate (stored in ‘lslparam’ here) —
% file = dir('*.png');
% imshow(imread(file.name))
x = (125:5:250).';
y = randn(numel(x),4);
ysubscript = ["ic" "max" "tc" "min"];
figure
tiledlayout(2,2)
for k = 1:size(y,2)
nexttile
scatter(x, y(:,k), 'filled')
xlabel("Hip Abduction PT/BW")
ylabel("HSP_{" + ysubscript(k) + "} (°)")
axis('padded')
hlsl = lsline;
hlsl.LineWidth = 2;
lslparam(:,k) = [hlsl.XData; 1 1].' \ hlsl.YData(:); % 'lsline' Parameters [slope; intercept]
end
lslparam
lslparam = 2×4
0.0029 0.0009 -0.0085 -0.0062 -0.5472 -0.2565 1.9090 1.5055
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

Community Treasure Hunt

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

Start Hunting!

Translated by