How to change the subplot positioning in an existing figure?

조회 수: 41 (최근 30일)
Lenwo
Lenwo 2017년 6월 25일
편집: dpb 2017년 6월 26일
Dear experts,
a while ago I created a figure with two subplots in the configuration subplot(1,2,x) (one row, two columns). I saved the figure as 'fig' file for later use. Now I would prefer the plots to be in subplot(2,1) (two rows, one column) configuration. I only have the .fig file and have no access to the relevant data to replot the figure.
Is there a way the accomplish this?
I opened the figure and tried the following:
hF = gcf;
hSub11 = subplot(1,2,1);
hSub12 = subplot(1,2,2);
nF = figure;
hSub21 = subplot(2,1,1);
copyobj(allchild(hSub11),hSub21)
hSub22 = subplot(2,1,2);
copyobj(allchild(hSub12),hSub22)
But besides the legends, titles and axis labels are missing, the plots show white spaces on top and bottom or left and right of the figure, especially when being resized.
Does anyone have an advice or an overall better solution for me?
Thank you very much! (I am using MATLAB 2014b)
  댓글 수: 3
Lenwo
Lenwo 2017년 6월 25일
Thanks for your reply! That's an good idea, I will try this. I hope things won't get more complicated since I am using contour plots.
dpb
dpb 2017년 6월 25일
Can you build a sample that duplicates this that can post that folks can play with easily?
I've not tried Jan's idea; I can see why it didn't do what you expected exactly as is, but it may be able to be made to do so...I'll poke around little and try something.

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2017년 6월 25일
편집: Jan 2017년 6월 25일
This swaps the positions of the two axes:
hFig = gcf;
hAxes = findobj(allchild(hFig), 'flat', 'Type', 'axes');
axesPos = get(hAxes, 'Position');
set(hAxes(1), 'Position', axesPos{2});
set(hAxes(2), 'Position', axesPos{1});
[EDITED] Thismove the first axes to the subplot(2,1,1) position, the 2nd to (2,1,2):
hFig = gcf;
hAxes = findobj(allchild(hFig), 'flat', 'Type', 'axes');
hSub = subplot(2,1,1);
set(hAxes(1), 'Position', get(hSub, 'Position'));
delete(hSub);
hSub = subplot(2,1,2);
set(hAxes(2), 'Position', get(hSub, 'Position'));
delete(hSub);
Please try it and explain, how the legends, white spaces etc. are effects.
Did you try to use the property editor? Open the figure, activate the toolbar, click on the arrow and move the objects around or resize with the mouse.
  댓글 수: 5
Lenwo
Lenwo 2017년 6월 26일
편집: Lenwo 2017년 6월 26일
Hey Jan and dpb, thank you very much, it worked out for me. I combined your answers and rescaled the axes since the countour legends were cut off. That's what I finally came up with:
hFig = gcf;
hAxes = findobj(allchild(hFig), 'flat', 'Type', 'axes');
hTempFig = figure;
for i=1:2, hAxRef(i)=subplot(2,1,3-i); end % create reference positions
set(hAxes,{'position'},{hAxRef.Position}.')
delete(hTempFig);
axPosition = hAxes(1).Position;
set(hAxes(1), 'Position', [axPosition(1) axPosition(2) axPosition(3)-0.15 axPosition(4)]);
axPosition = hAxes(2).Position;
set(hAxes(2), 'Position', [axPosition(1) axPosition(2) axPosition(3)-0.15 axPosition(4)]);
I bet there is a more efficient way for the rescaling part, but it works just fine!
Thanks again!!
dpb
dpb 2017년 6월 26일
편집: dpb 2017년 6월 26일
Welcome, Jan got me to realize it would work that way; I initially was thinking about the subplot behavior that would delete the underlying axes if another occludes an existing one's position -- but at the lower level of directly manipulating the 'position' vectors there's no checking that that occurs...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by