Avoid colorbar resizing image

115 views (last 30 days)
Jason
Jason on 2 Mar 2012
Commented: Shiyu Zhao on 20 Feb 2021
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.

Accepted Answer

Image Analyst
Image Analyst on 2 Mar 2012
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 Comments
Image Analyst
Image Analyst on 3 Mar 2012
You mean like suplabel?: http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel
Jason
Jason on 5 Mar 2012
Yep, that's exactly what I mean. I'll try that!

Sign in to comment.

More Answers (3)

Sindar
Sindar on 27 Mar 2019
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.

Chad Greene
Chad Greene on 2 Mar 2012
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 Comments
Jason
Jason on 2 Mar 2012
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 on 2 Mar 2012
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])

Sign in to comment.


Jason
Jason on 2 Mar 2012
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

Community Treasure Hunt

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

Start Hunting!