How to resize a subplot copied to a new figure using CopyObj
9 ビュー (過去 30 日間)
古いコメントを表示
It seems easy but I don't seem to find a straight forward preivous answer to my question. I want to copy a subplot from a parent figure of 3 subplots to a new figure of only 2 subplots, however I don't know how to resize the copied subplot such that it fits a a figure of only 2 subplots (please see attached image for reference). Please help!!
I simply used the copyobj function but I don't know hwo to adjust the size:
NewFig=figure;
p(1)=subplot(2,1,1)
%generate subplot 1
p(2)=subplot(2,1,2)
subplot(2,1,2);
copyobj(p(OldFig),NewFig);
0 件のコメント
採用された回答
Dave B
2021 年 10 月 8 日
編集済み: Dave B
2021 年 10 月 8 日
This is way easier with tiledlayout and nexttile. With subplot, you can't very easily swap axes: you can use the positions of some blank axes in the new 2 plot figure, but subplot adjusts the plots to align the inner positions based on the labels etc. and things go wrong if those labels take up a lot of space. You can adjust to re-align things (really you're doing the work subplot would normally do) but it's hard to generalize that logic.
Here's how this works with tiledlayout:
f1=figure(1);
tiledlayout(f1,1,3)
a=nexttile;plot(rand(3))
b=nexttile;bar(1:10)
xlabel({'this' 'is' 'a' 'long' 'label'})
nexttile;imagesc(magic(5))
f2=figure(2);
t2=tiledlayout(f2,1,2);
copyobj(a,t2)
copyobj(b,t2)
With subplot this would be trouble, because of the long xlabel, so you'd have to do some work to tweak positions (but generalizing this is quite difficult:
f3=figure(3);
aa=subplot(1,3,1);plot(rand(3))
bb=subplot(1,3,2);bar(1:10)
xlabel({'this' 'is' 'a' 'long' 'label'})
subplot(1,3,3);imagesc(magic(5))
f4=figure(4);
km=subplot(1,2,1);
cc=copyobj(aa,f4);
cc.OuterPosition=km.OuterPosition;
delete(km);
km=subplot(1,2,2);
dd=copyobj(bb,f4);
dd.OuterPosition=km.OuterPosition;
delete(km)
%not quite right...match the Y and HEIGHT of the second axes
cc.InnerPosition([2 4])=dd.InnerPosition([2 4]);
One last note - you can even copy an old subplot into a new tiledlayout, just set the Layout.Tile property:
f5 = figure(5);clf
t3 = tiledlayout(f5,1,2);
aaa=copyobj(aa,t3);
bbb=copyobj(bb,t3);
bbb.Layout.Tile=2;
2 件のコメント
Dave B
2021 年 10 月 8 日
Ah sorry, yes you were right on the cusp as tiledlayout started in 2019b...glad it helped!
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Subplots についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!