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

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

main.m
% Example data
sumSquaredError = [10, 20, 30, 40, 50, 60];  % Sum of squared error values
adjustedRSquared = [0.9, 0.85, 0.80, 0.75, 0.70, 0.65];  % Adjusted R-squared values
fStatistic = [5, 10, 15, 20, 25, 30];  % F-statistic values

% Pad the data with zeros
sumSquaredError = [sumSquaredError, zeros(1,6)];
adjustedRSquared = [adjustedRSquared, zeros(1,6)];
fStatistic = [fStatistic, zeros(1,6)];

% Create the bar plot
figure;
yyaxis left;
bar(sumSquaredError);
ylabel('Sum of Squared Error');
yyaxis right;
bar(adjustedRSquared);
ylabel('Adjusted R-squared');

% Add the F-statistic as additional bars
hold on;
yyaxis right;
bar(fStatistic, 'FaceAlpha', 0.5);
hold off;

% Adjust the y-axis limits and labels
ylim([0, max([sumSquaredError, adjustedRSquared, fStatistic]) * 1.1]);
yticks([]);

% Add labels and legends
xlabel('Data Points');
legend('Sum of Squared Error', 'Adjusted R-squared', 'F-statistic');
916 chars
33 lines

In the code above, we first create example data for the sum of squared error, adjusted R-squared, and F-statistic values. Then, we pad the data with six zeros to match the number of bars in the figure.

Next, we create a figure and use the yyaxis function to specify that we want two different y-axes. We plot the sum of squared error on the left y-axis using the bar function, and the adjusted R-squared on the right y-axis. Then, we add the F-statistic as additional bars on the right y-axis using the bar function again.

After that, we adjust the y-axis limits and labels using the ylim and yticks functions. Finally, we add the x-axis label and the legend to the plot.

Note that the FaceAlpha is used to adjust the opacity of the bars representing the F-statistic. You can modify this value to achieve the desired visual effect.

gistlibby LogSnag