bar plot with two y-axis one for sum of squared error value and the second for adjusted r squared and f-statistic values padding with zeros in matlab

To create a bar plot with two y-axes in MATLAB, you can use the yyaxis function along with the bar function. Here's an example of how you can do this, padding the values with zeros:

main.m
% Generate some example data
errorValues = [0.5, 1.3, 0.8, 2.1]; % Sum of Squared Error values
rSquaredValues = [0.75, 0.85, 0.72, 0.91]; % Adjusted R-squared values
fStatisticValues = [15, 8, 20, 12]; % F-statistic values

% Pad the shorter arrays with zeros to make them the same length
maxLen = max([length(errorValues), length(rSquaredValues), length(fStatisticValues)]);
errorValues = padarray(errorValues, [0 maxLen - length(errorValues)], 'post');
rSquaredValues = padarray(rSquaredValues, [0 maxLen - length(rSquaredValues)], 'post');
fStatisticValues = padarray(fStatisticValues, [0 maxLen - length(fStatisticValues)], 'post');

% Create a bar plot with two y-axes
figure
yyaxis left % Sum of Squared Error y-axis
bar(errorValues, 'b')
ylabel('Sum of Squared Error')

hold on

yyaxis right % Adjusted R-squared and F-statistic y-axes
bar([rSquaredValues, fStatisticValues], 'r')
ylabel('Adjusted R-squared / F-statistic')

% Add a legend to differentiate between the bars
legend('Sum of Squared Error', 'Adjusted R-squared', 'F-statistic', 'Location', 'northwest')

% Set the x-axis tick labels accordingly
xticklabels({'Model 1', 'Model 2', 'Model 3', 'Model 4'})

% Adjust the figure size if necessary
set(gcf, 'Position', [100, 100, 800, 400])

% Optionally save the figure
% saveas(gcf, 'bar_plot.png')

1317 chars
36 lines

This code will generate a bar plot with two y-axes, one for the sum of squared error values shown in blue bars, and the other for the adjusted R-squared and F-statistic values shown in red bars. Zeros are added to the shorter arrays to make them the same length. Change the values in the errorValues, rSquaredValues, and fStatisticValues arrays to match your actual data. You can also modify the colors, labels, and other formatting options as needed.

I hope this helps!

related categories

gistlibby LogSnag