为什么独立的子图会互相覆盖?(Why do independent subplots overwrite each other? Why is the order of plotting import

编程入门 行业动态 更新时间:2024-10-27 17:11:16
为什么独立的子图会互相覆盖?(Why do independent subplots overwrite each other? Why is the order of plotting important?)

我正在写一个gui,需要精确定位一些子图:

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 ])

I'm writing a gui which requires to exactly position some subplots:

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])

which gives me the desired results:

However, when I change the order of the subplots to:

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 ])

I just get the last subplot plotted:

I really don't see why this is happening? Why do the subplots overwrite each other?

Unfortunately the order of plotting is important to me, so how can I fix this?


Funnily, I can't reproduce the problem with another number or arrangement of subplots, like:

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 ])

最满意答案

原因目前还不完全清楚,但这里说:

根据文档,如果覆盖子区域的面积,它将删除现有的区域并创建一个新区域。 AFAIK无法避免这种行为。 在文档中有一个覆盖轴与子图()的示例; 请注意,它通过用轴指定轴来避免这种情况,而不是另一个子图。

如果您只是在创建后才使用已保存的手柄来引用两个坐标轴,那么您可能会与您的坐标一致。

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

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]));

无论如何,奇怪的问题。

The reason is still not entirely clear to me, but here is said:

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.

The suggested solution also works in my case:

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 ])

However, as I specify the exact position of every subplot, the whole purpose of subplots is gone, so I will just use:

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]));

Weird problem anyhow.

更多推荐

本文发布于:2023-07-31 14:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1345281.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:独立   independent   子图会   subplots   important

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!