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 |
gforget |
1.4 |
if strcmp(myCmap,'jetBW1'); myCmap='jet'; myBW=1; end; |
56 |
|
|
if strcmp(myCmap,'jetBW2'); myCmap='jet'; myBW=2; end; |
57 |
|
|
|
58 |
gforget |
1.1 |
%vecLimCol must be strickly increasing : |
59 |
|
|
tmp1=vecLimCol(2:end)-vecLimCol(1:end-1); |
60 |
|
|
if ~isempty(find(tmp1<=0)); fprintf('please use increasing values \n'); |
61 |
|
|
myColorbar=-1; return; end; |
62 |
|
|
|
63 |
|
|
%original colormap precision : |
64 |
|
|
%nb_colors=64*3; |
65 |
|
|
tmp1=min(vecLimCol(2:end)-vecLimCol(1:end-1)); |
66 |
|
|
tmp2=vecLimCol(end)-vecLimCol(1); |
67 |
|
|
tmp3=ceil(tmp2/tmp1/64); |
68 |
|
|
nb_colors=64*10*tmp3; |
69 |
|
|
%nb_colors=64*500*tmp3; |
70 |
|
|
|
71 |
|
|
%colormap and caxis : |
72 |
gforget |
1.2 |
if ~isempty(strfind(myCmap,'FLIP')); |
73 |
|
|
eval(['tmp_map=colormap(' myCmap(5:end) '(nb_colors));']); |
74 |
gforget |
1.1 |
tmp_map=flipdim(tmp_map,1); |
75 |
|
|
else; |
76 |
gforget |
1.2 |
eval(['tmp_map=colormap(' myCmap '(nb_colors));']); |
77 |
gforget |
1.1 |
end; |
78 |
|
|
tmp_val=[vecLimCol(1) vecLimCol(end)]; |
79 |
|
|
tmp_val=[tmp_val(1) : (tmp_val(2)-tmp_val(1))/(nb_colors-1) : tmp_val(2)]; |
80 |
|
|
caxis([tmp_val(1) tmp_val(end)]); |
81 |
|
|
|
82 |
|
|
%subset of colours : |
83 |
|
|
tmp_colors=round( [1:length(vecLimCol)-1] * nb_colors/(length(vecLimCol)-1) ); |
84 |
|
|
tmp_colors(1)=1; tmp_colors(end)=nb_colors; |
85 |
|
|
tmp_colors=tmp_map(tmp_colors,:); |
86 |
|
|
|
87 |
|
|
%final colormap : |
88 |
|
|
tmp_map2=tmp_map; |
89 |
|
|
for kkk=1:nb_colors |
90 |
|
|
tmp1=min(find(vecLimCol>=tmp_val(kkk))); |
91 |
|
|
if isempty(tmp1) |
92 |
|
|
tmp_map2(kkk,:)=tmp_colors(end,:); |
93 |
|
|
elseif tmp1==1 |
94 |
|
|
tmp_map2(kkk,:)=tmp_colors(1,:); |
95 |
|
|
elseif tmp1==length(vecLimCol) |
96 |
|
|
tmp_map2(kkk,:)=tmp_colors(end,:); |
97 |
|
|
else |
98 |
|
|
tmp_map2(kkk,:)=tmp_colors(tmp1-1,:); |
99 |
|
|
end |
100 |
|
|
end |
101 |
|
|
|
102 |
|
|
%to blank the center of colorscale: |
103 |
gforget |
1.2 |
if myBW==1; |
104 |
gforget |
1.1 |
tmp1=tmp_map2(floor(length(tmp_map2)/2),:); |
105 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
106 |
|
|
tmp_map2(tmp2,:)=1; |
107 |
|
|
end; |
108 |
|
|
|
109 |
|
|
%to black the lowest/highest range and blank the middle range: |
110 |
|
|
%=> if problem with colormap==gray ... |
111 |
gforget |
1.2 |
if myBW==2; |
112 |
gforget |
1.1 |
tmp1=tmp_map2(1,:); |
113 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
114 |
|
|
tmp1=tmp_map2(end,:); |
115 |
|
|
tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
116 |
|
|
tmp_map2(tmp2,:)=0; |
117 |
|
|
tmp_map2(tmp3,:)=0; |
118 |
|
|
tmp1=tmp_map2(floor(length(tmp_map2)/2),:); |
119 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
120 |
|
|
tmp_map2(tmp2,:)=1; |
121 |
|
|
end; |
122 |
|
|
|
123 |
|
|
%to blank the lowest range: |
124 |
gforget |
1.2 |
if myBW==-1; |
125 |
gforget |
1.1 |
tmp1=tmp_map2(1,:); |
126 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
127 |
|
|
tmp_map2(tmp2,:)=1; |
128 |
|
|
end; |
129 |
|
|
|
130 |
|
|
%to black the lowest range and blank the highest range: |
131 |
|
|
%=> if problem with colormap==gray ... |
132 |
gforget |
1.2 |
if myBW==-2; |
133 |
gforget |
1.1 |
tmp1=tmp_map2(1,:); |
134 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
135 |
|
|
tmp1=tmp_map2(end,:); |
136 |
|
|
tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
137 |
|
|
tmp_map2(tmp2,:)=0; |
138 |
|
|
tmp_map2(tmp3,:)=1; |
139 |
|
|
end; |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
colormap(tmp_map2); |
143 |
|
|
|
144 |
|
|
%colorbar : |
145 |
|
|
aaa=gca; |
146 |
|
|
tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3]))+0.01 tmp1(2) 0.02 tmp1(4)]; |
147 |
|
|
%tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3])) tmp1(2) 0.01 tmp1(4)]; |
148 |
|
|
myColorbar=axes('position',tmp1); |
149 |
|
|
% set(myColorbar,'Position',[0.915 0.26 0.03 0.52]);%to account for m_map |
150 |
|
|
set(myColorbar,'Position',tmp1);%to account for m_map |
151 |
|
|
|
152 |
|
|
tmp1=[1:2]'*ones(1,length(vecLimCol)); |
153 |
|
|
tmp2=[1:length(vecLimCol)]; tmp2=[tmp2;tmp2]; |
154 |
|
|
tmp3=[0.5*( vecLimCol(2:end)+vecLimCol(1:end-1) ) vecLimCol(end)]; tmp3=[tmp3;tmp3]; |
155 |
|
|
|
156 |
|
|
pcolor(tmp1,tmp2,tmp3); caxis([vecLimCol(1) vecLimCol(end)]); |
157 |
|
|
set(myColorbar,'YAxisLocation','right'); |
158 |
|
|
set(myColorbar,'XTick',[]); |
159 |
|
|
set(myColorbar,'YTick',[1:length(vecLimCol)]); |
160 |
|
|
|
161 |
|
|
%straight labels: |
162 |
|
|
%--------------- |
163 |
|
|
if 1; |
164 |
|
|
%set(myColorbar,'YTickLabel',num2str(vecLimCol')); |
165 |
|
|
|
166 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.1e',vecLimCol(kcur))); end; |
167 |
|
|
%%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%i',vecLimCol(kcur))); end; |
168 |
|
|
%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.3f',vecLimCol(kcur))); end; |
169 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end; |
170 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2g',vecLimCol(kcur))); end; |
171 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end; |
172 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%4.3f',vecLimCol(kcur))); end; |
173 |
|
|
%%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.4f',vecLimCol(kcur))); end; |
174 |
|
|
|
175 |
|
|
%tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded |
176 |
|
|
else; |
177 |
|
|
|
178 |
|
|
test1=max(abs(round(vecLimCol)-vecLimCol)); |
179 |
|
|
test2=max(abs(round(vecLimCol*100)-vecLimCol*100)); |
180 |
|
|
if ~test1; |
181 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end; |
182 |
|
|
elseif ~test2; |
183 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end; |
184 |
|
|
else; |
185 |
|
|
test3=1; ii=abs(ceil(log10(max(abs(vecLimCol))))); |
186 |
|
|
while test3>1e-6; |
187 |
|
|
test3=max(abs(round(vecLimCol*10^ii)-vecLimCol*10^ii)); |
188 |
|
|
if test3>1e-6; ii=ii+1; end; |
189 |
|
|
end; |
190 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur)*10^ii)); end; |
191 |
|
|
title(['x 10^{-' num2str(ii) '}'],'FontSize',16); |
192 |
|
|
end; |
193 |
|
|
tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded |
194 |
|
|
|
195 |
|
|
end |
196 |
|
|
|
197 |
|
|
tmp2(1,:)=' '; %do not display the first value |
198 |
|
|
tmp2(end,:)=' '; %do not display the last value |
199 |
|
|
|
200 |
|
|
%scaled labels: |
201 |
|
|
%------------- |
202 |
|
|
if 0; |
203 |
|
|
vecLimCol2=floor(log10(max(abs(vecLimCol)))); |
204 |
|
|
fprintf(['cbar labels in 10^' num2str(vecLimCol2) '\n']); |
205 |
|
|
vecLimCol2=vecLimCol*10^(-vecLimCol2); |
206 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol2(kcur))); end; |
207 |
|
|
end; |
208 |
|
|
|
209 |
|
|
set(myColorbar,'YTickLabel',tmp2); |
210 |
|
|
|
211 |
gforget |
1.4 |
%add tag spec. to colorbar generated with this routine |
212 |
|
|
set(myColorbar,'Tag','gfCbar'); |
213 |
gforget |
1.1 |
|
214 |
|
|
axes(aaa); |
215 |
|
|
|
216 |
|
|
|