FCOLORBAR Display colorbar with discrete color axis for filled contour plot CALL: [h0,h1] = fcolorbar(C,L,label,W,hand); [h0,h1] = fcolorbar(V,L,label,W,hand); V = The contour-level-specification vector used when making the CONTOURF-plot (the most robust method), C = The contour-matrix as described in CONTOURC L = String giving location of colorbar: 't'op 'b'ottom 'r'ight (default) 'l'eft 'o'utside (when rescaling of graph is undesireable) 'a'lone (in a subplot when it refers to several graphs in a subplot-figure) label = string for the color-axis-label (default= empty) W = Relative width (ratio of graph width/height) (default= 1/20) hand = handles to axes which the colorbar refers to. Use this to ensure the right colorspan on all graphs and their colorbar. [h0,h1] are handles to the graph and to the colorbar axes, respectively. When contours (V) are given, the colorlimit ('Clim') property of both the plot and the colorbar is locked to the lower and upper contour level. This should solve the problem of how to keep the same color-to-data relationship from plot to plot. As a general rule, place the colorbar on a side of the graph that has no ticklabels or title (right is this function's default). NOTE : CONTOURF scales CAXIS from the lowest input contourlevel to the highest given contourlevel or max(max(data)) whichever is greatest!!! :-( FCOLORBAR locks the coloraxis of both plot and colorbar between the first and last contourlevel, to ensure consistency. When several axes 'share' same colorbar, the 'clim' property of all axes should be set equal to the colorbar, by input of their handles in 'hand'! Examples: % Difference from the ordinary colorbar-function: [x,y,z]=peaks; v=-10:2:8; figure(1); [cs,h]=contourf(x,y,z,v); clabel(cs,h); colorbar figure(2); [cs,h]=contourf(x,y,z,v); clabel(cs,h); fcolorbar(v); % And not using contourspecification: figure(3); [cs,h]=contourf(x,y,z); clabel(cs,h); fcolorbar(cs); % Not equidistant contours. figure(4); v=[-8 -4 -2 -1 0 1 2 4 8]; [cs,h]=contourf(x,y,z,v); clabel(cs,h); fcolorbar(v); See also contourf, clevels
Extract the contour levels from the contour matrix | |
Create axes in arbitrary positions. | |
Filled contour plot. | |
Handles to figure children that contain data. | |
Delete file or graphics object. | |
Difference and approximate derivative. | |
Display message and abort function. | |
Find objects with specified property values. | |
Find one string within another. | |
Get handle to current axis. | |
Get handle to current callback object. | |
Get handle to current figure. | |
Get object properties. | |
True for character array (string). | |
True if arrays are numerically equal. | |
True if field is in structure array. | |
True for graphics handles. | |
Display legend. | |
Convert string to lowercase. | |
Average or mean value. | |
X and Y arrays for 3-D plots. | |
Convert number to string. (Fast version) | |
Set object properties. | |
Compare strings. | |
Compare strings ignoring case. | |
Text annotation. | |
Set unique. | |
X-axis label. | |
Y-axis label. |
0001 function [h00,h1]=fcolorbar(D,L,label,W,hand) 0002 %FCOLORBAR Display colorbar with discrete color axis for filled contour plot 0003 % 0004 % CALL: [h0,h1] = fcolorbar(C,L,label,W,hand); 0005 % [h0,h1] = fcolorbar(V,L,label,W,hand); 0006 % 0007 % V = The contour-level-specification vector used when making the 0008 % CONTOURF-plot (the most robust method), 0009 % C = The contour-matrix as described in CONTOURC 0010 % L = String giving location of colorbar: 0011 % 't'op 0012 % 'b'ottom 0013 % 'r'ight (default) 0014 % 'l'eft 0015 % 'o'utside (when rescaling of graph is undesireable) 0016 % 'a'lone (in a subplot when it refers to several 0017 % graphs in a subplot-figure) 0018 % label = string for the color-axis-label (default= empty) 0019 % W = Relative width (ratio of graph width/height) (default= 1/20) 0020 % hand = handles to axes which the colorbar refers to. Use this to 0021 % ensure the right colorspan on all graphs and their colorbar. 0022 % 0023 % [h0,h1] are handles to the graph and to the colorbar axes, respectively. 0024 % 0025 % When contours (V) are given, the colorlimit ('Clim') property of 0026 % both the plot and the colorbar is locked to the lower and upper 0027 % contour level. This should solve the problem of how to keep the same 0028 % color-to-data relationship from plot to plot. 0029 % 0030 % As a general rule, place the colorbar on a side of the graph that has 0031 % no ticklabels or title (right is this function's default). 0032 % 0033 % NOTE : CONTOURF scales CAXIS from the lowest input contourlevel to the 0034 % highest given contourlevel or max(max(data)) whichever is 0035 % greatest!!! :-( 0036 % FCOLORBAR locks the coloraxis of both plot and colorbar between 0037 % the first and last contourlevel, to ensure consistency. When 0038 % several axes 'share' same colorbar, the 'clim' property of all 0039 % axes should be set equal to the colorbar, by input of their 0040 % handles in 'hand'! 0041 % 0042 % Examples: % Difference from the ordinary colorbar-function: 0043 % 0044 % [x,y,z]=peaks; v=-10:2:8; 0045 % figure(1); [cs,h]=contourf(x,y,z,v); clabel(cs,h); colorbar 0046 % figure(2); [cs,h]=contourf(x,y,z,v); clabel(cs,h); fcolorbar(v); 0047 % % And not using contourspecification: 0048 % figure(3); [cs,h]=contourf(x,y,z); clabel(cs,h); fcolorbar(cs); 0049 % 0050 % % Not equidistant contours. 0051 % figure(4); v=[-8 -4 -2 -1 0 1 2 4 8]; 0052 % [cs,h]=contourf(x,y,z,v); clabel(cs,h); fcolorbar(v); 0053 % 0054 % See also contourf, clevels 0055 0056 0057 % ### Updates: ### 0058 % revised pab 31.12.2002 0059 % -possible to give a single contour-level specification 0060 % -added deleteProxy object 0061 % -renamed from ecolorbar to fcolorbar 0062 % -moved some code into separate functions 0063 % revised pab 20.08.2001 0064 % - streamlined some code. 0065 % revised pab 03.07.2001 0066 % - changed help header to WAFO style. 0067 % 00.10.17: Added options for "outside"- and "alone"-positioning 0068 % 99.11.19: I think I found the solution for the color-span problem. 0069 % 0070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0071 % modified from my_colorbar by Tore Furevik, Geophysical Institute, 0072 % University of Bergen. 0073 % ECOLORBAR by Jan Even Nilsen: 0074 %Time-stamp:<Last updated on 01/01/30 at 12:04:59 by even@gfi.uib.no> 0075 %File:<d:/home/matlab/ecolorbar.m> 0076 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0077 0078 error(nargchk(1,5,nargin)); 0079 0080 % Catch fcolorbar('delete') special case -- must be called by the deleteFcn. 0081 if nargin==1 & strcmp(D,'delete'), 0082 ax = gcbo; 0083 if strcmp(get(ax,'tag'),'ColorbarDeleteProxy') 0084 cbo = ax; 0085 ax = get(cbo,'userdata'); 0086 if ishandle(ax) 0087 ud = get(ax,'userdata'); 0088 0089 % Do a sanity check before deleting colorbar 0090 if isfield(ud,'DeleteProxy') & isequal(ud.DeleteProxy,cbo) 0091 try 0092 delete(ax) 0093 end 0094 if ishandle(ud.DeleteProxy) 0095 try 0096 delete(ud.DeleteProxy) 0097 end 0098 end 0099 0100 if isfield(ud,'PlotHandle') & ishandle(ud.PlotHandle) & ... 0101 isfield(ud,'originalPosition') & ~isempty(ud.originalPosition) 0102 units = get(ud.PlotHandle,'units'); 0103 set(ud.PlotHandle,'units','normalized'); 0104 set(ud.PlotHandle,'position',ud.originalPosition); 0105 set(ud.PlotHandle,'units',units); 0106 0107 updateLegend(ud.PlotHandle); 0108 0109 end 0110 end 0111 end 0112 elseif isempty(ax), 0113 %fcolorbar delete call 0114 ax = findobj(gcf,'type','axes','tag','Colorbar'); 0115 ud = get(ax,'userdata'); 0116 if isfield(ud,'PlotHandle') & ishandle(ud.PlotHandle) & ... 0117 isfield(ud,'originalPosition') & ~isempty(ud.originalPosition) 0118 units = get(ud.PlotHandle,'units'); 0119 set(ud.PlotHandle,'units','normalized'); 0120 set(ud.PlotHandle,'position',ud.originalPosition); 0121 set(ud.PlotHandle,'units',units); 0122 0123 updateLegend(ud.PlotHandle); 0124 end 0125 if isfield(ud,'DeleteProxy') & ishandle(ud.DeleteProxy) 0126 try 0127 delete(ud.DeleteProxy) 0128 end 0129 end 0130 0131 end 0132 return 0133 end 0134 0135 if nargin < 5 | isempty(hand), hand = [];end 0136 if nargin < 4 | isempty(W), W = 1/20;,end 0137 if nargin < 3 | isempty(label) | ~ischar(label), label='';end 0138 if nargin < 2 | isempty(L), L = 'r';,end 0139 0140 outside=0; alone=0; 0141 L = lower(L); 0142 switch L(1) 0143 case 'o', outside = 1; L(1) = []; 0144 case 'a', alone = 1; L(1) = []; 0145 end 0146 if isempty(L), L = 'r';end 0147 0148 0149 % What D is input... 0150 Dsiz = size(D); 0151 if prod(Dsiz)==max(Dsiz) & all(Dsiz>0) % D is a vector and therefore a contourspecification 0152 V = unique(D); 0153 elseif Dsiz(1)==2, 0154 V = clevels(D); % contour matrix given 0155 else 0156 error('First input must be a vector of contour level specifications or a contour-matrix!'); 0157 end 0158 0159 %moved tick and limits here to streamline the code. pab 20.08.2001 0160 %tick = maketick(V); 0161 tick = V; 0162 if (length(V)>1) 0163 mv = mean(diff(V(:)))/2; % average stepsize divided by 2 0164 limits = [min(V)-mv max(V)+mv]; 0165 else 0166 mv = max(abs(V),1); 0167 limits = V + [-mv mv]; 0168 end 0169 logScale = 0; 0170 if logScale, 0171 if min(V)<=eps 0172 error('log scale impossible') 0173 end 0174 if limits(1)<0 0175 limits(1) = eps; 0176 end 0177 end 0178 V = [limits(1) ;V(:); limits(2)]; 0179 0180 [x,VC] = meshgrid(1:2,V); % using the given contour-range 0181 0182 forceEqualSpacing = 1; 0183 if forceEqualSpacing, 0184 [x,y] = meshgrid(1:2, 1:length(V)); 0185 else 0186 y = VC; 0187 end 0188 0189 0190 % h0 = handle to the graph's axes ; 0191 % h1 = handle to the colorbar's axes 0192 h0 = gca; 0193 0194 0195 if alone, set(h0,'visible','off'); end 0196 0197 h1 = findColorbarAxes(h0); 0198 0199 if isempty(h1), % no colorbar present 0200 0201 %legend('RestoreSize',h0); %restore axes to pre-legend size 0202 0203 % Make colorbarAxes and update graph axes position 0204 units = get(h0,'units'); 0205 set(h0,'units','normalized') 0206 originalPosition = get(h0,'position'); 0207 [cAxesPosition,gAxesPosition] = getNewAxesPositions(h0,originalPosition,L,outside,W); 0208 set(h0,'Position',gAxesPosition); 0209 set(h0,'units',units) 0210 h1 = axes('Position', cAxesPosition); 0211 0212 %Create a userData object saving important info about the colorbar, graph axes and deleteProxy 0213 ud.originalPosition = originalPosition; 0214 ud.PlotHandle = h0; 0215 ud.Location = L(1); 0216 ud.outside = outside; 0217 0218 % create DeleteProxy object (an invisible text object in 0219 % the colorbar axes) so that the colorbar will be deleted 0220 % properly. 0221 deleteProxyProperties = {'parent',h0,'visible','off','tag','ColorbarDeleteProxy',... 0222 'handlevisibility','off','deletefcn','fcolorbar(''delete'')'};%'eval(''delete(get(gcbo,''''userdata''''))'','''')');% 0223 ud.DeleteProxy = text(deleteProxyProperties{:}); 0224 0225 set(ud.DeleteProxy,'userdata',h1) 0226 0227 else % colorbar axes already exist 0228 ud = get(h1,'UserData'); 0229 % Make sure contourf deletefcn doesn't trigger a fcolorbar('delete') 0230 % for colorbar update 0231 %set(get(h1,'children'),'deletefcn','') 0232 0233 sameColorbarLocation = strcmpi(ud.Location,L(1)); 0234 if ~sameColorbarLocation | ud.outside ~=outside, 0235 %update userData object 0236 ud.Location = L(1); 0237 ud.outside = outside; 0238 0239 % update colorbar Axes and graph axes positions 0240 0241 %set(h0,'Position',ud.originalPosition); 0242 [cAxesPosition,gAxesPosition] = getNewAxesPositions(h0,ud.originalPosition,L,outside,W); 0243 units = get(h0,'units'); 0244 set(h0,'units','normalized') 0245 set(h0,'Position',gAxesPosition); 0246 set(h0,'units',units) 0247 set(h1,'Position',cAxesPosition); 0248 0249 end 0250 axes(h1); 0251 end 0252 0253 0254 % Plot the actual colorbar 0255 if any(L(1)=='tb') 0256 %[x,y] = deal(y,x); % swap x and y 0257 contourf(y,x,VC,V); 0258 if (L(1)=='t'), set(h1,'XAxisLocation','top'); end 0259 set(h1,'XTick',tick,'Xlim',limits,'YTick',[]); 0260 set(h1,'XScale','log') 0261 if forceEqualSpacing 0262 set(h1,'YTick',[],'XTick',2:length(tick)+1,'Xlim',[1 length(V)]); 0263 set(h1,'Xticklabel',num2str(tick(:))); 0264 end 0265 xlabel(label) 0266 else 0267 contourf(x,y,VC,V); 0268 if (L(1)=='r'), set(h1,'YAxisLocation','right'); end 0269 0270 set(h1,'XTick',[],'YTick',tick,'Ylim',limits); 0271 if logScale, 0272 set(h1,'YScale','log') 0273 end 0274 if forceEqualSpacing 0275 set(h1,'XTick',[],'YTick',2:length(tick)+1,'Ylim',[1 length(V)]); 0276 set(h1,'Yticklabel',num2str(tick(:))); 0277 end 0278 ylabel(label) 0279 end 0280 0281 0282 ud.PlotHandle = h0; 0283 set(h1,'userdata',ud,'Tag','Colorbar'); 0284 set(gcf,'CurrentAxes',h0); % set current axis pointer back on the graph 0285 0286 %set(ud.DeleteProxy,'deletefcn','fcolorbar(''delete'')'); 0287 0288 updateLegend(h0); 0289 0290 % lock the coloraxis onto the colorbar in both plot and bar: 0291 set([h0 h1 hand(:)'],'clim',limits); 0292 0293 if nargout>0, h00 = h0; end % pab 20.08.2001 0294 0295 return 0296 0297 0298 %------------------------------------------------------------- 0299 function tick=maketick(lev) 0300 %MAKETICK Make the number of ticklabels be less than 10 0301 % 0302 % CALL: tick = maketick(levels) 0303 tick=lev; 0304 while length(tick)>10 0305 tick=tick(1:2:length(tick)); 0306 end 0307 0308 % no tick on the edges of colorbar 0309 %if max(tick)==max(lev) 0310 % tick=tick(1:length(tick)-1); 0311 %end 0312 return 0313 0314 0315 0316 0317 function cax = findColorbarAxes(dataAxes); 0318 %FINDCOLORBARAXES Return the handle to the colorbar of dataAxes if it exists. 0319 % 0320 0321 cfig = get(dataAxes,'parent'); % get figure handle 0322 0323 % Search for existing colorbar if any 0324 ch = findobj(cfig,'type','axes','tag','Colorbar'); 0325 cax = []; 0326 for i=1:length(ch), 0327 ud = get(ch(i),'UserData'); 0328 d = ud.PlotHandle; 0329 if prod(size(d))==1 & isequal(d,dataAxes), 0330 cax = ch(i); 0331 break; 0332 end 0333 end 0334 return 0335 0336 0337 function [cAxesPosition,gAxesPosition] = getNewAxesPositions(h0,a,L,outside,W); 0338 %GETNEWAXESPOSITIONS Return the new normalized axes positions for the colorbar and the graph, respectively. 0339 % 0340 % CALL: [cAxesPosition,gAxesPosition] = getNewAxesPositions(gax,L,outside,W); 0341 % 0342 % cAxesPosition 0343 % ,gAxesPosition = new normalized axes positions for the colorbar and the graph, respectively. 0344 % gax = handle to the graph axes. 0345 % gAxesOrigPosition = original axes position for the graph (normalized) 0346 % L = String giving location of colorbar: 0347 % 't'op 0348 % 'b'ottom 0349 % 'r'ight 0350 % 'l'eft 0351 % outside = 0 when rescaling of graph is wanted 0352 % 1 when rescaling of graph is undesireable 0353 % W = Relative width (ratio of graph width/height) 0354 % 0355 0356 0357 %a = get(h0,'Position'); 0358 0359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0360 switch L(1) 0361 case 't' 0362 W=W*a(4); 0363 % placement of the colorbar: 0364 if findstr(get(h0,'XAxisLocation'),'top'),os=2*W; else os=W; end 0365 if outside 0366 cAxesPosition = [a(1) a(2)+a(4)+os a(3) W]; 0367 gAxesPosition = a; 0368 else 0369 cAxesPosition = [a(1) a(2)+a(4)-W a(3) W]; 0370 gAxesPosition = [a(1) a(2) a(3) a(4)-W-os]; 0371 end 0372 0373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0374 case 'b' 0375 W=W*a(4); 0376 % placement of the colorbar: 0377 if findstr(get(h0,'XAxisLocation'),'bottom'),os=W*2; else os=W; end 0378 if outside 0379 cAxesPosition = [a(1) a(2)-os-W a(3) W]; 0380 gAxesPosition = a; 0381 else 0382 cAxesPosition = [a(1) a(2) a(3) W]; 0383 gAxesPosition = [a(1) a(2)+os+W a(3) a(4)-os-W]; 0384 end 0385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0386 case 'r' %default 0387 W=W*a(3); 0388 % placement of the colorbar: 0389 if findstr(get(h0,'YAxisLocation'),'right'),os=W*2; else os=W; end 0390 if outside 0391 cAxesPosition = [a(1)+a(3)+os a(2) W a(4)]; 0392 gAxesPosition = a; 0393 else 0394 cAxesPosition = [a(1)+a(3)-W a(2) W a(4)]; 0395 gAxesPosition = [a(1) a(2) a(3)-os-W a(4)]; 0396 end 0397 0398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0399 case 'l' 0400 W=W*a(3); 0401 % placement of the colorbar: 0402 if findstr(get(h0,'YAxisLocation'),'left'),os=W*2; else os=W; end 0403 if outside 0404 cAxesPosition = [a(1) a(2)-os-W W a(4)]; 0405 gAxesPosition = a; 0406 else 0407 cAxesPosition = [a(1) a(2) W a(4)]; 0408 gAxesPosition = [a(1)+os+W a(2) a(3)-os-W a(4)]; 0409 end 0410 end; 0411 return 0412 0413 function h = gcda(hfig, haxes) 0414 %GCDA Get current data axes 0415 0416 h = datachildren(hfig); 0417 if isempty(h) | any(h == haxes) 0418 h = haxes; 0419 else 0420 h = h(1); 0421 end 0422 return 0423 0424 function updateLegend(axesHandle) 0425 %UPDATELEGEND Updates the legend if any, in the axes specified by axesHandle 0426 0427 legH = legend(axesHandle); 0428 if ~isempty(legH) & ishandle(legH) 0429 %legend('RecordSize',axesHandle); 0430 legend(legH) % Update legend 0431 end 0432 return
Comments or corrections to the WAFO group