How to create a subplot of multiple plots within a for loop?

28 views (last 30 days)
I am generating a table using a for loop, and using that table to create three plots. How do I convert them into a layout such that first two figures are on top, and the third is at the bottom? Thanks. Here's my MWE
% Generating my table first
for j = 1:4
Table = [];
for i = 1:7
M = array2table(randi([55 100],6,1), 'VariableNames', strcat('X_', string(i)), 'RowNames',{'A', 'B', 'C', 'D', 'E', 'F'});
% Combining each column table
Table = [Table, M];
end
disp(Table)
% --- MY FIGURES SHOULD BE PLACED HERE WITH TWO SIDE-BY-SIDE ON TOP AND ONE AT THE
% BOTTOM.
% Figure 1
% --------------------------------------------------------------------------------
figure1 = figure;
x = 1:7; % Set x axis values
y = [Table{{'A'},:};
Table{{'C'},:}]; % Set y axis values
a1 = bar(x,y); % Generate figure
xlabel('Years'); % Add x label
ylabel('(in billion $)'); % Add y label
title('Profit as a share of revenue across years'); % Add plot title
set(a1, {'DisplayName'}, {'Profits','Revenue'}'); % Add display names to legends
legend('Location','northwest' ); % Change location of legends
legend boxoff % Remove the box around legends
box off % Remove the top and right splines from the plot
% Figure 2
% --------------------------------------------------------------------------------
figure2 = figure;
y = [Table{{'B'},:};
Table{{'D'},:}]; % Set y axis values
b1 = bar(x,y, 'stacked');
hold on
b2 = bar(x,Table{{'A'},:},0.55,'FaceColor',[0 0.7 0.7]);
hold on
b3 = bar(x,-1*Table{{'F'},:},0.1,'FaceColor',[0.4 0.1 0.7]);
hold off
hold off
xlabel('Years') % Add x label
ylabel('(in billion $)')
title('Present Value of Profits and Discount Factor') % Add plot title
set(b1, {'DisplayName'}, {'Profits', 'Revenue'}') % Add display names to legends
set(b2, {'DisplayName'}, {'Profits (in PV terms)'}')
set(b3, {'DisplayName'}, {'Discount Factor'}')
legend('Location','northwest') % Change location of legends
legend boxoff % Remove the box around legends
box off % Remove the top and right splines from the plot
% Figure 3
% --------------------------------------------------------------------------------
figure3 = figure;
y = [1000*Table{{'C'},:}]; % Set y axis values
c1 = bar(x,y); % Generate figure
xlabel('Years') % Add x label
ylabel('(in million $)') % Add y label
title('Loss across years (Present Value)') % Add plot title
box off % Remove the top and right splines from the plot
end

Accepted Answer

William Rose
William Rose on 2 Apr 2021
This code generates 2 plots above and one plot below:
>> x=1:10;
>> figure;
>> subplot(2,2,1); plot(x,x,'rx-');
>> subplot(2,2,2); plot(x,x,'go-');
>> subplot(2,1,2); plot(x,x,'bs-');
A screenshot is below.
  7 Comments

Sign in to comment.

More Answers (1)

William Rose
William Rose on 6 Apr 2021
@Alston D'Souza, The atattahced code makes one figure on each pass through the main loop of your code: four passes, four figures. Each figure has two plots on top and one on the bottom, as you requested. You may want to adjust the legend positions or the axes, so that the legends do not get in the way of the axes. Screenshot of figure 1 is below; the other 3 figs are similar.

Categories

Find more on Discrete Data Plots 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!