Search code examples
matlabplotmatlab-figurefiguresubplot

Plotting using subplot with different x-axis for each plot


I am trying to plot figure with three plots inside it using subplot. The y-axis for all three plots is same from -1 to 1 while the x-axis is 2.5, 5, and 10 Respectively. The problem that is for all three plots the x-axis looks similar for all of them. I tried to use axis square but it changed plots to a square. I want the first plot to start from the left and end at a point while the second one start from the left and end at a point where it is a double distance of x-axis of the first plot. The third plot must start from the left and end at the point where the x-axis is thripple of the x-axis of the first plot. Is there any way to do that?

This is an example with an empty plot just to show how I want my figure to look like.

I used below code but unfortunately it is not working correctly

figure
subplot (3,1,1);
xlabel('x cm')                                              
ylabel('y cm')                                          
grid on
set(gca, 'XTick', 0:0.5:2.5)
set(gca, 'YTick', -1:1:1)
xlim([0 2.5]);
ylim([-1 1]);
% axis square
subplot (3,1,2);
xlabel('x cm')                                              
ylabel('y cm')                                          
grid on
set(gca, 'XTick', 0:1:5)
set(gca, 'YTick', -1:1:1)
xlim([0 5]);
ylim([-1 1]);
% axis square
subplot (3,1,3);
xlabel('x cm')                                              
ylabel('y cm')                                          
grid on
set(gca, 'XTick', 0:2:10)
set(gca, 'YTick', -1:1:1)
xlim([0 10]);
ylim([-1 1]);
% axis square

enter image description here


Solution

  • You can use the Position property of axes (here each of your subplot) and customize them as you wish, using the nomenclature

    [l b w h]
    

    where:

    l: left b: bottom
    w: width h: height

    For example assign a handle to each subplot:

    h(1) = subplot(3,1,1)
    h(2) = subplot(3,1,2)
    h(3) = subplot(3,1,3)
    

    and change their position (here in normalized units):

    set(h(1),'Position',[.1 .75 .2 .2])
    set(h(2),'Position',[.1 .45 .5 .2])
    set(h(3),'Position',[.1 .15 .8 .2])
    

    you can also do it in one go as follows:

    h(1) = subplot(3,1,1,'Position',[.1 .75 .2 .2])
    

    etc.

    Example output:

    enter image description here