Home > atmlab > graphs > colorbarf.m

colorbarf

PURPOSE ^

COLORBARF Display color bar for a filled contour plot.

SYNOPSIS ^

function hcb=colorbarf(cout,H,loc)

DESCRIPTION ^

 COLORBARF Display color bar for a filled contour plot.
 =========================================================================
 colorbarf  Version 1.6 4-Aug-2004

 Usage: 
   colorbarf(cout,H,[loc])

   Example: [cout,H,cf]=contourf(peaks);
            colorbarf(cout,H);

 Description:
   This Matlab function uses the output arguments from the contourf function
   to produce a colorbar that sets the tick marks equal to the contour levels
   in the figure. The area of the colorbar between the tick marks is filled
   with the same color used by the contourf function. The location may be
   specified as either 'vert' or 'horiz'.

 Input:
   cout - output argument of contourf containing an matrix describing the
          contours
   H    - graphics handles for the contours in the figure
   loc  - location of the colorbar specified 'vert' for vertical (Default) and
          'horiz' for horizontal. If not specified, default is set.

 Output:
   hcb  - graphics handle for the colorbar axis

 Author:
   Blair Greenan
   Bedford Institute of Oceanography
   December 22, 1998
   Matlab 5.2.1
   greenanb@mar.dfo-mpo.gc.ca
 =========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

colorbarf.m

SOURCE CODE ^

0001 function hcb=colorbarf(cout,H,loc)
0002 % COLORBARF Display color bar for a filled contour plot.
0003 % =========================================================================
0004 % colorbarf  Version 1.6 4-Aug-2004
0005 %
0006 % Usage:
0007 %   colorbarf(cout,H,[loc])
0008 %
0009 %   Example: [cout,H,cf]=contourf(peaks);
0010 %            colorbarf(cout,H);
0011 %
0012 % Description:
0013 %   This Matlab function uses the output arguments from the contourf function
0014 %   to produce a colorbar that sets the tick marks equal to the contour levels
0015 %   in the figure. The area of the colorbar between the tick marks is filled
0016 %   with the same color used by the contourf function. The location may be
0017 %   specified as either 'vert' or 'horiz'.
0018 %
0019 % Input:
0020 %   cout - output argument of contourf containing an matrix describing the
0021 %          contours
0022 %   H    - graphics handles for the contours in the figure
0023 %   loc  - location of the colorbar specified 'vert' for vertical (Default) and
0024 %          'horiz' for horizontal. If not specified, default is set.
0025 %
0026 % Output:
0027 %   hcb  - graphics handle for the colorbar axis
0028 %
0029 % Author:
0030 %   Blair Greenan
0031 %   Bedford Institute of Oceanography
0032 %   December 22, 1998
0033 %   Matlab 5.2.1
0034 %   greenanb@mar.dfo-mpo.gc.ca
0035 % =========================================================================
0036 %
0037 
0038 %   This function has been derived from the Matlab colorbar function
0039 %   Author: Clay M. Thompson 10-9-92
0040 %   Copyright (c) 1984-98 by The MathWorks, Inc.
0041 %   $Revision: 1.2 $  $Date: 2004/08/06 09:28:59 $
0042 %
0043 
0044 % Modifications:
0045 %   Version 1.1 - function now works with the output of the Matlab
0046 %   contourf function. Colorbarf can now handle situations in which
0047 %   the lowest contour level is not filled if an array, which does not
0048 %   include a value at or below the minimum of the data, is passed to
0049 %   the contourf function. Version 1.0 also did not plot the colorbar
0050 %   appropriately if a colorbar already existed on the figure...this
0051 %   is now fixed.
0052 %   Version 1.2 - ch.UserData changed to ch(i).UserData. This enables
0053 %   colrbarf to be used on multiple plot figures - Thanks to Peter Brickley, U. of Maine
0054 %   Version 1.3 - changes made to handle NaNs in data - 11-Mar-1999 - Thanks to J. van der Molen
0055 %   Version 1.4 - removed for loop at line 128 to speed up execution time
0056 %   Version 1.5 - modified line 129 "if (N1 > 1)" to accomodate the MATLAB 6.0 include some NULL
0057 %                 contours that have one point that is simply NaN.
0058 %   Version 1.6 - modified by Aslak Grinsted at line 145 to ensure MATLAB 7
0059 %                   compatability
0060 
0061 %   If called with COLORBAR(H) or for an existing colorbar, don't change
0062 %   the NextPlot property.
0063 changeNextPlot = 1;
0064 
0065 % colorbar must have output parameters from the contourf function
0066 if (nargin < 2)
0067    error('colorbarf requires a minimum of two input parameters');
0068 end
0069 
0070 % default location for colorbar
0071 if nargin<3, loc = 'vert'; end
0072 
0073 % if an axes handle is passed for the location
0074 ax = [];
0075 if nargin==3,
0076     if ishandle(loc)
0077         ax = loc;
0078         if ~strcmp(get(ax,'type'),'axes'),
0079             error('Requires axes handle.');
0080         end
0081         units = get(ax,'units'); set(ax,'units','pixels');
0082         rect = get(ax,'position'); set(ax,'units',units)
0083         if rect(3) > rect(4), loc = 'horiz'; else loc = 'vert'; end
0084         changeNextPlot = 0;
0085     end
0086 end
0087 
0088 h = gca;
0089 
0090 if (nargin == 2)
0091    % Search for existing colorbar so that we can plot over it if we find it
0092    ch = get(findobj(gcf,'type','axes','tag','Colorbar')); ax = [];
0093    for i=1:length(ch),
0094       ud = ch(i).UserData; % ch.UserData changed to ch(i).UserData - suggested by P. Brickley
0095       d = ud.PlotHandle;
0096       if prod(size(d))==1 & isequal(d,h), 
0097          ax = findobj(gcf,'type','axes','tag','Colorbar'); 
0098          pos = ch.Position;
0099          if pos(3)<pos(4), loc = 'vert'; else loc = 'horiz'; end
0100          changeNextPlot = 0;
0101          break; 
0102       end
0103    end
0104 elseif ((nargin == 3) & (~ishandle(loc)))
0105    % Search for existing colorbar so that we can plot over it if we find it
0106    ch = get(findobj(gcf,'type','axes','tag','Colorbar')); ax = [];
0107    for i=1:length(ch),
0108       ud = ch(i).UserData; % ch.UserData changed to ch(i).UserData - suggested by P. Brickley
0109       d = ud.PlotHandle;
0110       if prod(size(d))==1 & isequal(d,h), 
0111          ax = findobj(gcf,'type','axes','tag','Colorbar'); 
0112          pos = ch.Position;
0113          if pos(3)<pos(4), loc = 'vert'; else loc = 'horiz'; end
0114          changeNextPlot = 0;
0115          break; 
0116       end
0117    end
0118 end
0119 
0120 origNextPlot = get(gcf,'NextPlot');
0121 if strcmp(origNextPlot,'replacechildren') | strcmp(origNextPlot,'replace'),
0122     set(gcf,'NextPlot','add')
0123 end
0124 
0125 %%%%%%%%%%%%%% Filled Colorbar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0126 % Blair Greenan Dec 16, 1998
0127 
0128 % strip out the information about the contours from the output of the contourf
0129 % function
0130 i = 1;
0131 while ~isempty(cout)
0132    C1(i) = cout(1,1); % contour level
0133    N1 = cout(2,1); % number of points in contour
0134    % Version 1.4 removed for loop to speed things up
0135    cout(:,1:N1+1) = []; % shrink the matrix
0136    if (N1 > 1) % has to be more than 1 point in contour to be valid ***VERSION 1.5 to accomodate MATLAB6****
0137       i = i + 1;
0138    end
0139 end
0140 
0141 
0142 
0143 C2 = unique(C1); % find the unique contour levels and sort
0144 numLevels = length(C2); 
0145 
0146 if strcmp(get(H,'type'),'hggroup') % version 1.6 added by Aslak Grinsted to ensure MATLAB v7 compatibility
0147     H=get(H,'children');
0148 end
0149 
0150 for j = 1:length(H)
0151    colors(j) = get(H(j),'CData'); % get the color used to fill the patches
0152 end
0153 colors = unique(colors);  % create a list of unique colors used in fills
0154 minc = min(colors);
0155 maxc = max(colors);
0156 colors(isnan(colors))=[];
0157 if ((length(colors)-numLevels)>1)
0158    % chop off extra colors that don't have a corresponding contour level
0159    colors(1:((length(colors)-numLevels)-1))=[]; 
0160 end
0161 % special case where no mimima is enclosed by a contour
0162 if (length(colors) == numLevels)
0163    colors(length(colors)+1)=NaN;
0164 end
0165 
0166 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0167 
0168 if loc(1)=='v', % Append vertical scale to right of current plot
0169     
0170     if isempty(ax),
0171         units = get(h,'units'); set(h,'units','normalized')
0172         pos = get(h,'Position'); 
0173         [az,el] = view;
0174         stripe = 0.075; edge = 0.02; 
0175         if all([az,el]==[0 90]), space = 0.05; else space = .1; end
0176         set(h,'Position',[pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)])
0177         rect = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
0178         ud.origPos = pos;
0179         
0180         % Create axes for stripe and
0181         % create DeleteProxy object (an invisible text object in
0182         % the target axes) so that the colorbar will be deleted
0183         % properly.
0184         ud.DeleteProxy = text('parent',h,'visible','off',...
0185                               'tag','ColorbarDeleteProxy',...
0186                               'handlevisibility','off',...
0187              'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
0188         ax = axes('Position', rect,'Tag','TMW_COLORBAR');
0189         set(ud.DeleteProxy,'userdata',ax)
0190         set(h,'units',units)
0191     else % if colobar axes already exist
0192         axes(ax);
0193         ud = get(ax,'userdata');
0194     end
0195     
0196     % Create color stripe by drawing the appropriate number of filled rectangles
0197     if any(isnan(colors)) % if the bottom level is not filled
0198        for j = 1:length(C2)+1
0199           k = j - 1; % don't fill bottom rectangle on colorbar
0200           x(1) = 0;
0201           x(2) = 1;
0202           x(3) = 1;
0203           x(4) = 0;
0204           y(1) = (j-1)*(1/(length(C2)+1));
0205           y(2) = (j-1)*(1/(length(C2)+1));
0206           y(3) = j*(1/(length(C2)+1));
0207           y(4) = j*(1/(length(C2)+1));
0208           if (k == 0)
0209              hfill = fill(x,y,colors(length(colors))); % fill with NaN
0210           else
0211              hfill = fill(x,y,colors(k));
0212           end
0213           hold on
0214        end
0215     else %we have filled the bottom contour level
0216        for j = 1:length(C2)+1
0217           x(1) = 0;
0218           x(2) = 1;
0219           x(3) = 1;
0220           x(4) = 0;
0221           y(1) = (j-1)*(1/(length(C2)+1));
0222           y(2) = (j-1)*(1/(length(C2)+1));
0223           y(3) = j*(1/(length(C2)+1));
0224           y(4) = j*(1/(length(C2)+1));
0225           hfill = fill(x,y,colors(j));
0226           hold on
0227        end
0228     end
0229     set(ax,'YAxisLocation','right')
0230     set(ax,'xtick',[])
0231     ylimits = get(gca,'ylim');
0232     myYticks = ylimits(1):(ylimits(2)-ylimits(1))/(length(C2)+1):ylimits(2)...
0233        -((ylimits(2)-ylimits(1))/(length(C2)+1));
0234     set(gca,'ytick',myYticks);
0235     
0236     myStr{1} = ' ';
0237     for kk = 1:length(C2)
0238        myStr{kk+1} = num2str(C2(kk));
0239     end
0240     myStr{(length(C2)+2)} = ' ';
0241     set(gca,'YTickLabel',myStr)
0242     set(gca,'CLim',[minc maxc])
0243 
0244     % set up axes deletefcn
0245     set(ax,'tag','Colorbar','deletefcn','colorbar(''delete'')')
0246     
0247 elseif loc(1)=='h', % Append horizontal scale to top of current plot
0248     
0249     if isempty(ax),
0250         units = get(h,'units'); set(h,'units','normalized')
0251         pos = get(h,'Position');
0252         stripe = 0.075; space = 0.1;
0253         set(h,'Position',...
0254             [pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)])
0255         rect = [pos(1) pos(2) pos(3) stripe*pos(4)];
0256         ud.origPos = pos;
0257 
0258         % Create axes for stripe and
0259         % create DeleteProxy object (an invisible text object in
0260         % the target axes) so that the colorbar will be deleted
0261         % properly.
0262         ud.DeleteProxy = text('parent',h,'visible','off',...
0263                               'tag','ColorbarDeleteProxy',...
0264                               'handlevisibility','off',...
0265              'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
0266         ax = axes('Position', rect,'Tag','TMW_COLORBAR');
0267         set(ud.DeleteProxy,'userdata',ax)
0268         set(h,'units',units)
0269     else % if colobar axes already exist
0270         axes(ax);
0271         ud = get(ax,'userdata');
0272     end
0273     
0274     % Create color stripe by drawing the appropriate number of filled rectangles
0275     if any(isnan(colors)) % if the bottom level is not filled
0276        for j = 1:length(C2)+1
0277           k = j - 1; % don't fill bottom rectangle on colorbar
0278           y(1) = 0;
0279           y(2) = 1;
0280           y(3) = 1;
0281           y(4) = 0;
0282           x(1) = (j-1)*(1/(length(C2)+1));
0283           x(2) = (j-1)*(1/(length(C2)+1));
0284           x(3) = j*(1/(length(C2)+1));
0285           x(4) = j*(1/(length(C2)+1));
0286           if (k == 0)
0287              hfill = fill(x,y,colors(length(colors))); % fill with NaN
0288           else
0289              hfill = fill(x,y,colors(k));
0290           end
0291           hold on
0292        end
0293     else %we have filled the bottom contour level
0294        for j = 1:length(C2)+1
0295           y(1) = 0;
0296           y(2) = 1;
0297           y(3) = 1;
0298           y(4) = 0;
0299           x(1) = (j-1)*(1/(length(C2)+1));
0300           x(2) = (j-1)*(1/(length(C2)+1));
0301           x(3) = j*(1/(length(C2)+1));
0302           x(4) = j*(1/(length(C2)+1));
0303           hfill = fill(x,y,colors(j));
0304           hold on
0305        end
0306     end
0307     set(ax,'ytick',[])
0308     xlimits = get(gca,'xlim');
0309     myXticks = xlimits(1):(xlimits(2)-xlimits(1))/(length(C2)+1):xlimits(2)...
0310        -((xlimits(2)-xlimits(1))/(length(C2)+1));
0311     set(gca,'xtick',myXticks);
0312     
0313     myStr{1} = ' ';
0314     for kk = 1:length(C2)
0315        myStr{kk+1} = num2str(C2(kk));
0316     end
0317     myStr{(length(C2)+2)} = ' ';
0318     set(gca,'XTickLabel',myStr)
0319     set(gca,'CLim',[minc maxc])
0320 
0321     % set up axes deletefcn
0322     set(ax,'tag','Colorbar','deletefcn','colorbar(''delete'')')
0323     
0324 else
0325   error('COLORBAR expects a handle, ''vert'', or ''horiz'' as input.')
0326 end
0327 
0328 if ~isfield(ud,'DeleteProxy'), ud.DeleteProxy = []; end
0329 if ~isfield(ud,'origPos'), ud.origPos = []; end
0330 ud.PlotHandle = h;
0331 set(ax,'userdata',ud)
0332 set(gcf,'CurrentAxes',h)
0333 set(gcf,'NextPlot',origNextPlot)
0334 
0335 if nargout>0, hcb = ax; end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005