matlab discrete colorbar
January 12, 2015 at 07:15 AM | categories: matlab, octave | View Comments
You shouldn't use the default "smooth" gradients of imagesc. The aesthetic side set aside, imagesc gives the wrong impression of an infinite resolution. While we are at it, let's get rid of the default nonesense y-axis opposite direction, and enable masking (whoaa. crazy.).
verbose_disp is my version of disp/sprintf, combining the goods of both of them. You can safely remove these lines, or make up your own version of "verbosity" function.
1: % purpose : interface for imagesc for producing good 2: % heat maps 3: % syntax : myimagesc(x,y,w,minval,maxval,bin,[mask],[flag_verbose]) 4: % x,y = vectors, representing the range of x and y axis. 5: % w = typically, a field which depends on both x and y. 6: % minval, maxval = first and last values of w which are color coded. 7: % values of w which go beyond minval and maxval will be represented by the 8: % same colorcoding like minval and maxval 9: % bin - an interval of values of w which has a unique color coding 10: % mask - binary matrix for pixels that myimagesc grays out 11: % flag_verbose - work in verbose mode 12: % 13: % dependencies : verbose_disp 14: % 15: % see also: imagesc 16: 17: 18: % Copyright 2012 Avi Gozolchiani (http://tiny.cc/avigoz) 19: % This program is free software: you can redistribute it and/or modify 20: % it under the terms of the GNU General Public License as published by 21: % the Free Software Foundation, either version 3 of the License, or 22: % (at your option) any later version. 23: % 24: % This program is distributed in the hope that it will be useful, 25: % but WITHOUT ANY WARRANTY; without even the implied warranty of 26: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 27: % GNU General Public License for more details. 28: % 29: % You should have received a copy of the GNU General Public License 30: % along with this program. If not, see <http://www.gnu.org/licenses/>. 31: 32: % $Log$ 33: function h=myimagesc(x,y,w,minval,maxval,bin,varargin) 34: %test case 35: % bin=1e4;minval=-8e4;maxval=8e4; 36: % x=0.1:0.1:100; 37: % y=0:200; 38: % [xx,yy]=ndgrid(x,y); 39: % w=sin(xx).*xx.^2.*exp(0.01*yy); 40: % w(:,195:198)=-70000; 41: % w(15:18,:)=70000; 42: % myimagesc(x,y,w,minval,maxval,bin) 43: accur=1e-6; % to avoid roundoff problems when interpolating the colormap 44: %% check the inputs 45: if(~isempty(nargchk(6,8,nargin))) 46: error('esyntax : myimagesc(x,y,w,minval,maxval,bin,[mask],[flag_verbose])'); 47: end 48: nxx=length(x); 49: nyy=length(y); 50: [nxw,nyw]=size(w); 51: if(nxx~=nxw || nyy~=nyw) 52: error('dimension mismatch'); 53: end % if(nxx~=nxw || nyy~=nyw) 54: if(minval>=maxval) 55: error('minval>=maxval'); 56: end % if(minval>=maxval) 57: if(bin>=(maxval-minval)/2.0) 58: error('color range spans less than two colors'); 59: end % if(bin>=(maxval-minval)/2.0) 60: mask=zeros(nxx,nyy); 61: flag_mask=false; 62: if(nargin>6) 63: flag_mask=true; 64: mask=varargin{1}; 65: if(~all([nxx,nyy]==size(mask))) 66: error('mask dimension does not match the other matrices'); 67: end % if(~all([nxx,nyy]==size(mask))) 68: end % if(nargin>6) 69: %% parameters 70: flag_verbos=false; 71: if(nargin==8) 72: flag_verbos=varargin{2}; 73: end % if(nargin==8) 74: accur=1e-5; 75: %% prepare colormap 76: verbose_disp(flag_verbos,'myimagesc : prepare colormap'); 77: colormap('default'); 78: cmap=colormap; 79: n_origbins=size(cmap,1); 80: W_bins=minval:bin:maxval; 81: W_bins_ext=(minval-bin/2):bin:(maxval+(1+accur)*bin/2); 82: cscal=(minval:(maxval-minval)/(n_origbins-1):maxval)'; 83: newcmap=interp1(cscal,cmap,W_bins); 84: newcmap=fix((newcmap-0.5)/accur-sign(newcmap))*accur+0.5; %take care that we don't get out of the [0,1] range 85: colormap(newcmap); 86: %% plot the heat map 87: verbose_disp(flag_verbos,'myimagesc : plot the heat map'); 88: h=imagesc(x,y,w',W_bins_ext([1 length(W_bins_ext)]));hh=colorbar;grid on; 89: set(hh,'ytick',W_bins); 90: %% plot the mask, if necessary 91: verbose_disp(flag_verbos,'myimagesc : plot the mask, if necessary'); 92: if(flag_mask) 93: % see 94: % http://blogs.mathworks.com/steve/2009/02/18/image-overlay-using-transparency/ 95: % for details 96: gray_lev=0.5*ones(size(w')); % gray level for masking 97: hold on; 98: gray=cat(3,gray_lev,gray_lev,gray_lev); 99: hh=imagesc(x,y,gray); 100: hold off; 101: set(hh,'alphadata',~mask'); 102: end % if(flag_mask) 103: %% invert the y-axis 104: verbose_disp(flag_verbos,'myimagesc : invert the y-axis'); 105: set(gca,'YDir','normal');
Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.