www.gusucode.com > Matlab动力系统和时间序列分析工具箱 > Matlab动力系统和时间序列分析工具箱/lab432/toolbox/hist_2D.m

    function hist_2D(x,y,K); 
% HIST_2D   Two-dimensional histogram.
%    HIST_2D(X,Y,K) bins the two-dimensional density of scalar time series 
%      X and Y into a KxK equally spaced matrix.
%    By default K is square root of time series length

% Copyright (c) 2002-2003
% Andre Sitz, Norbert Marwan, Potsdam University, Germany
% http://www.agnld.uni-potsdam.de
%
% Last Revision: 2002-03-20
% Version: 2.3
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or any later version.
%
% last modified 30.11.04 by Max Logunov

   
if nargin<3
    nbin=ceil(length(x)^0.5);
elseif nargin==3
    nbin=K;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1=(x-min(x))/max(x-min(x))-eps;
y1=(y-min(y))/max(y-min(y))-eps;

temp=fix(x1*nbin)+(fix(y1*nbin))*nbin;

figure('Name','2D Histogram',...
            'NumberTitle','off','color',[1 1 1]);

p=histc(temp,0:(nbin^2-1))';
p2=reshape(p,nbin,nbin)/length(x);

minx = min(min(x)); maxx = max(max(x));
if minx == maxx, minx = minx - floor(nbin/2) - 0.5; maxx = maxx + ceil(nbin/2) - 0.5; end
miny = min(min(y)); maxy = max(max(y));
if miny == maxy, miny = miny - floor(nbin/2) - 0.5; maxy = maxy + ceil(nbin/2) - 0.5; end

bins(1,:)=minx:(maxx-minx)/(nbin-1):maxx+eps;
bins(2,:)=miny:(maxy-miny)/(nbin-1):maxy+eps;

bar3(bins(2,:),p2')
dhy=bins(2,2)-bins(2,1);
set(gca,'xlim',[0 nbin+1-.1],'ylim',[bins(2,1)-dhy bins(2,end)+dhy])
hx=get(gca,'xtick'); dhx=diff(hx); 
set(gca,'xtick',[1:dhx(1):nbin+1])
if dhx>1
tx=num2str(fix(100*[bins(1,1):(bins(1,end)-bins(1,1))/(length(hx)-1):bins(1,end)]')/100);
else
tx=num2str(fix(100*[bins(1,:)]')/100);
end
set(gca,'xticklabel',tx,'fontsize',8);