Code covered by the BSD License  

Highlights from
custom colormap

image thumbnail

custom colormap

by

 

02 May 2012 (Updated )

sets the colormap according to monotonically increasing user defined ranges

customcmap(cbar_labels,user_cmap,minMaxValues)
function varargout = customcmap(cbar_labels,user_cmap,minMaxValues)
%CUSTOMCMAP sets the colormap defined by the user_cmap according to
% monotonically increasing user defined ranges cbar_lables. 
% This function is useful if you don't want to use the linear scaled 
% colormap that are the default in Matlab                   
%                                             
% USAGE:
%         CUSTOMCMAP(cbar_labels,user_cmap,minMaxValues)
%         hcb = CUSTOMCMAP(cbar_labels,user_cmap,minMaxValues)
%                                             
% INPUT:
%    cbar_labels - Vector (n x 1) of monotonically increasing ranges of data
%    user_cmap   - Matrix ((n-1) x 3) of colormap you want to see
%    minMaxValues - Vector (2 x 1) of minimum and maximum value of data
%                   that is plotted
%                                          
% OUTPUT:
%    hcb - handle to colorbar                                                                        
%        
% EXAMPLES:
% data = rand(10,10);
% minVal = min(min(data));
% maxVal = max(max(data));
% image(data,'CDataMapping','scaled');
% cbar_labels = [0 0.12 0.2 0.3 0.5 0.6 0.75 0.8 1];  % user defined ranges
% user_cmap = jet(8);  % user defined colormap
% customcmap(cbar_labels,user_cmap,[minVal maxVal])
%      
% See also: 
%
% HISTORY
% 1.0.0 - Initial release 22-Mar-2012
% 1.0.1 19-Jun-201221
%       - bug fixed on line 73

% Author:Durga Lal Shrestha
% CSIRO Land & Water, Highett, Australia
% eMail: durgalal.shrestha@gmail.com
% Website: www.durgalal.co.cc
% Copyright 2012 Durga Lal Shrestha
% $First created: 22-Mar-2012
% $Revision: 1.0.1 $ $Date: 19-Jun-2012 13:51:00 $

% ***********************************************************************
%% INPUT ARGUMENTS CHECK
error(nargchk(3,3,nargin))
% Check if the cbar_labels is a monotonically increasing 
if ~all(diff(cbar_labels)>0)
    error('customcmap:cbar_labels', 'First argument "cbar_labels" should be monotonically increasing')
end

% Check the size of input 
if ~isvector(cbar_labels) || isscalar(minMaxValues)
  error('customcmap:cbar_labels', 'First and third arguments should be vector')
end  

n = length(cbar_labels);
if ~isempty(user_cmap) && n~=size(user_cmap,1)+1
    error('customcmap:cbar_labels', 'Length of user_cmap should be equal to length of cbar_labels - 1')
end

minVal = minMaxValues(1);
maxVal = minMaxValues(2);


% Check if the cbar_labels are outside the minimum or maximum value of data and remove them 
ind = find(minVal>cbar_labels);
if ~isempty(ind)
    cbar_labels(1:ind(end)-1)=[];
end

ind1 = find(maxVal<cbar_labels);
if ~isempty(ind1)
    cbar_labels(ind1(1)+1:end)=[];
end

% Adjust user_cmap according to the cbar_labels if they are outside
if nargin <2 || isempty(user_cmap)
    user_cmap = jet(length(cbar_labels)-1);
else
    if ~isempty(ind)
        user_cmap(1:ind(end)-1,:)=[];
    end
    if ~isempty(ind1)
        user_cmap(ind1(1)+1:end,:)=[];
    end
end

% Adjust cbar_labels to max and min value of data
cbar_labels(1)=minVal;
cbar_labels(end)= maxVal;

len = sum(diff(cbar_labels));
f= 256/len;           % here 256 is the maximum number of colorbar
len = floor(diff(cbar_labels).*f); % because of rounding error,
% colorbar would not be exact

cmap=[];
for i=1:length(len)
    cmap = cat(1,cmap,repmat(user_cmap(i,:),len(i),1));      
end
set(gcf,'Colormap',cmap);
caxis([minVal maxVal])
hcb=colorbar('peer',gca,'YGrid','on',...
    'GridLineStyle','-');
set(hcb,'YTickMode','manual','YTick',cbar_labels,'YTickLabel',cbar_labels);

if nargout==1
    varargout{1}=hcb;
end

Contact us