Question

This should be simple - but it appears not. I just would like to have a figure with five subplots next to each other - without spaces in between. Also, the first tricky part perhaps is that I want to have a first subplot that uses floatAxisX.

Let's say I have four variables:

 x1 = salinity 
 x2 = temperature
 x3 = density 
 y = depth

So my code looks like this:

figure;
hfig = figure('Name','xxx');
set(gcf,'Position',get(0,'ScreenSize'))
set(hfig,'color','w');

subplot(151);set(subplot(151),'Position',[0.15 0.15 0.15 0.75]);

    % plot salinity vs depth
     hl1=plot(x1,y,'k-');
    % assign current axis handle to variable for later reference if needed
     ax1=gca;
    % set properties of the axes
     set(ax1,'XMinorTick','on','ydir','reverse', 'ytick',[0:25:150],'box','on','xcolor',get(hl1,'color'))   

    % add 1st floating axis for the second parameter (temperature) plotted
      [hl2,ax2,ax3] = floatAxisX(x2,y,'r:','Temperature (C)',[5 15 0 150]);
      set(ax2,'ydir','reverse','ytick',[0:25:150])

     % add 2nd floating axis for the third parameter (density) plotted
      [hl3,ax4,ax5] = floatAxisX(x3,y,'b--','Density (Kg m^-^3)',[24 27 0 150]);
      set(ax4,'ydir','reverse','ytick',[0:25:150]);

 subplot(152);set(subplot(152),'Position',[0.31 0.35 0.15 0.55]);

For example, I have distinct plots put together using hold on:

        plot(x1,y);axis ij; 

 subplot(153);set(subplot(153),'Position',[0.46 0.35 0.15 0.55]);

        plot(x1,y);axis ij; 

 subplot(154);set(subplot(154),'Position',[0.61 0.35 0.15 0.55]);

        plot(x1,y);axis ij; 

 subplot(155);set(subplot(155),'Position',[0.76 0.35 0.15 0.55]);

        plot(x1,y);axis ij; 

Note that just doing the following

 subplot(151);set(subplot(151),'Position',[0.15 0.15 0.15 0.75]);
 subplot(152);set(subplot(152),'Position',[0.31 0.35 0.15 0.55]);
 subplot(153);set(subplot(153),'Position',[0.46 0.35 0.15 0.55]);
 subplot(154);set(subplot(154),'Position',[0.61 0.35 0.15 0.55]);
 subplot(155);set(subplot(155),'Position',[0.76 0.35 0.15 0.55]);

only gives me a figure with the last three subplots next to each other. I would be grateful to get some explanation why this happens and how to solve it.

Was it helpful?

Solution

From the help for subplot:

If a subplot specification causes a new axes to overlap an
existing axes, the existing axes is deleted - unless the position
of the new and existing axes are identical.

Your code is doing this, though you may not realize it. You call subplot(151), which places an axis in the default location and then you position it manually. Because you have manually positioned the first axis, calling subplot(152) is the same as calling subplot(151) and the new axis ends up on top of the first one causing the latter to be deleted. And so on, until you move axes away from the the area where the default axis is placed.

There are several ways around this. You can create all of your subplots and then go back and position them. You can create your subplots starting from the right of your figure (subplot(155)) and move left. Or you can create the subplots in the desired positions directly via:

subplot('Position',[0.15 0.15 0.15 0.75]);
subplot('Position',[0.31 0.35 0.15 0.55]);
subplot('Position',[0.46 0.35 0.15 0.55]);
subplot('Position',[0.61 0.35 0.15 0.55]);
subplot('Position',[0.76 0.35 0.15 0.55]);

You can have these return handles if you need to plot to these axes separately at a later time via plot(AX,...) or subplot(AX); plot(...), where AX is an axis handle.

Also note that the help for subplot contains special comments for the subplot(151) style, particularly:

This syntax does not return a handle, so it is an error to specify a return argument.

I get a handle back myself, but I don't know if it's to be trusted. Still, even changing the style to subplot(1,5,1) the issue of only showing the last three persists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top