为什么独立的子图会相互覆盖?为什么绘图的顺序很重要?

Why do independent subplots overwrite each other? Why is the order of plotting important?

我正在写一个需要精确定位一些子图的图形用户界面:

h = figure('Units','pixels','Position',[1920 432  1881 982]*0.5);

ax.VSize = 815.3800*0.5
ax.VOffset = 48.9228*0.5
ax.HSize = 1.8063e+03*0.5
ax.HOffset = 56.4480*0.5

subax.VSize = 0.33*(ax.VSize-3*ax.VOffset);
subax.HSize = ax.HSize;
subplot(313,'units','pixels','Position',[ax.HOffset ax.VOffset ax.HSize subax.VSize ])
subplot(312,'units','pixels','Position',[ax.HOffset 2*ax.VOffset+subax.VSize subax.HSize subax.VSize])
subplot(311,'units','pixels','Position',[ax.HOffset 3*ax.VOffset+2*subax.VSize subax.HSize subax.VSize])

这给了我想要的结果:

但是,当我将子图的顺序更改为:

subplot(311,'units','pixels','Position',[ax.HOffset 3*ax.VOffset+2*subax.VSize subax.HSize subax.VSize])
subplot(312,'units','pixels','Position',[ax.HOffset 2*ax.VOffset+subax.VSize subax.HSize subax.VSize])
subplot(313,'units','pixels','Position',[ax.HOffset ax.VOffset ax.HSize subax.VSize ])

我刚刚绘制了最后一个子图:

我真的不明白为什么会这样? 为什么子图会互相覆盖?

不幸的是,绘图的顺序对我来说很重要,所以我该如何解决这个问题?


有趣的是,我无法用其他数字或子图排列重现该问题,例如:

subax.VSize = 0.5*(ax.VSize-2*ax.VOffset);
subax.HSize = 0.5*(ax.HSize-2*ax.HOffset);
subplot(221,'units','pixels','Position',[ax.HOffset 2*ax.VOffset+subax.VSize subax.HSize subax.VSize])
subplot(222,'units','pixels','Position',[2*ax.HOffset+subax.HSize 2*ax.VOffset+subax.VSize subax.HSize subax.VSize])
subplot(223,'units','pixels','Position',[ax.HOffset ax.VOffset subax.HSize subax.VSize ])
subplot(224,'units','pixels','Position',[2*ax.HOffset+subax.HSize ax.VOffset subax.HSize subax.VSize ])

原因我还不是很清楚,不过here据说是:

Per the doc, if you overwrite the area of a subplot, it deletes the existing one and creates a new one. AFAIK there's no way to avoid this behavior. There's an example of an overlaying axes with subplot() in the documentation; note that it avoids this by specifying that axes with axes, not another subplot.

You can probably get by with yours if you only refer to the two axes with the saved handles once they're created.

建议的解决方案也适用于我的情况:

s(1) = subplot(311,'units','pixels')
s(2) = subplot(312,'units','pixels')
s(3) = subplot(313,'units','pixels')

set(s(1),'Position',[ax.HOffset 3*ax.VOffset+2*subax.VSize subax.HSize subax.VSize])
set(s(2),'Position',[ax.HOffset 2*ax.VOffset+subax.VSize subax.HSize subax.VSize])
set(s(3),'Position',[ax.HOffset ax.VOffset ax.HSize subax.VSize ])

然而,当我指定每个子图的确切位置时,子图的全部目的就消失了,所以我将只使用:

axes('units','pixels','Position',floor([ax.HOffset 3*ax.VOffset+2*subax.VSize subax.HSize subax.VSize]));
axes('units','pixels','Position',floor([ax.HOffset 2*ax.VOffset+subax.VSize subax.HSize subax.VSize]));
axes('units','pixels','Position',floor([ax.HOffset ax.VOffset 0.96*hsize subax.VSize]));

无论如何都是奇怪的问题。