How do I plot two subplots side by side in one figure?

Hello,
I'd like to plot two subplots side by side in one figure. I currently have two subplots in a for loop each, and from what I've read online, MATLAB doesn't allow to plot a subplot within a subplot. Is there another way of plotting these subplots alongside each other?
suplotTitle = {'0.4mA', '0.6mA', '0.8mA', '1.2', '1.6mA'};
f1=figure
for ii=1:5
subplot(5,1,ii);
b1 = bar(conRR(2:end,1), conRR(2:end, ii+1));
b1.FaceColor = '#D95319';
b1.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
f2=figure
for ii=1:5
subplot(5,1,ii);
b2 = bar(b9RR(2:end,1), b9RR(2:end, ii+1));
b2.FaceColor = '#0072BD';
b2.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
I have tried converting the subplots into images using the following code:
F1 = getframe(f1);
[X1, Map1] = frame2im(F1);
F2 = getframe(f2);
[X2, Map2] = frame2im(F2);
and then plotting a subplot with the images:
figure;
subplot(1,2,1), imshow(X1,Map1)
subplot(1,2,2), imshow(X2,Map2)
However, the quality of the figure (images) are not that great.
Would appreciate any feedback to plot the subplots another way or to perhaps improve the quality of the images.
Thanks.

댓글 수: 2

MATLAB doesn't allow to plot a subplot within a subplot.
MATLAB permits subplots within uipanel() . Each uipanel() acts as a frame that can contain multiple axes, with it being possible to position the uipanels independently of each other.
Thanks for letting me know. This will come useful to me. Cheers.

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2020년 6월 7일
Try this:
conRR = [(0:10).' rand(11,5)*80]; % Create Matrix
b9RR = [(0:10).' rand(11,5)*80]; % Create Matrix
f1=figure
for ii=1:5
subplot(5,1,ii);
b1 = bar(conRR(2:end,1), conRR(2:end, ii+1));
b1.FaceColor = '#D95319';
b1.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
f2=figure
for ii=1:5
subplot(5,1,ii);
b2 = bar(b9RR(2:end,1), b9RR(2:end, ii+1));
b2.FaceColor = '#0072BD';
b2.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
That appears to do what you want it to do.

댓글 수: 4

As it currently is, the code still plots each subplot as individual figures. I would like to plot them alongside one another in one figure. Thanks.
I forgot to delete the:
f2 = figure
line in the code I posted. My apologies.
Try this:
conRR = [(0:10).' rand(11,5)*80]; % Create Matrix
b9RR = [(0:10).' rand(11,5)*80]; % Create Matrix
suplotTitle = {'0.4mA', '0.6mA', '0.8mA', '1.2mA', '1.6mA'};
f1=figure
for ii=1:5
subplot(5,2,2*ii-1);
b1 = bar(conRR(2:end,1), conRR(2:end,ii+1));
b1.FaceColor = '#D95319';
b1.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
for ii=1:5
subplot(5,2,2*ii);
b2 = bar(b9RR(2:end,1), b9RR(2:end,ii+1))
b2.FaceColor = '#0072BD';
b2.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
.
That was straight forward, thanks again! I've been cranking my brain for hours over this. I'm fairly new to MATLAB, but I'll get there eventually. Cheers.
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Vitek Stepien
Vitek Stepien 2021년 12월 18일
Try using subplot with 2 columns, iand see if that does what you need.
Instead of:
subplot(5,1,ii);
Use:
subplot(5,1,ii);
Then you can get away with just a single for loop, using:
subplot(5,1,2*ii-1); % For conRR plots
subplot(5,1,2*ii); % For b9RR plots

댓글 수: 3

That’s exactly what my posted code does —
conRR = [(0:10).' rand(11,5)*80]; % Create Matrix
b9RR = [(0:10).' rand(11,5)*80]; % Create Matrix
suplotTitle = {'0.4mA', '0.6mA', '0.8mA', '1.2mA', '1.6mA'};
f1=figure;
for ii=1:5
subplot(5,2,2*ii-1);
b1 = bar(conRR(2:end,1), conRR(2:end,ii+1));
b1.FaceColor = '#D95319';
b1.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
for ii=1:5
subplot(5,2,2*ii);
b2 = bar(b9RR(2:end,1), b9RR(2:end,ii+1));
b2.FaceColor = '#0072BD';
b2.EdgeColor = 'none';
xticks(1:1:16);
ylim([0 80]);
title(suplotTitle{ii})
end
.
Oh right, sorry - it was hidden down the comments and I missed it before, I only saw the top comment which didn't have that!
No worries!
.

댓글을 달려면 로그인하십시오.

카테고리

질문:

NA
2020년 6월 7일

댓글:

2021년 12월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by