/[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.2 - (hide annotations) (download)
Wed Jun 22 21:51:38 2011 UTC (14 years, 1 month ago) by gforget
Branch: MAIN
Changes since 1.1: +53 -31 lines
- make the arguments handling of m_map_gcmfaces.m and gcmfaces_cmap_cbar.m
  more generic and safe (using the {'myParam',myParam} approach).
- set the colorbar position when using m_map_gcmfaces.m with gcmfaces_cmap_cbar.m
- update plot_one_field.m accordingly, and add a test case using gcmfaces_cmap_cbar.m

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     %set optional paramaters to default values
26     myCmap='jet'; myBW=0;
27     %set more optional paramaters to user defined values
28     for ii=1:nargin-1;
29     if ~iscell(varargin{ii});
30     warning('inputCheck:gcmfaces_cmap_cbar_1',...
31     ['As of june 2011, gcmfaces_cmap_cbar expects \n'...
32     ' its optional parameters as cell arrays. \n'...
33     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
34     ' Type ''help gcmfaces_cmap_cbar'' for details.']);
35     elseif ~ischar(varargin{ii}{1});
36     warning('inputCheck:gcmfaces_cmap_cbar_2',...
37     ['As of june 2011, gcmfaces_cmap_cbar expects \n'...
38     ' its optional parameters cell arrays \n'...
39     ' to start with character string. \n'...
40     ' Argument no. ' num2str(ii+1) ' was ignored \n'...
41     ' Type ''help gcmfaces_cmap_cbar'' for details.']);
42     else;
43     if strcmp(varargin{ii}{1},'myCmap')|...
44     strcmp(varargin{ii}{1},'myBW');
45     eval([varargin{ii}{1} '=varargin{ii}{2};']);
46     else;
47     warning('inputCheck:gcmfaces_cmap_cbar_3',...
48     ['unknown option ''' varargin{ii}{1} ''' was ignored']);
49     end;
50     end;
51     end;
52 gforget 1.1
53     %vecLimCol must be strickly increasing :
54     tmp1=vecLimCol(2:end)-vecLimCol(1:end-1);
55     if ~isempty(find(tmp1<=0)); fprintf('please use increasing values \n');
56     myColorbar=-1; return; end;
57    
58     %original colormap precision :
59     %nb_colors=64*3;
60     tmp1=min(vecLimCol(2:end)-vecLimCol(1:end-1));
61     tmp2=vecLimCol(end)-vecLimCol(1);
62     tmp3=ceil(tmp2/tmp1/64);
63     nb_colors=64*10*tmp3;
64     %nb_colors=64*500*tmp3;
65    
66     %colormap and caxis :
67 gforget 1.2 if ~isempty(strfind(myCmap,'FLIP'));
68     eval(['tmp_map=colormap(' myCmap(5:end) '(nb_colors));']);
69 gforget 1.1 tmp_map=flipdim(tmp_map,1);
70     else;
71 gforget 1.2 eval(['tmp_map=colormap(' myCmap '(nb_colors));']);
72 gforget 1.1 end;
73     tmp_val=[vecLimCol(1) vecLimCol(end)];
74     tmp_val=[tmp_val(1) : (tmp_val(2)-tmp_val(1))/(nb_colors-1) : tmp_val(2)];
75     caxis([tmp_val(1) tmp_val(end)]);
76    
77     %subset of colours :
78     tmp_colors=round( [1:length(vecLimCol)-1] * nb_colors/(length(vecLimCol)-1) );
79     tmp_colors(1)=1; tmp_colors(end)=nb_colors;
80     tmp_colors=tmp_map(tmp_colors,:);
81    
82     %final colormap :
83     tmp_map2=tmp_map;
84     for kkk=1:nb_colors
85     tmp1=min(find(vecLimCol>=tmp_val(kkk)));
86     if isempty(tmp1)
87     tmp_map2(kkk,:)=tmp_colors(end,:);
88     elseif tmp1==1
89     tmp_map2(kkk,:)=tmp_colors(1,:);
90     elseif tmp1==length(vecLimCol)
91     tmp_map2(kkk,:)=tmp_colors(end,:);
92     else
93     tmp_map2(kkk,:)=tmp_colors(tmp1-1,:);
94     end
95     end
96    
97     %to blank the center of colorscale:
98 gforget 1.2 if myBW==1;
99 gforget 1.1 tmp1=tmp_map2(floor(length(tmp_map2)/2),:);
100     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
101     tmp_map2(tmp2,:)=1;
102     end;
103    
104     %to black the lowest/highest range and blank the middle range:
105     %=> if problem with colormap==gray ...
106 gforget 1.2 if myBW==2;
107 gforget 1.1 tmp1=tmp_map2(1,:);
108     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
109     tmp1=tmp_map2(end,:);
110     tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
111     tmp_map2(tmp2,:)=0;
112     tmp_map2(tmp3,:)=0;
113     tmp1=tmp_map2(floor(length(tmp_map2)/2),:);
114     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
115     tmp_map2(tmp2,:)=1;
116     end;
117    
118     %to blank the lowest range:
119 gforget 1.2 if myBW==-1;
120 gforget 1.1 tmp1=tmp_map2(1,:);
121     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
122     tmp_map2(tmp2,:)=1;
123     end;
124    
125     %to black the lowest range and blank the highest range:
126     %=> if problem with colormap==gray ...
127 gforget 1.2 if myBW==-2;
128 gforget 1.1 tmp1=tmp_map2(1,:);
129     tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
130     tmp1=tmp_map2(end,:);
131     tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0);
132     tmp_map2(tmp2,:)=0;
133     tmp_map2(tmp3,:)=1;
134     end;
135    
136    
137     colormap(tmp_map2);
138    
139     %colorbar :
140     aaa=gca;
141     tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3]))+0.01 tmp1(2) 0.02 tmp1(4)];
142     %tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3])) tmp1(2) 0.01 tmp1(4)];
143     myColorbar=axes('position',tmp1);
144     % set(myColorbar,'Position',[0.915 0.26 0.03 0.52]);%to account for m_map
145     set(myColorbar,'Position',tmp1);%to account for m_map
146    
147     tmp1=[1:2]'*ones(1,length(vecLimCol));
148     tmp2=[1:length(vecLimCol)]; tmp2=[tmp2;tmp2];
149     tmp3=[0.5*( vecLimCol(2:end)+vecLimCol(1:end-1) ) vecLimCol(end)]; tmp3=[tmp3;tmp3];
150    
151     pcolor(tmp1,tmp2,tmp3); caxis([vecLimCol(1) vecLimCol(end)]);
152     set(myColorbar,'YAxisLocation','right');
153     set(myColorbar,'XTick',[]);
154     set(myColorbar,'YTick',[1:length(vecLimCol)]);
155    
156     %straight labels:
157     %---------------
158     if 1;
159     %set(myColorbar,'YTickLabel',num2str(vecLimCol'));
160    
161     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.1e',vecLimCol(kcur))); end;
162     %%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%i',vecLimCol(kcur))); end;
163     %tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.3f',vecLimCol(kcur))); end;
164     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end;
165     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2g',vecLimCol(kcur))); end;
166     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end;
167     % tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%4.3f',vecLimCol(kcur))); end;
168     %%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.4f',vecLimCol(kcur))); end;
169    
170     %tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded
171     else;
172    
173     test1=max(abs(round(vecLimCol)-vecLimCol));
174     test2=max(abs(round(vecLimCol*100)-vecLimCol*100));
175     if ~test1;
176     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end;
177     elseif ~test2;
178     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end;
179     else;
180     test3=1; ii=abs(ceil(log10(max(abs(vecLimCol)))));
181     while test3>1e-6;
182     test3=max(abs(round(vecLimCol*10^ii)-vecLimCol*10^ii));
183     if test3>1e-6; ii=ii+1; end;
184     end;
185     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur)*10^ii)); end;
186     title(['x 10^{-' num2str(ii) '}'],'FontSize',16);
187     end;
188     tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded
189    
190     end
191    
192     tmp2(1,:)=' '; %do not display the first value
193     tmp2(end,:)=' '; %do not display the last value
194    
195     %scaled labels:
196     %-------------
197     if 0;
198     vecLimCol2=floor(log10(max(abs(vecLimCol))));
199     fprintf(['cbar labels in 10^' num2str(vecLimCol2) '\n']);
200     vecLimCol2=vecLimCol*10^(-vecLimCol2);
201     tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol2(kcur))); end;
202     end;
203    
204     set(myColorbar,'YTickLabel',tmp2);
205    
206    
207     axes(aaa);
208    
209    

  ViewVC Help
Powered by ViewVC 1.1.22