hist2w.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function H = hist2w(X,W,y,z,C)
  2. %% Weighted 2D histogram
  3. % function H = hist2w(X,W,y,z,C)
  4. %
  5. % Inputs: X - Nx2 data
  6. % W - Nx1 weights
  7. % y - vector of M(1) bin centers along 1st dim of X
  8. % z - vector of M(2) bin centers along 2nd dim of X
  9. % C - M(1)xM(2)x3 RGB coloring or Kx3 colormap (default: gray)
  10. % Output: H - M(1)xM(2) bar heights
  11. %
  12. % Grid vectors (y, z) must have linear spacing
  13. % If no output arguments specified, plots 2D colored histogram.
  14. %
  15. % Johannes Traa - March 2014
  16. %% check inputs
  17. if nargin < 5 || isempty(C); C = linspace(0.5,1,200)'*ones(1,3); end
  18. %% prepare
  19. M = [length(y) length(z)]; % # bins
  20. w = [y(2)-y(1) z(2)-z(1)]; % bar widths
  21. %% calculate weighted histogram
  22. H = zeros(M(1),M(2),1); % bar heights
  23. % boundary indicators
  24. yu = (X(:,1) <= y(2) - w(1)/2); % under y range
  25. ya = (X(:,1) >= y(M(1)-1) + w(1)/2); % above y range
  26. zu = (X(:,2) <= z(2) - w(2)/2); % under z range
  27. za = (X(:,2) >= z(M(2)-1) + w(2)/2); % above z range
  28. % corners
  29. H(1,1) = sum(W(yu & zu)); % corner (-,-)
  30. H(M(1),1) = sum(W(ya & zu)); % corner (+,-)
  31. H(1,M(2)) = sum(W(yu & za)); % corner (-,+)
  32. H(M(1),M(2)) = sum(W(ya & za)); % corner (+,+)
  33. % sides
  34. for i=2:M(2)-1 % left, right
  35. sh = ((X(:,2) >= z(i) - w(2)/2) & (X(:,2) <= z(i) + w(2)/2)); % horizontal slice
  36. H(1,i) = sum(W(sh & yu)); % left
  37. H(M(1),i) = sum(W(sh & ya)); % right
  38. end
  39. for j=2:M(1)-1 % bottom, top
  40. sv = ((X(:,1) >= y(j) - w(1)/2) & (X(:,1) <= y(j) + w(1)/2)); % vertical slice
  41. H(j,1) = sum(W(sv & zu)); % bottom
  42. H(j,M(2)) = sum(W(sv & za)); % top
  43. end
  44. % interior
  45. for i=2:M(2)-1
  46. for j=2:M(1)-1
  47. sh = ((X(:,2) >= z(i) - w(2)/2) & (X(:,2) <= z(i) + w(2)/2)); % horizontal slice
  48. sv = ((X(:,1) >= y(j) - w(1)/2) & (X(:,1) <= y(j) + w(1)/2)); % vertical slice
  49. H(j,i) = sum(W(sh & sv));
  50. end
  51. end
  52. %% plot
  53. if nargout == 0
  54. figure
  55. bar3c(H,y,z,[],C);
  56. axis([min(y) max(y) min(z) max(z) 0 1.1*max(H(:))])
  57. clear H B
  58. return
  59. end
  60. end
  61. function H = bar3c(Z,x,y,w,C)
  62. %% 3D bar plot from matrix (colored by height)
  63. % function H = bar3c(Z,x,y,w,C)
  64. %
  65. % Input: Z - NxM matrix
  66. % x - 1xN grid (default: 1:N)
  67. % y - 1xM grid (default: 1:M)
  68. % w - 1x2 bar half-widths (default: full half-width)
  69. % C - Kx3 colormap or
  70. % NxMx3 bar colors (default: colormap(gray))
  71. % Output: H - matrix of surf handles
  72. %
  73. % bar3c creates a 3D colored bar plot by plotting each bar as an individual
  74. % surface object with its own color.
  75. %
  76. % Example (Gaussian bar plot):
  77. % M = 50; % grid resolution
  78. % x = linspace(-3,3,M); % x-grid
  79. % y = linspace(-3,3,M); % y-grid
  80. % C = [0.3 -0.2; -0.2 0.6]; % covariance
  81. % [X,Y] = meshgrid(x,y);
  82. % XY = [X(:) Y(:)]; % grid pairs
  83. % Z = 1/sqrt(det(2*pi*C))*exp(-1/2*sum((XY/C).*XY,2));
  84. % Z = reshape(Z,[M,M]);
  85. % figure
  86. % bar3c(Z,x,y,[],jet(50))
  87. %
  88. % Johannes Traa - July 2013
  89. [N,M] = size(Z);
  90. %% check inputs
  91. if nargin < 2 || isempty(x); x = 1:N; end
  92. if nargin < 3 || isempty(y); y = 1:M; end
  93. if nargin < 4 || isempty(w); w = [x(2)-x(1) y(2)-y(1)]/2; end
  94. if nargin < 5 || isempty(C); C = colormap(gray); end
  95. %% x and y info for surfs
  96. Hx = cell(1,N); % x values of surf
  97. Hy = cell(1,M); % y values of surf
  98. for i=1:N
  99. X = x(i) + [-w(1) -w(1) w(1) w(1)];
  100. Hx{i} = repmat(X,[4,1]);
  101. end
  102. for j=1:M
  103. Y = y(j) + [-w(2) -w(2) w(2) w(2)]';
  104. Hy{j} = repmat(Y,[1,4]);
  105. end
  106. %% bar coloring
  107. if ismatrix(C)
  108. r = ceil((Z-min(Z(:)))/(max(Z(:))-min(Z(:)))*size(C,1));
  109. r(r(:)==0) = 1;
  110. end
  111. %% make bars
  112. hold on
  113. H = zeros(N,M); % surf handles
  114. Hz = zeros(4,4); %+min(Z(:)); % z values of surf
  115. for i=1:N
  116. for j=1:M
  117. Hz(2:3,2:3) = ones(2,2)*Z(i,j);
  118. % bar color
  119. if ismatrix(C); C_ij = C(r(i,j),:);
  120. else C_ij = reshape(C(i,j,:),[1,3]);
  121. end
  122. % plot bar
  123. H(i,j) = surf(Hx{i},Hy{j},Hz,'FaceColor',C_ij,'EdgeColor','k');
  124. end
  125. end
  126. view(-50,40)
  127. axis vis3d tight
  128. grid on
  129. drawnow
  130. %% output
  131. if nargout < 1; clear H; end
  132. end