how to add best fit line in subplots in function plotmatrix?
Show older comments
Data = readtable('autompg.csv', 'VariableNamingRule','preserve');
Displacement = Data.displacement;
Horsepower = Data.horsepower;
Weight = Data.weight;
Acceleration = Data.acceleration;
% Add row and column labels
variableNames = {'Displacement', 'Horsepower', 'Weight', 'Acceleration'};
Predictors = [Displacement, Horsepower, Weight, Acceleration];
[S,AX,BigAx,H,HAx] = plotmatrix(Predictors)
iterations = size(AX,1);
for i = 1:iterations
for j = 1:iterations
if i ~= j
% Overlay best fit line
ax = AX(i, j);
% Add correlation value
corr_val = corr_matrix(i, j);
text(ax, 0.5, 0.9, sprintf('r = %.3f', corr_val), ...
'Units', 'normalized', 'HorizontalAlignment', 'center');
end
end
end
% Add labels
for i = 1:iterations
AX(i,1).YLabel.String = variableNames(i);
AX(iterations,i).XLabel.String = variableNames(i);
end
Right now the above code shows the image below using the function plotmatrix

however I want this to look like the second one where there is the best fit line in each subplots. Please note the second image
uses the function corrplot.

1 Comment
N/A
on 15 May 2024
Accepted Answer
More Answers (0)
Categories
Find more on Grid Lines, Tick Values, and Labels 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!

