Code covered by the BSD License  

Highlights from
hfill and vfill

image thumbnail

hfill and vfill

by

 

15 Aug 2013 (Updated )

Simply create horizontal or vertical shaded sections of a plot.

vfill(xbounds,ColorSpec,varargin)
function [h] = vfill(xbounds,ColorSpec,varargin)
% h = vfill(xbounds,ColorSpec,varargin) creates fill objects bounded by 
% the values xbounds.  ColorSpec defines the color of the fill objects. 
% Optional varargin can be used to set edgecolor, transparency, etc.
% varargin must be in the form of name-value pairs (e.g., 'facealpha',.5 )
% Optional output h is the handle(s) of each patch object.  
% 
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
% INPUTS: 
% xbounds   Bounding values of area(s) to be filled. Can be in any of the
%           following forms: 
%           xbounds = [xstart1,xend1,xstart2,xend2,...,xstartn,xendn]
% 
%           xbounds = [xstart1 xend1;
%                      xstart2 xend2;
%                      ...
%                      xstartn xendn];
%
%           xbounds = [xstart1 xstart2 ... xstartn;
%                      xend1   xend2   ... xendn];
%
%           xbounds = scalarvalue produces a vertical line at scalarvalue.
% 
% ColorSpec Defines the color of the patch(es) to be created. Can be RGB 
%           value (e.g., [0 1 0]), short name (e.g., 'g'), or long name 
%           (e.g. 'green'). Also supports the 'gray' for 50% gray.
% 
% varargin  List of name-value pairs such as 'facealpha',.5,'edgecolor','r'.
% 
% varargin can also include the tag 'bottom' or 'back' to send the patch to the back.
% 
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
% EXAMPLE: 
% 
% plot(0:100,(0:100).^2,'linewidth',2)
% vfill([80 90],'gray')
% vfill([5 15; 30 50],'g','facealpha',.6,'edgecolor','r','linestyle','--')
% vfill([65 75],'r','bottom')
% 
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
% Chad A. Greene, August 15, 2013. 
% * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
%
% See also hfill. 

plotbehind=false; 

% Input check: 
if length(xbounds)==1
    xbounds(2)=xbounds(1);
end
    
nxb = numel(xbounds); % number of elements in xbounds. 
if mod(nxb,2)
    fprintf('Number of bounding values must be even.');
    fprintf('In other words, enter start and end values for fill bounds.');
    return
end

if strcmpi(ColorSpec,'gray')||strcmpi(ColorSpec,'grey')
    ColorSpec = .5*[1 1 1];
end

for k = 1:length(varargin)
    if strcmpi(varargin{k},'bottom')||strcmpi(varargin{k},'back')
        plotbehind = true;
        varargink=k; 
    end
end

if plotbehind
    varargin(varargink,:)=[];
end
        
% Sort xbounds and start assuming they are in the order
% [xstart1,xend1,xstart2,xend2,...,xstartn,xendn]
sxb=sort(reshape(xbounds,nxb,1)); % sorted x bounds

% Current axis properties: 
ylim = get(gca,'ylim');
initialHoldState = ishold(gca);
hold on

% Plot the patches: 
n=0; % a counter
h = NaN(nxb/2,1); % preallocate patch handles for speed. 
for k = 1:2:nxb-1; 
    n=n+1;
    h(n) = fill([sxb(k) sxb(k+1) sxb(k+1) sxb(k)],...
        [ylim(1) ylim(1) ylim(2) ylim(2)],ColorSpec);
end

% Set optional input properties as name-value pairs: 
for kv = 1:2:length(varargin)
    set(h(:),varargin{kv},varargin{kv+1});
end
    
% Return hold state: 
if initialHoldState==0
    hold off;
end

if plotbehind
    uistack(h,'bottom');
end

if nargout==0
    clear h; 
end

end

Contact us