0
votes

Firstly, apologies in advance, as I'm new to MATLAB (and coding).

I'm trying to put multiple plots in a subplot (4,9,n) however the last plot overlays the rest of the plots and is the only one containing data.

I'd like each plot to look like something like this: Line plot with two x axes

Here's a snippet of the code I'm using (because I'm new, I haven't used a loop, so I've got code for each separate figure!):

%% TMR2    
%set up data
TMR2 = HEOBI1{HEOBI1.STNNBR==2,:}
x1 = TMR2(:,[4])
x2 = TMR2(:,[5])
x3 = TMR2(:,[7])/25
y = TMR2(:,[3])
%set up figure in subplot
subplot(4,9,1)
%set up secondary (H2O2) axis
b=axes('Position',[.1 .1 .8 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
b.XColor=[0 153/255 0];
%primary plot (DFE and FEII)
a=axes('Position',[.1 .2 .8 .7]);
set(a,'Units','normalized');
plot(x1,y,'-o','Color',[221/255 15/255 4/255],...
'MarkerFaceColor',[221/255 15/255 4/255])
set(gca,'Ydir','reverse')
xlim([0 3])
hold on
plot(x2,y,'-o','Color',[0 153/255 153/255],...
'MarkerFaceColor',[0 153/255 153/255])
plot(x3,y,'-o','Color',[0 153/255 0],...
'MarkerFaceColor',[0 153/255 0])
hold off
title('2')
%Set secondary (H2O2) axis limit
set(b,'xlim',[0 25]*3);

Finishing with:

%% TMR40
%set up data
TMR40 = HEOBI1{HEOBI1.STNNBR==40,:}
x1 = TMR40(:,[4])
x2 = TMR40(:,[5])
x3 = TMR40(:,[7])/25
y = TMR40(:,[3])
%set up figure in subplot
subplot(4,9,36)
%set up secondary (H2O2) axis
b=axes('Position',[.1 .1 .8 1e-12]);
set(b,'Units','normalized');
set(b,'Color','none');
b.XColor=[0 153/255 0];
%primary plot (DFE and FEII)
a=axes('Position',[.1 .2 .8 .7]);
set(a,'Units','normalized');
plot(x1,y,'-o','Color',[221/255 15/255 4/255],...
'MarkerFaceColor',[221/255 15/255 4/255])
set(gca,'Ydir','reverse')
xlim([0 3])
hold on
plot(x2,y,'-o','Color',[0 153/255 153/255],...
'MarkerFaceColor',[0 153/255 153/255])
plot(x3,y,'-o','Color',[0 153/255 0],...
'MarkerFaceColor',[0 153/255 0])
hold off
title('40')
%Set secondary (H2O2) axis limit
set(b,'xlim',[0 25]*3);

And here's what my final output looks like: Incorrect Subplot

@Suever I thought about your suggestion a bit more and took out all code for axes for each of the subplots. I then set up the subplot with handles and gave each subplot a handle. My code now starts like this:

%% Subplot setup
figure;
for k = 1:36
    h(k) = subplot(4,9,k);
end

and the code for each subplot now looks like this:

%% TMR40
%set up data
TMR40 = HEOBI1{HEOBI1.STNNBR==40,:}
x1 = TMR40(:,[4])
x2 = TMR40(:,[5])
x3 = TMR40(:,[7])/25
y = TMR40(:,[3])
%set up plot
subplot(h(36))
%primary plot (DFE and FEII)
plot(x1,y,'-o','Color',[221/255 15/255 4/255],...
'MarkerFaceColor',[221/255 15/255 4/255])
set(gca,'Ydir','reverse')
xlim([0 3])
hold on
plot(x2,y,'-o','Color',[0 153/255 153/255],...
'MarkerFaceColor',[0 153/255 153/255])
plot(x3,y,'-o','Color',[0 153/255 0],...
'MarkerFaceColor',[0 153/255 0])
hold off
title('40')

The subplots are now displaying correctly and I can edit individual subplots with their respective handles. Thanks again for your help!

1

1 Answers

1
votes

It's seems like you're confused about what subplot does and your terminology is a bit off. You don't "setup a figure in a subplot". A figure is the entire window that holds all of your plots, and subplot is an axes that holds your various plot objects (lines, images, surfaces, etc.).

The main issue though is that subplot creates an axes. This means that you don't need to call axes() after calling subplot otherwise that call to axes will create a new axes that does not obey the layout of the subplot inputs.

So instead of

subplot(9,4,36)
a = axes('Position', [.1 .2 .8 .7]);
plot(x, y)

Just do

subplot(9,4,36)
plot(x,y)