Home > FMAToolbox > Plot > PlotDistribution2.m

PlotDistribution2

PURPOSE ^

PlotDistribution2 - Plot bivariate data along with respective distributions.

SYNOPSIS ^

function [h,a] = PlotDistribution2(var1,var2,varargin)

DESCRIPTION ^

PlotDistribution2 - Plot bivariate data along with respective distributions.

  USAGE

    [h,a] = PlotDistribution2(var1,var2,<options>)

    Using cell arrays will overlay variable pairs.

    var1           variable 1
    var2           variable 2
    <options>      optional list of property-value pairs (see table below)

    =========================================================================
     Properties    Values
    -------------------------------------------------------------------------
     'nBins'       number of bins for distributions (default = 100)
     'xBins'       [m M n], lower and upper bounds, and number of bins,
                   respectively, for x axis (default [min max 100])
     'yBins'       [m M n], lower and upper bounds, and number of bins,
                   respectively, for y axis (default [min max 100])
     'smooth'      standard deviation of Gaussian kernel (default = 5)
    =========================================================================

  OUTPUT

    h              handles to the plots and histograms (one line per set of
                   variables)
    a              axes for the plots and histograms: [main top right]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [h,a] = PlotDistribution2(var1,var2,varargin)
0002 
0003 %PlotDistribution2 - Plot bivariate data along with respective distributions.
0004 %
0005 %  USAGE
0006 %
0007 %    [h,a] = PlotDistribution2(var1,var2,<options>)
0008 %
0009 %    Using cell arrays will overlay variable pairs.
0010 %
0011 %    var1           variable 1
0012 %    var2           variable 2
0013 %    <options>      optional list of property-value pairs (see table below)
0014 %
0015 %    =========================================================================
0016 %     Properties    Values
0017 %    -------------------------------------------------------------------------
0018 %     'nBins'       number of bins for distributions (default = 100)
0019 %     'xBins'       [m M n], lower and upper bounds, and number of bins,
0020 %                   respectively, for x axis (default [min max 100])
0021 %     'yBins'       [m M n], lower and upper bounds, and number of bins,
0022 %                   respectively, for y axis (default [min max 100])
0023 %     'smooth'      standard deviation of Gaussian kernel (default = 5)
0024 %    =========================================================================
0025 %
0026 %  OUTPUT
0027 %
0028 %    h              handles to the plots and histograms (one line per set of
0029 %                   variables)
0030 %    a              axes for the plots and histograms: [main top right]
0031 
0032 
0033 % Copyright (C) 2004-2013 by Michaƫl Zugaro
0034 %
0035 % This program is free software; you can redistribute it and/or modify
0036 % it under the terms of the GNU General Public License as published by
0037 % the Free Software Foundation; either version 3 of the License, or
0038 % (at your option) any later version.
0039 
0040 colors = [1 0.2 0;0 0.4 1;0 1 0.2;1 0.6 0;0.8 0 1;0.8 1 0];
0041 % Default values
0042 nBins = 100;
0043 smooth = 5;
0044 xBins = [];
0045 yBins = [];
0046 
0047 % Check number of parameters
0048 if nargin < 2 | mod(length(varargin),2) ~= 0,
0049   error('Incorrect number of parameters (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0050 end
0051 
0052 if isa(var1,'cell'),
0053     if ~isa(var2,'cell'),
0054         error('One parameter is a cell array and the other is not (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0055     end
0056     if length(var1) ~= length(var2),
0057         error('Variable lists have different lengths (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0058     end
0059 else
0060     var1 = {var1};
0061     var2 = {var2};
0062 end
0063 
0064 % Check parameter sizes
0065 for i = 1:length(var1),
0066     if ~isdvector(var1{i}) | ~isdvector(var2{i}) | any(size(var1{i})~=size(var2{i})),
0067         error('Individual variables must be vectors of identical length (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0068     end
0069 end
0070 
0071 % Parse parameter list
0072 for i = 1:2:length(varargin),
0073     if ~ischar(varargin{i}),
0074         error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).']);
0075     end
0076     switch(lower(varargin{i})),
0077         case 'nbins',
0078         nBins = varargin{i+1};
0079         if ~isiscalar(nBins,'>0'),
0080             error('Incorrect value for property ''nBins'' (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0081         end
0082     case 'xBins',
0083         xBins = varargin{i+1};
0084         if ~isdvector(xBins,'#3') | xBins(1) > xBins(2) | ~isiscalar(xBins(3),'>0'),
0085             error('Incorrect value for property ''xBins'' (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0086         end
0087     case 'yBins',
0088         yBins = varargin{i+1};
0089         if ~isdvector(yBins,'#3') | yBins(1) > yBins(2) | ~isiscalar(yBins(3),'>0'),
0090             error('Incorrect value for property ''yBins'' (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0091         end
0092     case 'smooth',
0093         smooth = varargin{i+1};
0094         if ~isdscalar(smooth,'>=0'),
0095             error('Incorrect value for property ''smooth'' (type ''help <a href="matlab:help PlotDistribution2">PlotDistribution2</a>'' for details).');
0096         end
0097     otherwise,
0098         error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help PlottDistribution2">PlotDistribution2</a>'' for details).']);
0099   end
0100 end
0101 
0102 % Resize current axes to make room for distribution plots
0103 main = gca;
0104 position = get(main,'position');
0105 x = position(1);
0106 h1 = position(3); % current width
0107 hg = h1/10; % horizontal gap
0108 h2 = h1 - hg; % new total width
0109 y = position(2);
0110 v1 = position(4); % current height
0111 vg = v1/10; % vertical gap
0112 v2 = v1 - vg; % new total height
0113 %  set(main,'position',[x+h2/3+hg y+v2/3+vg h2*2/3 v2*2/3]);
0114 set(main,'position',[x y h2*2/3 v2*2/3]);
0115 right = axes('position',[x+h2*2/3+hg y h2/3 v2*2/3]);
0116 top = axes('position',[x y+v2*2/3+vg h2*2/3 v2/3]);
0117 
0118 % Axis limits
0119 if isempty(xBins),
0120     m1 = min(var1{1});
0121     M1 = max(var1{1});
0122     for i = 2:length(var1),
0123         m1 = min([m1;var1{i}]);
0124         M1 = max([M1;var1{i}]);
0125     end
0126     nBinsX = nBins;
0127 else
0128     m1 = xBins(1);
0129     M1 = xBins(2);
0130     nBinsX = xBins(3);
0131 end
0132 if isempty(yBins),
0133     m2 = min(var2{1});
0134     M2 = max(var2{1});
0135     for i = 2:length(var1),
0136         m2 = min([m2;var2{i}]);
0137         M2 = max([M2;var2{i}]);
0138     end
0139     nBinsY = nBins;
0140 else
0141     m2 = yBins(1);
0142     M2 = yBins(2);
0143     nBinsY = yBins(3);
0144 end
0145 d1 = M1-m1;
0146 m1 = m1-d1/10;
0147 M1 = M1+d1/10;
0148 d2 = M2-m2;
0149 m2 = m2-d2/10;
0150 M2 = M2+d2/10;
0151 
0152 h = [];
0153 a = [main top right];
0154 
0155 for i = 1:length(var1),
0156     % Plot bivariate data
0157     axes(main);hold on;
0158     p = plot(var1{i},var2{i},'o');
0159     k = mod(i,size(colors,1)+1);
0160     set(p,'MarkerFaceColor',colors(k,:),'MarkerEdgeColor',colors(i,:));
0161     if i > 1,
0162         m1 = min([m1 pm1]);
0163         M1 = max([M1 pM1]);
0164         m2 = min([m2 pm2]);
0165         M2 = max([M2 pM2]);
0166     end
0167     pm1 = m1;
0168     pM1 = M1;
0169     pm2 = m2;
0170     pM2 = M2;
0171     xlim([m1 M1]);ylim([m2 M2]);
0172 
0173     % Distribution of var1
0174     axes(top);hold on;
0175     [dist1,x1] = hist(var1{i},m1:(M1-m1)/nBinsX:M1);
0176     dist1 = Smooth(dist1/sum(dist1),smooth);
0177     bv = bar(x1,dist1,1);
0178     k = mod(i,size(colors,1)+1);
0179     set(bv,'FaceColor',colors(k,:),'EdgeColor',colors(i,:));
0180     set(top,'xtick',[]);
0181     xlim([m1 M1]);
0182 
0183     % Distribution of var2
0184     axes(right);hold on;
0185     [dist2,x2] = hist(var2{i},m2:(M2-m2)/nBinsY:M2);
0186     dist2 = Smooth(dist2/sum(dist2),smooth);
0187     bh = barh(x2,dist2,1);
0188     k = mod(i,size(colors,1)+1);
0189     set(bh,'FaceColor',colors(k,:),'EdgeColor',colors(i,:));
0190     set(right,'ytick',[]);
0191     ylim([m2 M2]);
0192     
0193     % store
0194     h = [h;p bv bh];
0195 end
0196 
0197 % Adjust axis locations
0198 set(main,'xaxislocation','bottom','yaxislocation','left');
0199 set(right,'xaxislocation','bottom','yaxislocation','right');
0200 set(top,'xaxislocation','top','yaxislocation','left');
0201 axes(main);

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005