MATLAB: Aren’t the colorbar limits set correctly when I place a colorbar on a CONTOURF plot

colorbarcontourflimitsplot

There appears to be a problem with the way COLORBAR limits are assigned when using a colorbar with the CONTOURF function. Typing the following for a CONTOURF plot displays a wrong color code.
colorbar
I have to create the right color code by hand. For example,
figure(1)
axes
hold on
imagesc([0 10],[0 6000],linspace(0,1,1000)')
axis([0 10 0 6000])
set(gca,'layer','top','pos',[.92 .725 .03 .2],...
'box','on','ygrid','on',...
'YTick',[0 2000 4000 6000],...
'YAxisLocation','right',...
'xtick',[])
title('Metres')
Is there any chance of getting colorbar to do this by itself?

Best Answer

The COLORBAR sets its range based on the value of the CLIM property which can be set with the CAXIS command. In the case of CONTOURF, the color limits of the plot do not span the entire range of data. You can use CAXIS to change this. Once you have changed this, you should issue the COLORBAR command again.
You can find more information on the CAXIS command by typing
help caxis
at the MATLAB command prompt.
Note: In MATLAB 7 and above you would need to use
contourf('v6',<data>);
instead of the usual CONTOURF function in order for this workaround to work.