How to generate scatter plot similar to attached scatter plot?

조회 수: 4 (최근 30일)
Devendra
Devendra 2023년 8월 17일
댓글: dpb 2023년 8월 19일
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.
Devendra
  댓글 수: 1
dpb
dpb 2023년 8월 17일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2023년 8월 18일
편집: KSSV 2023년 8월 18일
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1458992/Test.csv') ;
plotregression(T.(1),T.(2))
If you don't have ML toolbox, you may follow:
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1458992/Test.csv') ;
% plotregression(T.(1),T.(2))
x = T.(1) ;
y = T.(2) ;
R = regression(x',y') ;
p = polyfit(x,y,1) ;
figure(2)
scatter(x,y,[],'k','filled')
hold on
plot(y,polyval(p,y),'--b')
str{1} = sprintf('y=%0.2fx+%0.2f\n',p(1),p(2)) ;
str{2} = sprintf('R = %0.2f\n',R) ;
text(2.8,10.5,str)
  댓글 수: 4
Devendra
Devendra 2023년 8월 19일
Please also suggest me how to compute mean square error(MSE) and root mean square error(RMSE) in your scatter plot. I just want to get these values without overlaying over the plot.
Devendra
dpb
dpb 2023년 8월 19일
<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('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1458992/Test.csv');
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
scatter(T.x,T.y,[],'k','filled')
hold on
p=[m b]; % slope, intercept for polyval()
plot(T.y,polyval(p,T.y),'--b')
str = sprintf('y=%0.2fx+%0.2f\nR^2 = %0.2f',p(1),p(2),R^2) ;
text(2.8,10.5,str,'interpreter','Tex')
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by