MATLAB: Colorbar in an other figure…

plot colorbar colormap

Here is a little problem I have encountered multiple times… usually I deal with it, but this time I really want it to be working.
I would like to display the colorbar (from an imagesc call) in a different figure or next to a different set of axes.
Here is an example: – I have 5 images that I rescale to the same colormap. – I do not want all the figures to show a colormap since they are all similar, but I also want all the images to look similar (in size) so that it is easier for comparison purposes.
In my opinion, the nicest thing is to do a subplot of 2×3, put the 5 images in 5 plot areas, and the colorbar in the 6th plot region.
Unfortunately, even by doing a colorbar('peer',axis_handle) it does not show in this last subplot, but rather in the plot of the actual axis_handle.
Is there really not a simple way of doing this?!
Thanks

Best Answer

The really simple way to do it is to just grab the bar with the mouse and move it. Do you want to automate this?
If you do want to automate it, you can play around with the properties of the graphics handles. Here is an example where I create the colorbar for subplot 1 and move it to subplot 2:
h1 = subplot(121);
surf(peaks(30));
cbar_axes = colorbar;
h2 = subplot(122);
%Get the relative positions of the two subplots and use to adjust colorbar position
v1 = get(h1,'OuterPosition');
v2 = get(h2,'OuterPosition');
va = get(cbar_axes,'OuterPosition');
set(cbar_axes,'OuterPosition',va+v2-v1)
axis off