4
votes

Note: This issue occurs in 2016b in my case.

I try to make a figure with 3x4 subplots together, with tick labels only on the leftmost and bottom subplots. However, when I run the code below, the first plot seems to change size:

figure
hold on
n = 12;
ax = gobjects(n,1);
for k = 1:n
pos = [0.1+0.2*(mod(k-1,4)), 0.65-0.3*floor(k/4-0.01), 0.2, 0.3]
ax(k) = subplot(3,4,k,'Position',pos);
end
set(ax(1:8),'XTick',[])
set(ax([2:4 6:8 10:12]),'YTick',[])

Note that I am printing pos. The output for the first subplot is

pos =

    0.1000    0.6500    0.2000    0.3000

but when I then double check the position of the first subplot...

ax(1).Position

ans =

    0.1300    0.7093   0.1566    0.2157

and the plot looks like this: enter image description here

Furthermore, I have tried to manipulate the position of the first subplot afterwards, but it just makes it worse, see below:

ax(1).Position = [0.1000, 0.6500, 0.2000, 0.3000];

enter image description here

All help appreciated!

1
Can not reproduce in MATLAB 2014b. When I run your code, I get a nice mesh of subplots, all of them tidy. - Ander Biguri
Try resizing the figure window - Mad Physicist
Also not reproducible in 2016a . This is what I get: i.stack.imgur.com/xFSnb.jpg - Sardar Usama
I see. Thank you for your input! I should have mentioned, the issue occurs in 2016b in my case. - jkazan
Also note reproducible in 2015b, I get the same as @Sardar_Usama - Wolfie

1 Answers

3
votes

I can reproduce the problem in R2016b. Interestingly, it works when you do this:

figure
hold on
n = 12;
ax = gobjects(n,1);
for k = 1:n
    pos = [0.1+0.2*(mod(k-1,4)), 0.65-0.3*floor(k/4-0.01), 0.2, 0.3]
    ax(k) = subplot(3,4,k);
    set(ax(k),'Position',pos);
end
set(ax(1:8),'XTick',[])
set(ax([2:4 6:8 10:12]),'YTick',[])

The only change is that I first create the subplot and then set its position.

I'm not entirely sure of the reason. However, comparing all the fields of the axes in both versions I noticed that in your original version, the first axis has a PlotBoxAspectRatio of [0.9670 1.0000 0.9670] whereas all other ones have a PlotBoxAspectRatio of [0.8889 1.0000 0.8889]. In my "fixed" version, all axes have the PlotBoxAspectRatio of [0.8889 1.0000 0.8889]. Not sure what exactly is happening there.