How to generate scatter plot similar to attached scatter plot?

3 views (last 30 days)
I want to generate scatter plot over laid by the equation of fit and R^2 value. Also the x and y axis lables as attached pdf plot. I am attaching the input data and plot and request you all to kindly suggest me how to do it in matlab. I would appreciate your kind suggestions.
  1 Comment
dpb on 17 Aug 2023
See <example> that shows a quadratic and its equation; simply follow those steps to fit your trendline after using scatter. Remember to set hold on before drawing the trendline to add to the same axis instead of clearing it.

Sign in to comment.

Accepted Answer

KSSV on 18 Aug 2023
Edited: KSSV on 18 Aug 2023
T = readtable('') ;
If you don't have ML toolbox, you may follow:
T = readtable('') ;
% plotregression(T.(1),T.(2))
x = T.(1) ;
y = T.(2) ;
R = regression(x',y') ;
p = polyfit(x,y,1) ;
hold on
str{1} = sprintf('y=%0.2fx+%0.2f\n',p(1),p(2)) ;
str{2} = sprintf('R = %0.2f\n',R) ;
dpb on 19 Aug 2023
<regression> is documented as "Not recommended"; why such an animal was ever introduced is another Q? The better (and recommended) function would be fitlm which will produce all the desired results dirctly.
Nota Bene: The "R" value returned by regression is an unusual choice; it is NOT the normally-reported R^2 ("R-squared") which is the fraction of the total variance explained by the regression but the square root of R^2. Since the data in this case is mostly a blob rather than showing a linear relationship, the value of ~0.5 is pretty misleading; R-sq for these data is 0.22. That makes the lack of correlation much more apparent.
T = readtable('');
T.Properties.VariableNames={'x','y'}; % make more convenient names
% IF going to use regression, at least use its results instead
% recalculating again later -- m,b are polyfit() coefficients
[R,m,b] = regression(T.x.',T.y.')
R = 0.4712
m = 0.9168
b = 0.6520
hold on
p=[m b]; % slope, intercept for polyval()
str = sprintf('y=%0.2fx+%0.2f\nR^2 = %0.2f',p(1),p(2),R^2) ;
Now use recommended fitlim...
T.Properties.VariableNames={'X','Y'}; % make uppercase for pretty
f=fitlm(T,'Y ~ X')
f =
Linear regression model: Y ~ 1 + X Estimated Coefficients: Estimate SE tStat pValue ________ ______ _______ __________ (Intercept) 0.65202 1.0723 0.60808 0.5442 X 0.9168 0.1511 6.0677 1.3449e-08 Number of observations: 131, Error degrees of freedom: 129 Root Mean Squared Error: 2.43 R-squared: 0.222, Adjusted R-Squared: 0.216 F-statistic vs. constant model: 36.8, p-value = 1.34e-08
It provides all the requested information directly
figure % don't wipe out first
plotAdded(f) % plot with regression line, model, bounds
You can adjust the plot as desired if don't want everything it puts on the plot by default and add the Rsq value if desired.

Sign in to comment.

More Answers (0)


Find more on Discrete Data Plots in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!