| 1 | %author : Gael Forget | 
| 2 | %date : Oct 31rst 2005 | 
| 3 | %object : non linear colormap, with associated colorbar | 
| 4 | %inputs :       -vecval = [a1 a2 ... aN], each interval [aI aI+1] | 
| 5 | %                       will be associated with a color | 
| 6 | %               -colormap_name (e.g. 'jet') | 
| 7 | %output :       -bbb, the colorbar handle | 
| 8 | function [bbb]=gael_cbar3(vecval,colormap_name); | 
| 9 |  | 
| 10 | %remark1 : to renew a colorbar generated with this routine, | 
| 11 | %       first delete it manually ... delete(bbb); | 
| 12 | %remark2 : because this routine regenerates the colormap, | 
| 13 | %       keeping the parameters (vecval,colormap_name) | 
| 14 | %       constant between subplots is safer | 
| 15 | %remark3 : the (hard coded) choices were to not modify the parent | 
| 16 | %       axes position, and to put the colorbar axes on its right. | 
| 17 | %       ... It might happen that labels go out of the paper : | 
| 18 | %       change the code below for the colorbar axes properties, | 
| 19 | %       or change the parent axes position. | 
| 20 | %remark4 : the parameter nb_colors must increase with the | 
| 21 | %       non linearity of the color scale, e.g. | 
| 22 | %       [[0:0.5:4] [5:10] [20 30 50 100 500]]; | 
| 23 |  | 
| 24 | %vecval must be strickly increasing : | 
| 25 | tmp1=vecval(2:end)-vecval(1:end-1); | 
| 26 | if ~isempty(find(tmp1<=0)); fprintf('please use increasing values \n'); | 
| 27 | bbb=-1; return; end; | 
| 28 |  | 
| 29 | %original colormap precision : | 
| 30 | %nb_colors=64*3; | 
| 31 | tmp1=min(vecval(2:end)-vecval(1:end-1)); | 
| 32 | tmp2=vecval(end)-vecval(1); | 
| 33 | tmp3=ceil(tmp2/tmp1/64); | 
| 34 | nb_colors=64*10*tmp3; | 
| 35 | %nb_colors=64*500*tmp3; | 
| 36 |  | 
| 37 | %colormap and caxis : | 
| 38 | eval(['tmp_map=colormap(' colormap_name  '(nb_colors));']); | 
| 39 | tmp_val=[vecval(1) vecval(end)]; | 
| 40 | tmp_val=[tmp_val(1) : (tmp_val(2)-tmp_val(1))/(nb_colors-1) : tmp_val(2)]; | 
| 41 | caxis([tmp_val(1) tmp_val(end)]); | 
| 42 |  | 
| 43 | %subset of colours : | 
| 44 | tmp_colors=round( [1:length(vecval)-1] * nb_colors/(length(vecval)-1) ); | 
| 45 | tmp_colors(1)=1; tmp_colors(end)=nb_colors; | 
| 46 | tmp_colors=tmp_map(tmp_colors,:); | 
| 47 |  | 
| 48 | %final colormap : | 
| 49 | tmp_map2=tmp_map; | 
| 50 | for kkk=1:nb_colors | 
| 51 | tmp1=min(find(vecval>=tmp_val(kkk))); | 
| 52 | if isempty(tmp1) | 
| 53 | tmp_map2(kkk,:)=tmp_colors(end,:); | 
| 54 | elseif tmp1==1 | 
| 55 | tmp_map2(kkk,:)=tmp_colors(1,:); | 
| 56 | elseif tmp1==length(vecval) | 
| 57 | tmp_map2(kkk,:)=tmp_colors(end,:); | 
| 58 | else | 
| 59 | tmp_map2(kkk,:)=tmp_colors(tmp1-1,:); | 
| 60 | end | 
| 61 | end | 
| 62 | colormap(tmp_map2); | 
| 63 |  | 
| 64 | %colorbar : | 
| 65 | aaa=gca; | 
| 66 | tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3]))+0.01 tmp1(2) 0.03 tmp1(4)]; | 
| 67 | bbb=axes('position',tmp1); | 
| 68 |  | 
| 69 | tmp1=[1:2]'*ones(1,length(vecval)); | 
| 70 | tmp2=[1:length(vecval)]; tmp2=[tmp2;tmp2]; | 
| 71 | tmp3=[0.5*( vecval(2:end)+vecval(1:end-1) ) vecval(end)]; tmp3=[tmp3;tmp3]; | 
| 72 |  | 
| 73 | pcolor(tmp1,tmp2,tmp3); caxis([vecval(1) vecval(end)]); | 
| 74 | set(bbb,'YAxisLocation','right'); | 
| 75 | set(bbb,'XTick',[]); | 
| 76 | set(bbb,'YTick',[1:length(vecval)]); | 
| 77 | set(bbb,'YTickLabel',num2str(vecval')); | 
| 78 |  | 
| 79 | axes(aaa); | 
| 80 |  | 
| 81 |  |