Avoid colorbar resizing image

97 次查看(过去 30 天)
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.

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 中查找有关 Title 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by