Avoid colorbar resizing image
60 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Image Analyst
il 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 Commenti
Image Analyst
il 3 Mar 2012
You mean like suplabel?: http://www.mathworks.com/matlabcentral/fileexchange/7772-suplabel
Più risposte (3)
Sindar
il 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
il 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 Commenti
Chad Greene
il 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])
Vedere anche
Categorie
Scopri di più su Subplots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!