How to change the height of the subplots without changing x,y-positions and width?

12 views (last 30 days)
I have plotted three subplots in one figure which appeared in the upper side of the main figure window as shown. The bottom of the figure is almost empty. When I use position to set their heights, their x,y-postions and widths were also changed because position is a four-elements vector i.e. [left bottom width height]. How can I increase only the height of the subplots (not the whole figure) without changing other priorities so that the tile at the bottom became less obvious?

Accepted Answer

Walter Roberson
Walter Roberson on 22 Mar 2021
all_axes = reshape(get(gcf, 'type','axes'), 1, []);
for ax = all_axes
ax.Position(4) = 250; %or as appropriate
end
This will only change the height information without changing the lower left corner or the width.
I believe you will find it to be unsatisfactory. Positions are numbered from the bottom left corner and proceed upwards. When you increase the height without changing anything else, then they are left at the same anchor point but go even higher up... promptly running off the top. I would suggest to you that, contrary to your earlier thoughts, that you do want to change multiple position parameters. For example,
dh = 250; %or as appropriate
all_axes = reshape(get(gcf, 'type','axes'), 1, []);
for ax = all_axes
ax.Position(3:4) = ax.Position(3:4) + [-dh,dh];
end
This moves the lower limit further down the screen (closer to the bottom, which is 0) and at the same time adds an equal amount to the height, so that the top edge stays the same but the bottom gets lower.
  5 Comments
Anu
Anu on 3 Mar 2022
Thanks for your prompt reply @Walter Roberson
However the issue is not fixed the way I want. Please see attached the file. I think the problem is what you expected before "I believe you will find it to be unsatisfactory. Positions are numbered from the bottom left corner and proceed upwards. When you increase the height without changing anything else, then they are left at the same anchor point but go even higher up... promptly running off the top. I would suggest to you that, contrary to your earlier thoughts, that you do want to change multiple position parameters. For example,"
So, I tried the nex t code you suggested, I just changed the "get" to "findobj".
dh = 250; %or as appropriate
all_axes = reshape(findobj(gcf, 'type','axes'), 1, []);
for ax = all_axes
ax.Position(3:4) = ax.Position(3:4) + [-dh,dh];
end
Again I get an error, "Error setting property 'Position' of class 'Axes':
Width and height must be greater than or equal to 0"
It means that -dh in the code can't be negative. I tried positive values, but that will not help us in this scenario. Don't know how to fix it now. I am not sure whether the info is important, butin total I have 7 subplots, that I am arranging in 4 columns and 2 rows. Here, just for simplicity, I have shown 2 subplots.
Thanks.
Walter Roberson
Walter Roberson on 3 Mar 2022
Maybe something like
all_axes = reshape(findobj(gcf, 'type','axes'), 1, []);
ax_pos = cell2mat(get(all_axes, 'Position'));
ax_y = ax_pos(:,2);
ax_h = ax_pos(:,4);
ax_maxy = ax_y + ax_h;
ax_pos(:,2) = 0.05 * min(ax_maxy);
ax_pos(:,4) = ax_h - ax_pos(:,2);
ax_pos_c = num2cell(ax_pos, 2);
set(all_axes, {'Position'}, ax_pos_c);

Sign in to comment.

More Answers (0)

Categories

Find more on Axes Appearance in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!