Thursday, February 24, 2011

caxis and colorbar in Matlab


If colorbar is used before the caxis, the colorbars show the original image limits. Thus, colorbars are not accurate
When the colorbar is used after caxis. This time the colorbars are accurate.

When using caxis with pcolor in Matlab, colorbar does not update if it is defined before caxis is called. The remedy is basically use colorbar whenever a new axis limit is defined by caxis. The sample code given in Matlab's manual is modified as follows to handle the issue of interest.


figure (1); clf; 
load cape
colormap(map)
subplot(2,2,1)
image(X,'CDataMapping','scaled');  
axis image
title('Caxis = [1 192]'); colorbar;
subplot(2,2,2)
image(X,'CDataMapping','scaled')
axis image
title('Caxis = [20 192]');  
caxis([20 192]); 
colorbar;  % Should be introduced after the caxis is employed
subplot(2,2,3)
image(X,'CDataMapping','scaled')
axis image
title('Caxis = [50 192]');  
caxis([50 192]); 
colorbar;
subplot(2,2,4)
image(X,'CDataMapping','scaled')
axis image
title('Caxis = [100 192]');  
caxis([100 192]); colorbar;

1 comment:

  1. Thank you very much! was really tricky to find and therefore so helpful!

    ReplyDelete