1 |
gforget |
1.1 |
function [myColorbar]=gcmfaces_cmap_cbar(vecLimCol,varargin); |
2 |
|
|
%author : Gael Forget |
3 |
|
|
%date : May 20th 2011 |
4 |
|
|
%object : non linear colormap + colorbar |
5 |
|
|
%inputs : vecLimCol, vector of color intervals |
6 |
|
|
% [opt] cmapChoice, name of colormap |
7 |
|
|
% blankValOption, subtitute white/black at colorbar edges/center |
8 |
|
|
%output : myColorbar, the colorbar handle |
9 |
|
|
% |
10 |
|
|
%notes: - to remove a resulting colorbar, type delete(myColorbar); |
11 |
|
|
% - I need parameters for |
12 |
|
|
% the choice of cbar label text display |
13 |
|
|
% the position/orientatin of the cbar |
14 |
|
|
% the density of the color axis (may be print statement instead) |
15 |
|
|
% |
16 |
|
|
%remark2 : because this routine regenerates the colormap, |
17 |
|
|
% keeping the parameters (vecLimCol,cmapChoice) |
18 |
|
|
% constant between subplots is safer |
19 |
|
|
%remark3 : the (hard coded) choices were to not modify the parent |
20 |
|
|
% axes position, and to put the colorbar axes on its right. |
21 |
|
|
% ... It might happen that labels go out of the paper : |
22 |
|
|
% change the code below for the colorbar axes properties, |
23 |
|
|
% or change the parent axes position. |
24 |
|
|
%remark4 : the parameter nb_colors must increase with the |
25 |
|
|
% non linearity of the color scale, e.g. |
26 |
|
|
% [[0:0.5:4] [5:10] [20 30 50 100 500]]; |
27 |
|
|
|
28 |
|
|
if nargin>2; cmapChoice=varargin{1}; else; cmapChoice='jet'; end; |
29 |
|
|
if nargin>3; blankValOption=varargin{1}; else; blankValOption=0; end; |
30 |
|
|
|
31 |
|
|
%vecLimCol must be strickly increasing : |
32 |
|
|
tmp1=vecLimCol(2:end)-vecLimCol(1:end-1); |
33 |
|
|
if ~isempty(find(tmp1<=0)); fprintf('please use increasing values \n'); |
34 |
|
|
myColorbar=-1; return; end; |
35 |
|
|
|
36 |
|
|
%original colormap precision : |
37 |
|
|
%nb_colors=64*3; |
38 |
|
|
tmp1=min(vecLimCol(2:end)-vecLimCol(1:end-1)); |
39 |
|
|
tmp2=vecLimCol(end)-vecLimCol(1); |
40 |
|
|
tmp3=ceil(tmp2/tmp1/64); |
41 |
|
|
nb_colors=64*10*tmp3; |
42 |
|
|
%nb_colors=64*500*tmp3; |
43 |
|
|
|
44 |
|
|
%colormap and caxis : |
45 |
|
|
if ~isempty(strfind(cmapChoice,'FLIP')); |
46 |
|
|
eval(['tmp_map=colormap(' cmapChoice(5:end) '(nb_colors));']); |
47 |
|
|
tmp_map=flipdim(tmp_map,1); |
48 |
|
|
else; |
49 |
|
|
eval(['tmp_map=colormap(' cmapChoice '(nb_colors));']); |
50 |
|
|
end; |
51 |
|
|
tmp_val=[vecLimCol(1) vecLimCol(end)]; |
52 |
|
|
tmp_val=[tmp_val(1) : (tmp_val(2)-tmp_val(1))/(nb_colors-1) : tmp_val(2)]; |
53 |
|
|
caxis([tmp_val(1) tmp_val(end)]); |
54 |
|
|
|
55 |
|
|
%subset of colours : |
56 |
|
|
tmp_colors=round( [1:length(vecLimCol)-1] * nb_colors/(length(vecLimCol)-1) ); |
57 |
|
|
tmp_colors(1)=1; tmp_colors(end)=nb_colors; |
58 |
|
|
tmp_colors=tmp_map(tmp_colors,:); |
59 |
|
|
|
60 |
|
|
%final colormap : |
61 |
|
|
tmp_map2=tmp_map; |
62 |
|
|
for kkk=1:nb_colors |
63 |
|
|
tmp1=min(find(vecLimCol>=tmp_val(kkk))); |
64 |
|
|
if isempty(tmp1) |
65 |
|
|
tmp_map2(kkk,:)=tmp_colors(end,:); |
66 |
|
|
elseif tmp1==1 |
67 |
|
|
tmp_map2(kkk,:)=tmp_colors(1,:); |
68 |
|
|
elseif tmp1==length(vecLimCol) |
69 |
|
|
tmp_map2(kkk,:)=tmp_colors(end,:); |
70 |
|
|
else |
71 |
|
|
tmp_map2(kkk,:)=tmp_colors(tmp1-1,:); |
72 |
|
|
end |
73 |
|
|
end |
74 |
|
|
|
75 |
|
|
%to blank the center of colorscale: |
76 |
|
|
if blankValOption==1; |
77 |
|
|
tmp1=tmp_map2(floor(length(tmp_map2)/2),:); |
78 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
79 |
|
|
tmp_map2(tmp2,:)=1; |
80 |
|
|
end; |
81 |
|
|
|
82 |
|
|
%to black the lowest/highest range and blank the middle range: |
83 |
|
|
%=> if problem with colormap==gray ... |
84 |
|
|
if blankValOption==2; |
85 |
|
|
tmp1=tmp_map2(1,:); |
86 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
87 |
|
|
tmp1=tmp_map2(end,:); |
88 |
|
|
tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
89 |
|
|
tmp_map2(tmp2,:)=0; |
90 |
|
|
tmp_map2(tmp3,:)=0; |
91 |
|
|
tmp1=tmp_map2(floor(length(tmp_map2)/2),:); |
92 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
93 |
|
|
tmp_map2(tmp2,:)=1; |
94 |
|
|
end; |
95 |
|
|
|
96 |
|
|
%to blank the lowest range: |
97 |
|
|
if blankValOption==-1; |
98 |
|
|
tmp1=tmp_map2(1,:); |
99 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
100 |
|
|
tmp_map2(tmp2,:)=1; |
101 |
|
|
end; |
102 |
|
|
|
103 |
|
|
%to black the lowest range and blank the highest range: |
104 |
|
|
%=> if problem with colormap==gray ... |
105 |
|
|
if blankValOption==-2; |
106 |
|
|
tmp1=tmp_map2(1,:); |
107 |
|
|
tmp2=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
108 |
|
|
tmp1=tmp_map2(end,:); |
109 |
|
|
tmp3=find(sum(abs(tmp_map2-ones(length(tmp_map2),1)*tmp1),2)==0); |
110 |
|
|
tmp_map2(tmp2,:)=0; |
111 |
|
|
tmp_map2(tmp3,:)=1; |
112 |
|
|
end; |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
colormap(tmp_map2); |
116 |
|
|
|
117 |
|
|
%colorbar : |
118 |
|
|
aaa=gca; |
119 |
|
|
tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3]))+0.01 tmp1(2) 0.02 tmp1(4)]; |
120 |
|
|
%tmp1=get(aaa,'Position'); tmp1=[sum(tmp1([1 3])) tmp1(2) 0.01 tmp1(4)]; |
121 |
|
|
myColorbar=axes('position',tmp1); |
122 |
|
|
% set(myColorbar,'Position',[0.915 0.26 0.03 0.52]);%to account for m_map |
123 |
|
|
set(myColorbar,'Position',tmp1);%to account for m_map |
124 |
|
|
|
125 |
|
|
tmp1=[1:2]'*ones(1,length(vecLimCol)); |
126 |
|
|
tmp2=[1:length(vecLimCol)]; tmp2=[tmp2;tmp2]; |
127 |
|
|
tmp3=[0.5*( vecLimCol(2:end)+vecLimCol(1:end-1) ) vecLimCol(end)]; tmp3=[tmp3;tmp3]; |
128 |
|
|
|
129 |
|
|
pcolor(tmp1,tmp2,tmp3); caxis([vecLimCol(1) vecLimCol(end)]); |
130 |
|
|
set(myColorbar,'YAxisLocation','right'); |
131 |
|
|
set(myColorbar,'XTick',[]); |
132 |
|
|
set(myColorbar,'YTick',[1:length(vecLimCol)]); |
133 |
|
|
|
134 |
|
|
%straight labels: |
135 |
|
|
%--------------- |
136 |
|
|
if 1; |
137 |
|
|
%set(myColorbar,'YTickLabel',num2str(vecLimCol')); |
138 |
|
|
|
139 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.1e',vecLimCol(kcur))); end; |
140 |
|
|
%%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%i',vecLimCol(kcur))); end; |
141 |
|
|
%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.3f',vecLimCol(kcur))); end; |
142 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end; |
143 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2g',vecLimCol(kcur))); end; |
144 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end; |
145 |
|
|
% tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%4.3f',vecLimCol(kcur))); end; |
146 |
|
|
%%tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%5.4f',vecLimCol(kcur))); end; |
147 |
|
|
|
148 |
|
|
%tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded |
149 |
|
|
else; |
150 |
|
|
|
151 |
|
|
test1=max(abs(round(vecLimCol)-vecLimCol)); |
152 |
|
|
test2=max(abs(round(vecLimCol*100)-vecLimCol*100)); |
153 |
|
|
if ~test1; |
154 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur))); end; |
155 |
|
|
elseif ~test2; |
156 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol(kcur))); end; |
157 |
|
|
else; |
158 |
|
|
test3=1; ii=abs(ceil(log10(max(abs(vecLimCol))))); |
159 |
|
|
while test3>1e-6; |
160 |
|
|
test3=max(abs(round(vecLimCol*10^ii)-vecLimCol*10^ii)); |
161 |
|
|
if test3>1e-6; ii=ii+1; end; |
162 |
|
|
end; |
163 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.0f',vecLimCol(kcur)*10^ii)); end; |
164 |
|
|
title(['x 10^{-' num2str(ii) '}'],'FontSize',16); |
165 |
|
|
end; |
166 |
|
|
tmp2(1,:)=' '; tmp2(end,:)=' '; %do not display the last value, as it can be exceeded |
167 |
|
|
|
168 |
|
|
end |
169 |
|
|
|
170 |
|
|
tmp2(1,:)=' '; %do not display the first value |
171 |
|
|
tmp2(end,:)=' '; %do not display the last value |
172 |
|
|
|
173 |
|
|
%scaled labels: |
174 |
|
|
%------------- |
175 |
|
|
if 0; |
176 |
|
|
vecLimCol2=floor(log10(max(abs(vecLimCol)))); |
177 |
|
|
fprintf(['cbar labels in 10^' num2str(vecLimCol2) '\n']); |
178 |
|
|
vecLimCol2=vecLimCol*10^(-vecLimCol2); |
179 |
|
|
tmp2=''; for kcur=1:length(vecLimCol); tmp2=strvcat(tmp2,sprintf('%3.2f',vecLimCol2(kcur))); end; |
180 |
|
|
end; |
181 |
|
|
|
182 |
|
|
set(myColorbar,'YTickLabel',tmp2); |
183 |
|
|
|
184 |
|
|
|
185 |
|
|
axes(aaa); |
186 |
|
|
|
187 |
|
|
|