how to create one size fits boxplot figures ?

5 views (last 30 days)
I wrote code but the problem is the size of subplots is different how can i create the same size for all subplots. The figure below show the high of plots is differnent.
How to resize the subplots ?
for i = 1:numel(dataA)
% plot boxplot of circuit 1 to circuit 7 and the aggregtae power for Line 1
subplot(3,3,i) % create the sub-plot with 3 rows and 3 columns
boxplot(dataA{i},'Labels',{'3','5','7','9','11','13','15'})
xlabel({'Harmonic Order'})
ylabel({'% of fundimental'})
title(titles{i})
h=findobj('LineStyle','--'); set(h, 'LineStyle','-');
grid, grid minor
end
This is the code, in which line and what is the code shoud be written?

Accepted Answer

Shubham
Shubham on 18 Oct 2023
Hi Omar,
To resize the subplots and make them the same size, you can use the subplot function's Position property. Adjusting the Position property allows you to control the size and position of each subplot within the figure.
% Sample data
dataA = cell(1, 9);
for i = 1:numel(dataA)
dataA{i} = rand(10, 7);
end
titles = {'Circuit 1', 'Circuit 2', 'Circuit 3', 'Circuit 4', 'Circuit 5', 'Circuit 6', 'Circuit 7', 'Circuit 8', 'Circuit 9'};
% Create the figure
figure;
% Define the number of rows and columns for the subplot grid
subplotRows = 3;
subplotCols = 3;
% Plot the subplots
for i = 1:numel(dataA)
% Plot boxplot of circuit 1 to circuit 9
subplot(subplotRows, subplotCols, i);
% Adjust the subplot size
ax = gca;
ax.Position(3) = ax.Position(3) * (subplotCols / subplotRows);
numGroups = size(dataA{i}, 2); % Number of groups in the data
labels = cellstr(num2str((1:numGroups)')); % Generate labels for each group
boxplot(dataA{i}, 'Labels', labels);
xlabel('Harmonic Order');
ylabel('% of fundamental');
title(titles{i});
h = findobj('LineStyle', '--');
set(h, 'LineStyle', '-');
grid on;
grid minor;
end
I assumed that each dataA entry contains a matrix where each column represents a group. The number of groups is determined by the size of the second dimension of the matrix. The labels are generated dynamically using cellstr and num2str based on the number of groups.
Please replace the rand(10, 7) with your own data matrix, ensuring that the number of columns represents the number of groups in each subplot.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!