1

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

1
  • In subplot's documentation they explain this Commented May 26, 2016 at 18:16

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Though it probably won't make any measurable difference performance-wise, if you're passing the 'Position' value explicitly anyway there isn't really any need to even use subplot over axes.
4

You can use your same code with a slight modification in the subplot command. Try the following:

figure
subplot (3,3,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]);

subplot (3,3,4:5);
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]);

subplot (3,3,7:9);
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]);

It'll give this output:enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.