Plot title being placed and then erased, and subplots different sizes

조회 수: 12 (최근 30일)
Hey folks -
This one's a problem with a loop I wrote to plot a handful of figures, each with two subplots, and I can't seem to figure out how to keep the title above the subplots from disappearing. Here's my code for the loop:
for i = 1:plotPCnum
I = num2str(i);
h = figure;
titlestr = char(['Scores and loadings for PC ',I]);
title(titlestr);
subplot(1,2,1); ...
box('on'); hold('all'); plot(loads_wt(:,i)); axis square; axis tight;
title(['Loadings PC ',num2str(I)]);...
set(axes, 'XTick', [], 'YTick', []);
subplot(1,2,2); imagesc(reshape(scores_wt(:,i), [nrows ncols]));...
axis image; title(['Scores PC ',num2str(I)]); colormap('jet'); colorbar
set(gca, 'XTick', [], 'YTick', []);
saveas(h,titlestr,'fig');
logline = char([titlestr,' has been created.']);
fprintf(fidOUT,'%s\n',logline);
end
What happens is that at the top right after I open the figure h and give it a title, as soon as the first subplot is opened that title disappears, and all I get is a series of plots each with 2 subplots with the subplot titles only. How can I code the figure title above the subplot titles so it doesn't disappear?
Also, the subplots are different sizes and I would like them to be the same size. How can I force this?
Thanks,
Jason

채택된 답변

Tom Lane
Tom Lane 2012년 3월 1일
Take a look at this:
title('main title');
set(gca,'Visible','off')
set(get(gca,'Title'),'Visible','on')
axes('OuterPosition',[0 0 .5 1]);
The first few lines create axes that hold the main title, then make the axes invisible except for their title. The last line creates a smaller plot directly, overcoming the subplot command's desire to clear out stuff in the way.
As for your other question, one issue is that the second plot is leaving room for the colorbar. You could get(ax,'Position') for each axes and adjust their widths and heights to make them as you want.

추가 답변 (2개)

Jason
Jason 2012년 3월 1일
To clarify a bit I would like titlestr to be the plot title that is centered above the subplots and their titles.
  댓글 수: 1
Image Analyst
Image Analyst 2012년 3월 3일
You mean like suplabel does it?
http://www.mathworks.com/matlabcentral/fileexchange/?term=suplabel

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


Jason
Jason 2012년 3월 1일
Sorry Tom, that didn't work. As soon as subplot(1,2,1) is called, the firgure title disappears.
  댓글 수: 3
Jason
Jason 2012년 3월 2일
Ah, I see. I'll try that...
Jason
Jason 2012년 3월 2일
That's the ticket! Once I place the "subplot" where I want it with axes('OuterPosition'), all is well with the figure title. Thanks for the help, Tom!

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by