Avoid colorbar resizing image

조회 수: 26 (최근 30일)
Jason
Jason 2012년 3월 2일
댓글: Shiyu Zhao 2021년 2월 20일
Hi guys -
How can I prevent an image from being resized when a colorbar is introduced? I am plotting two plots side by side, one with a line plot and one with imagesc. Before the colorbar is added the plots are exactly the same size, which I would prefer, but when I add the colorbar the imagesc plot is resized to be smaller than before. I'd like to avoid this.

채택된 답변

Image Analyst
Image Analyst 2012년 3월 2일
Jason, just try this demo code and I think all your questions will be answered:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
% Put up a plot in the first position
h1 = subplot(1,2,1);
plot(rand(15), 'LineWidth', 3);
title('Line Plot', 'FontSize', fontSize);
% Get the current axis size
originalSize1 = get(gca, 'Position')
% Put up an image in the second position
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Display the original gray scale image.
h2 = subplot(1, 2, 2);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off');
% Get the current axis size
originalSize2 = get(gca, 'Position')
uiwait(msgbox('Click OK to see a colorbar and watch how it changes both axes sizes'));
colormap('winter');
colorbar;
title('Shrunken Image with ColorBar', 'FontSize', fontSize);
% Print out the new sizes and see how they are different.
newSize1 = get(h1, 'Position')
newSize2 = get(h2, 'Position')
uiwait(msgbox('Click OK to restore the original image size'));
% Reset axes to original size.
set(h1, 'Position', originalSize1); % Can also use gca instead of h1 if h1 is still active.
set(h2, 'Position', originalSize2); % Can also use gca instead of h2 if h2 is still active.
title('Restored Image with ColorBar', 'FontSize', fontSize);
% Print out the restored sizes to verify.
restoredSize1 = get(h1, 'Position')
restoredSize2 = get(h2, 'Position')
  댓글 수: 3
Image Analyst
Image Analyst 2012년 3월 3일
You mean like suplabel?: http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel
Jason
Jason 2012년 3월 5일
Yep, that's exactly what I mean. I'll try that!

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

추가 답변 (3개)

Sindar
Sindar 2019년 3월 27일
If you set the position of the colorbar manually, the figure won't be resized. So:
cb=colorbar;
cb.Position = cb.Position + 1e-10; % or + [left, bottom, width, height] to place it where you want
Will auto-place the colorbar, then un-resize the plot.
  댓글 수: 1
Shiyu Zhao
Shiyu Zhao 2021년 2월 20일
This helps! Thanks!

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


Chad Greene
Chad Greene 2012년 3월 2일
This is sort of a clunky solution, but you can force the entire figure to be the size you want by putting this line after plotting:
set(gcf,'position',[100 100 1100 650]) % sets figure size
I believe the 100s above correspond to the position (in pixels) of the lower left corner of the figure on your screen, and 1100 and 650 are the figure width and height, respectively.
  댓글 수: 3
Jason
Jason 2012년 3월 2일
Not sure what's going on, but the addition of the line you suggested makes the figure disappear completely! I used if to point to the handle of the parent figure, and the figure simply disappeared. Super weird!
Chad Greene
Chad Greene 2012년 3월 2일
It's possible that my line placed your figure somewhere off your screen. Try running your code WITHOUT my line, then in the command line type
get(gcf,'position')
It should return some value like [50 100 500 450] (I'm making up the values). Those are the default values for the position and size of your figure. If you want your figure to be, say, 100 pixels wider, then take those values and type in
set(gcf,'position',[50 100 600 450])

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


Jason
Jason 2012년 3월 2일
For clarification, here is the code loop I use to generate i figures of 2 plots each:
for i = 1:plotPCnum
I = num2str(i);
h = figure('Units','normalized');
titlestr = char(['Scores and loadings for PC ',I]);
title(titlestr); set(gca,'Visible','off');
set(get(gca,'Title'),'Visible','on');
axes('OuterPosition',[0 0 .5 1],...
'ActivePositionProperty','outerposition');
%subplot(1,2,1); ...
box('on'); hold('all'); plot(loads_wt(:,i));
axis square; axis tight;
title(['Loadings PC ',num2str(I)]);
%subplot(1,2,2);
scores = axes('OuterPosition',[.4 0 .5 1]);
imagesc(reshape(scores_wt(:,i), [nrows ncols]));...
axis image; axis square;
set(scores,'XTick', [], 'YTick', []);
title(['Scores PC ',num2str(I)]);
colormap('jet');
colorbar
saveas(h,titlestr,'fig');
logline = char([titlestr,' has been created.']);
fprintf(fidOUT,'%s\n',logline);
end

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by