0001 function [h,a] = PlotDistribution2(var1,var2,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
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
0042 nBins = 100;
0043 smooth = 5;
0044 xBins = [];
0045 yBins = [];
0046
0047
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
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
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
0103 main = gca;
0104 position = get(main,'position');
0105 x = position(1);
0106 h1 = position(3);
0107 hg = h1/10;
0108 h2 = h1 - hg;
0109 y = position(2);
0110 v1 = position(4);
0111 vg = v1/10;
0112 v2 = v1 - vg;
0113
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
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
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
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
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
0194 h = [h;p bv bh];
0195 end
0196
0197
0198 set(main,'xaxislocation','bottom','yaxislocation','left');
0199 set(right,'xaxislocation','bottom','yaxislocation','right');
0200 set(top,'xaxislocation','top','yaxislocation','left');
0201 axes(main);