Borrar filtros
Borrar filtros

how to add best fit line in subplots in function plotmatrix?

6 visualizaciones (últimos 30 días)
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 comentario
NAFISA SIDDIQUI
NAFISA SIDDIQUI el 15 de Mayo de 2024
i tried doing this but there is error
iterations = size(AX,1);
for i = 1:iterations
for j = 1:iterations
if i ~= j
% Overlay best fit line
ax = AX(i, j);
xdata = Predictors(:, i);
ydata = Predictors(:, j);
p = polyfit(xdata, ydata, 1);
hold(ax, 'on');
plot(ax, xdata, polyval(p, xdata), 'r--');

Iniciar sesión para comentar.

Respuesta aceptada

Shubham
Shubham el 15 de Mayo de 2024
Hi Nafisa,
The code snippet you've shared is almost correct in its approach to overlay a best fit line on each subplot generated by plotmatrix. However, it seems like you're missing a crucial part where you calculate the correlation matrix (corr_matrix) that you're trying to use later for annotating the plots. Additionally, the way you're attempting to overlay the best fit line is correct, but let's ensure it's integrated properly into your loop.
Here's a revised version of your code that includes the calculation of the correlation matrix and ensures that the best fit lines are correctly added to each subplot:
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];
% Calculate correlation matrix for later use
corr_matrix = corr(Predictors, 'Rows', 'complete');
[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);
xdata = Predictors(:, j); % Note: The columns (j) should match x-axis in plotmatrix
ydata = Predictors(:, i); % Note: The rows (i) should match y-axis in plotmatrix
p = polyfit(xdata, ydata, 1); % Fit line
hold(ax, 'on');
plot(ax, xdata, polyval(p, xdata), 'r--'); % Plot line
% Add correlation value
corr_val = corr_matrix(i, j);
text(ax, 0.5, 0.9, sprintf('r = %.3f', corr_val), ...
'Units', 'normalized', 'HorizontalAlignment', 'center', 'FontSize', 8);
hold(ax, 'off'); % Release hold to allow next plots
end
end
end
% Add labels
for i = 1:iterations
AX(i,1).YLabel.String = variableNames{i};
AX(iterations,i).XLabel.String = variableNames{i};
end
Key modifications and clarifications:
  • Before looping through the axes to add best fit lines and correlation values, calculate the correlation matrix of your predictors. This matrix is then used within the loop to annotate each subplot with the corresponding correlation coefficient.
  • When extracting xdata and ydata for fitting the line, ensure that you're using the correct indices. The outer loop variable i should index ydata, and the inner loop variable j should index xdata, matching how plotmatrix organizes the subplots.
  • The hold(ax, 'on') command is correctly applied to ensure that the line plot is added on top of the existing scatter plot. After plotting the line, hold(ax, 'off') is used to release the hold state, though it's not strictly necessary at the end of your plotting commands for each subplot.
This revised code should overlay a best fit line on each of the off-diagonal plots in your plotmatrix output and annotate them with the correlation coefficient, closely aligning with the functionality you described.
  1 comentario
NAFISA SIDDIQUI
NAFISA SIDDIQUI el 15 de Mayo de 2024
Editada: NAFISA SIDDIQUI el 15 de Mayo de 2024
Thank you so very much Shubham, the code definitely worked!! and looks like this
however i was wondering if we could extend the line so that it crosses the y intercept so I tried using this code
for i = 1:iterations
for j = 1:iterations
if i ~= j
% Overlay best fit line
ax = AX(i, j);
xdata = Predictors(:, j); % Note: The columns (j) should match x-axis in plotmatrix
ydata = Predictors(:, i); % Note: The rows (i) should match y-axis in plotmatrix
p = polyfit(xdata, ydata, 1); % Fit line
% Extend the best fit line
x_limits = xlim(ax);
y_limits = ylim(ax);
x_min = x_limits(1);
x_max = x_limits(2);
y_min = polyval(p, x_min);
y_max = polyval(p, x_max);
% Plot extended line
hold(ax, 'on');
plot(ax, [x_min, x_max], [y_min, y_max], 'r');
% Add correlation value
corr_val = corr_matrix(i, j);
text(ax, 0.5, 0.9, sprintf('r = %.3f', corr_val), ...
'Units', 'normalized', 'HorizontalAlignment', 'center', 'FontSize', 8);
hold(ax, 'off'); % Release hold to allow next plots
end
end
end
this above code gives this image
however when I use the function corrplot it gives an image like the one below so does that mean there is something wrong with my code?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by