MATLAB last plot in subplot overlays other plots - matlab

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!

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)

Related

Matlab Bar Plot with multiple X-Axis

i've been struggling with this for some time.
i want to be able to plot the following example except while using a bar plot.
simple plot that needs to be converted to bar plot
this was implemented by myself. This is basically two plot overlay each other with a different axes each (current example shows the same ticks values but i want them to be changes later on)
my problem is that using bar plot changes axis location like so and i cannot resolve it. see image below:
desired image but lower axis are not aligned
The axes here are not aligned. i need both to start at the same place, that is the most left bar
your help is much appreciated
my code below:
close all
figure
y= magic(10)
a=axes('Position',[.1 .2 .8 .7],'XColor','b');
% hold on;
bar( y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
xticklabels( {3:12});
legend;
hold on
bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
b= axes('Position',[.1 .1 .8 1e-12],'XColor','r')
xticklabels( {4:13});
legend;
EDIT:
managed to solve it - see update code below
close all
figure
y= magic(10)
ax1=axes('Position',[.1 .2 .8 .7],'XColor','b');
bar( y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
ax = gca
xticklabels( {3:12});
legend;
hold on
bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
axPos = ax1.Position;
ax2 = axes('Position', (axPos .* [1 1 1 1e-3])-[0 .07 0 -1e-3] , 'XColor', 'r', 'linewidth', 2);
set(ax2,'XLim',[-0.2 11.2])
set(ax2,'XTick',(1:10))
xticklabels( {4:13});
Just use same x-limit for two bar plots. The MATLAB function is 'xlim'.
figure
y= magic(10)
a=axes('Position',[.1 .2 .8 .7],'XColor','b'); hold all;
bar( y(1,:),'b','DisplayName','before prop','FaceAlpha',0.1);
xticks(3:1:12); xlim([0 15])
legend;
bar(y(2,:),'r','DisplayName','after prop','FaceAlpha',0.1);
b= axes('Position',[.1 .1 .8 1e-12],'XColor','r'); xlim([3 12]);
xticks(4:1:13); xlim([0 15])
legend;

Multiple axes for a single surf plot

I have a surf plot, in which I would like to have two y-axes. I cannot seem to find any other discussion quite like this.
The closest I got so far is:
surf(peaks);
view(0,0)
ax(1) = gca;
axPos = ax(1).Position;
ax(2) = axes('Position',axPos, 'Color', 'none','XTick',[],'ZTick',[],'YAxisLocation','right');
linkprop(ax, 'CameraPosition');
rotate3d on
% Desired plot
surf(peaks);
% Save axis
ax(1) = gca;
% Use the position of the first axis to define the new axis
pos = ax(1).Position;
pos2 = pos - [0.08 0 0 0];
ax(2) = axes('Position',pos2,'Color', 'none');
% Plot random line in 3D, just make sure your desired axis is correct
plot3(ones(length(peaks),1), 10:10:length(peaks)*10,...
ones(length(peaks),1), 'Color','none')
% Make plot, and non-desired axes, invisible
set(gca,'zcolor','none','xcolor','none','Color','none')
% Link axes
linkprop(ax, 'View');

plot in all the subplots at the same time

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)
You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2
If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

Set equal limits for y-axis for two figures

How can I make the vertical axes of two plots equal?
For example:
a = [1 2 3; 21 1 3; 4 2 3; 4 5 6]
After plotting plot(a(1, :)) I get the following figure:
I have done some simple operations:
[U E V] = svd(a);
figure(2);
plot(U(1,:))
And get another figure:
How do I make the y-axis limits of both plots equal? Is it with the axes equal command?
UPDATE:
I've used the following commands:
figure (1)
ylim([0 3])
plot(a(1,:))
figure (2);
ylim([0 3])
plot(U(1,:))
But get the same result...
You can use ylim to force limits on the y-axis. For example:
figure(1)
%// Some plotting...
ylim([0 3])
figure(2)
%// Some more plotting
ylim([0 3])
This ensures that the y-axis is limited to the range [0, 3] in both plots. You can do the same for the limits of the x-axis with the command xlim.
Also note that if you want to set the limits for both axes at once, instead of using xlim and ylim (two commands), you can use axis (one command).
you can use the ylim or xlim functions.
You can clone the limits of one plot to another plot in this fashion:
h1 = figure;
% do first plot...
h2 = figure;
%do second plot...
% set first figure as active
figure(h1);
%get limits properties of the axes that are drawn in Figure 1
xL = get(gca, 'XLim');
yL = get(gca, 'YLim');
%switch to second figure and set it as active
figure(h2);
%set axis limit properties of Figure 2 to be the same as in Figure 1
set(gca, 'XLim', xL);
set(gca, 'YLim', yL);

Plot outside axis in Matlab

How to plot something outside the axis with MATLAB? I had like to plot something similar to this figure;
Thank you.
Here is one possible trick by using two axes:
%# plot data as usual
x = randn(1000,1);
[count bin] = hist(x,50);
figure, bar(bin,count,'hist')
hAx1 = gca;
%# create a second axis as copy of first (without its content),
%# reduce its size, and set limits accordingly
hAx2 = copyobj(hAx1,gcf);
set(hAx2, 'Position',get(hAx1,'Position').*[1 1 1 0.9], ...
'XLimMode','manual', 'YLimMode','manual', ...
'YLim',get(hAx1,'YLim').*[1 0.9])
delete(get(hAx2,'Children'))
%# hide first axis, and adjust Z-order
axis(hAx1,'off')
uistack(hAx1,'top')
%# add title and labels
title(hAx2,'Title')
xlabel(hAx2, 'Frequency'), ylabel(hAx2, 'Mag')
and here is the plot before and after:
You can display one axis with the scale you want, then plot your data on another axis which is invisible and large enough to hold the data you need:
f = figure;
% some fake data
x = 0:20;
y = 23-x;
a_max = 20;
b_max = 23;
a_height = .7;
%% axes you'll see
a = axes('Position', [.1 .1 .8 a_height]);
xlim([0 20]);
ylim([0 20]);
%% axes you'll use
scale = b_max/a_max;
a2 = axes('Position', [.1 .1 .8 scale*a_height]);
p = plot(x, y);
xlim([0 20]);
ylim([0 b_max]);
set(a2, 'Color', 'none', 'Visible', 'off');
I had similar problem and I've solved it thanks to this answer. In case of bar series the code is as follows:
[a,b] = hist(randn(1000,1)); % generate random data and histogram
h = bar(b,a); % plot bar series
ylim([0 70]) % set limits
set(get(h,'children'),'clipping','off')% turn off clippings