问题
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 ])
回答1:
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.
来源:https://stackoverflow.com/questions/36846300/why-do-independent-subplots-overwrite-each-other-why-is-the-order-of-plotting-i