How to generate scatter plot similar to attached scatter plot?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
Respuesta aceptada
KSSV
el 18 de Ag. de 2023
Editada: KSSV
el 18 de Ag. de 2023
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 comentarios
dpb
el 19 de Ag. de 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('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.')
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')
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.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!