/[MITgcm]/MITgcm_contrib/gael/matlab_class/gcmfaces_maps/gcmfaces_cmap_cbar.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/matlab_class/gcmfaces_maps/gcmfaces_cmap_cbar.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (hide annotations) (download)
Wed May 2 14:55:23 2012 UTC (13 years, 3 months ago) by gforget
Branch: MAIN
Changes since 1.2: +2 -0 lines
gcmfaces_maps/gcmfaces_cmap_cbar.m
- allow for empty vector of color intervals -> just do colorbar;

1 gforget 1.1 function [myColorbar]=gcmfaces_cmap_cbar(vecLimCol,varargin);
2     %object : non linear colormap + colorbar
3     %inputs : vecLimCol, vector of color intervals
4 gforget 1.2 %optional: optional paramaters take the following form {'name',param1,param2,...}
5     % the two that are currently active are
6     % {'myCmap',myCmap} is the colormap name ('jet' by default)
7     % {'myBW',myBW} put white/black at colorbar edges/center (0 by default)
8 gforget 1.1 %output : myColorbar, the colorbar handle
9     %
10 gforget 1.2 %notes: - myBW==0 does nothing; myBW~=0 modifies the colormap as follows
11     % myBW==1; blanks the center of colorscale
12     % myBW==2; blacks the low&high ends and blanks the middle
13     % myBW==-1; blanks the low end
14     % myBW==-2; blacks the low end and blanks the high end
15     % - to remove a resulting colorbar, type delete(myColorbar);
16     % - since this routine regenerates the colormap according to
17     % vecLimCol and there can be only one colormap per figure, you
18     % dont want to change vecLimCol from one subplot to the next.
19     % - the colorbar position is hard coded (for now) as 'to the right
20     % of the parent axis'. You can change it via the myColorbar handle.
21     % - the colormap density is hard coded (for now). It may need to
22     % be increased if you specify a very non-linear colormap such as
23     % [[0:0.5:4] [5:10] [20 30 50 100 500]]; spanning 3 orders of magnitue
24    
25 gforget 1.3 if isempty(vecLimCol); myColorbar=colorbar; return; end;
26    
27 gforget 1.2 %set optional paramaters to default values
28     myCmap='jet'; myBW=0;
29     %set more optional paramaters to user defined values
30     for ii=1:nargin-1;
31     if ~iscell(varargin{ii});
32     warning('inputCheck:gcmfaces_cmap_cbar_1',...
33     ['As of june 2011, gcmfaces_cmap_cbar expects \n'...
34     ' its optional parameters as cell arrays. \n'...
35     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
36     ' Type ''help gcmfaces_cmap_cbar'' for details.']);
37     elseif ~ischar(varargin{ii}{1});
38     warning('inputCheck:gcmfaces_cmap_cbar_2',...
39     ['As of june 2011, gcmfaces_cmap_cbar expects \n'...
40     ' its optional parameters cell arrays \n'...
41     ' to start with character string. \n'...
42     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
43     ' Type ''help gcmfaces_cmap_cbar'' for details.']);
44     else;
45     if strcmp(varargin{ii}{1},'myCmap')|...
46     strcmp(varargin{ii}{1},'myBW');
47     eval([varargin{ii}{1} '=varargin{ii}{2};']);
48     else;
49     warning('inputCheck:gcmfaces_cmap_cbar_3',...
50     ['unknown option ''' varargin{ii}{1} ''' was ignored']);
51     end;
52     end;
53     end;
54 gforget 1.1
55     %vecLimCol must be strickly increasing :
56     tmp1=vecLimCol(2:end)-vecLimCol(1:end-1);
57     if ~isempty(find(tmp1<=0)); fprintf('please use increasing values \n');
58     myColorbar=-1; return; end;
59    
60     %original colormap precision :
61     %nb_colors=64*3;
62     tmp1=min(vecLimCol(2:end)-vecLimCol(1:end-1));
63     tmp2=vecLimCol(end)-vecLimCol(1);
64     tmp3=ceil(tmp2/tmp1/64);
65     nb_colors=64*10*tmp3;
66     %nb_colors=64*500*tmp3;
67    
68     %colormap and caxis :
69 gforget 1.2 if ~isempty(strfind(myCmap,'FLIP'));
70     eval(['tmp_map=colormap(' myCmap(5:end) '(nb_colors));']);
71 gforget 1.1 tmp_map=flipdim(tmp_map,1);
72     else;
73 gforget 1.2 eval(['tmp_map=colormap(' myCmap '(nb_colors));']);
74 gforget 1.1 end;
75     tmp_val=[vecLimCol(1) vecLimCol(end)];
76     tmp_val=[tmp_val(1) : (tmp_val(2)-tmp_val(1))/(nb_colors-1) : tmp_val(2)];
77     caxis([tmp_val(1) tmp_val(end)]);
78    
79     %subset of colours :
80     tmp_colors=round( [1:length(vecLimCol)-1] * nb_colors/(length(vecLimCol)-1) );
81     tmp_colors(1)=1; tmp_colors(end)=nb_colors;
82     tmp_colors=tmp_map(tmp_colors,:);
83    
84     %final colormap :
85     tmp_map2=tmp_map;
86     for kkk=1:nb_colors
87     tmp1=min(find(vecLimCol>=tmp_val(kkk)));
88     if isempty(tmp1)
89     tmp_map2(kkk,:)=tmp_colors(end,:);
90     elseif tmp1==1
91     tmp_map2(kkk,:)=tmp_colors(1,:);
92     elseif tmp1==length(vecLimCol)
93     tmp_map2(kkk,:)=tmp_colors(end,:);
94     else
95     tmp_map2(kkk,:)=tmp_colors(tmp1-1,:);
96     end
97     end
98    
99     %to blank the center of colorscale:
100 gforget 1.2 if myBW==1;
101 gforget 1.1 tmp1=tmp_map2(floor(length(tmp_map2)/2),:);
102     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
103     tmp_map2(tmp2,:)=1;
104     end;
105    
106     %to black the lowest/highest range and blank the middle range:
107     %=> if problem with colormap==gray ...
108 gforget 1.2 if myBW==2;
109 gforget 1.1 tmp1=tmp_map2(1,:);
110     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
111     tmp1=tmp_map2(end,:);
112     tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
113     tmp_map2(tmp2,:)=0;
114     tmp_map2(tmp3,:)=0;
115     tmp1=tmp_map2(floor(length(tmp_map2)/2),:);
116     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
117     tmp_map2(tmp2,:)=1;
118     end;
119    
120     %to blank the lowest range:
121 gforget 1.2 if myBW==-1;
122 gforget 1.1 tmp1=tmp_map2(1,:);
123     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
124     tmp_map2(tmp2,:)=1;
125     end;
126    
127     %to black the lowest range and blank the highest range:
128     %=> if problem with colormap==gray ...
129 gforget 1.2 if myBW==-2;
130 gforget 1.1 tmp1=tmp_map2(1,:);
131     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
132     tmp1=tmp_map2(end,:);
133     tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
134     tmp_map2(tmp2,:)=0;
135     tmp_map2(tmp3,:)=1;
136     end;
137    
138    
139     colormap(tmp_map2);
140    
141     %colorbar :
142     aaa=gca;
143     tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3]))+0.01 tmp1(2) 0.02 tmp1(4)];
144     %tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3])) tmp1(2) 0.01 tmp1(4)];
145     myColorbar=axes('position',tmp1);
146     % set(myColorbar,'Position',[0.915 0.26 0.03 0.52]);%to account for m_map
147     set(myColorbar,'Position',tmp1);%to account for m_map
148    
149     tmp1=[1:2]'*ones(1,length(vecLimCol));
150     tmp2=[1:length(vecLimCol)]; tmp2=[tmp2;tmp2];
151     tmp3=[0.5*( vecLimCol(2:end)+vecLimCol(1:end-1) ) vecLimCol(end)]; tmp3=[tmp3;tmp3];
152    
153     pcolor(tmp1,tmp2,tmp3); caxis([vecLimCol(1) vecLimCol(end)]);
154     set(myColorbar,'YAxisLocation','right');
155     set(myColorbar,'XTick',[]);
156     set(myColorbar,'YTick',[1:length(vecLimCol)]);
157    
158     %straight labels:
159     %---------------
160     if 1;
161     %set(myColorbar,'YTickLabel',num2str(vecLimCol'));
162    
163     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.1e',vecLimCol(kcur))); end;
164     %%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%i',vecLimCol(kcur))); end;
165     %tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.3f',vecLimCol(kcur))); end;
166     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end;
167     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2g',vecLimCol(kcur))); end;
168     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end;
169     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%4.3f',vecLimCol(kcur))); end;
170     %%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.4f',vecLimCol(kcur))); end;
171    
172     %tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded
173     else;
174    
175     test1=max(abs(round(vecLimCol)-vecLimCol));
176     test2=max(abs(round(vecLimCol*100)-vecLimCol*100));
177     if ~test1;
178     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end;
179     elseif ~test2;
180     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end;
181     else;
182     test3=1; ii=abs(ceil(log10(max(abs(vecLimCol)))));
183     while test3>1e-6;
184     test3=max(abs(round(vecLimCol*10^ii)-vecLimCol*10^ii));
185     if test3>1e-6; ii=ii+1; end;
186     end;
187     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur)*10^ii)); end;
188     title(['x 10^{-' num2str(ii) '}'],'FontSize',16);
189     end;
190     tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded
191    
192     end
193    
194     tmp2(1,:)=' '; %do not display the first value
195     tmp2(end,:)=' '; %do not display the last value
196    
197     %scaled labels:
198     %-------------
199     if 0;
200     vecLimCol2=floor(log10(max(abs(vecLimCol))));
201     fprintf(['cbar labels in 10^' num2str(vecLimCol2) '\n']);
202     vecLimCol2=vecLimCol*10^(-vecLimCol2);
203     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol2(kcur))); end;
204     end;
205    
206     set(myColorbar,'YTickLabel',tmp2);
207    
208    
209     axes(aaa);
210    
211    

  ViewVC Help
Powered by ViewVC 1.1.22