-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexscatter.m
219 lines (192 loc) · 6.04 KB
/
hexscatter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
function h = hexscatter(xdata, ydata, xlims, ylims, res, drawEdges, showZeros, NV)
arguments
xdata
ydata
xlims(1,2) double = [min([xdata(:);ydata(:)]) max([xdata(:);ydata(:)])]
ylims(1,2) double = [min([xdata(:);ydata(:)]) max([xdata(:);ydata(:)])]
res(1,1) double = 50
drawEdges(1,1) logical = 0
showZeros(1,1) logical = 0
NV.title char = ''
NV.charlbl char = ''
NV.cblbl char = 'counts'
NV.cbnds double = []
NV.cscale char {mustBeMember(NV.cscale,{'log','linear'})} = 'log'
NV.Interpreter char {mustBeMember(NV.Interpreter,{'tex','latex','none'})} = 'latex'
NV.axis = 'equal'
NV.reflineQ = true
end
% HEXSCATTER A scatter-plot substitute - generate a density plot using hexagonal patches.
%% h = HEXSCATTER( x, y, ... )
% Gordon Bean, February 2014
%
% Syntax
% hexscatter(xdata, ydata)
% hexscatter(xdata, ydata, 'Name', Value, ...)
% h = hexscatter(...)
%
% Description
% hexscatter(xdata, ydata) creates a density plot of the ydata versus the
% xdata using hexagonal tiles. xdata and ydata should be vectors. NaN
% values (and their corresponding values in the other vector) are ignored.
%
% hexscatter(xdata, ydata, 'Name', Value, ...) accepts name-value pairs of
% arguments from the following list (defaults in {}):
% 'xlim' { [min(xdadta(:) max(xdata(:))] } - a 2-element vector containing
% the lower and upper bounds of the 2nd dimension of the grid.
% 'ylim' { [min(ydadta(:) max(ydata(:))] } - a 2-element vector containing
% the lower and upper bounds of the 1st dimension of the grid.
% 'res' { 50 } - the resolution, or number of bins in each dimension. The
% total number of bins will be the resolution squared.
% 'drawEdges' { false } - if true, edges are drawn around each hexagonal
% patch.
% 'showZeros' { false } - if true, bins with 0 counts are shaded; if
% false, only bins with non-zero counts are colored.
%
% h = hexscatter( ... ) returns the object handle to the patch object
% created.
%
% Examples
% hexscatter(rand(2000,1), rand(2000,1))
%
% hexscatter(rand(2000,1), rand(2000,1), 'res', 90)
%
% Also available in the Bean Matlab Toolkit:
% https://github.com/brazilbean/bean-matlab-toolkit
% params = default_param( varargin, ...
% 'xlim', [min(xdata(:)) max(xdata(:))], ...
% 'ylim', [min(ydata(:)) max(ydata(:))], ...
% 'res', 50, ...
% 'drawEdges', false, ...
% 'showZeros', false);
if drawEdges
ec = 'flat';
else
ec = 'none';
end
%% Determine grid
xl = xlims;
yl = ylims;
xbins = linspace(xl(1), xl(2), res);
ybins = linspace(yl(1), yl(2), res);
dy = diff(ybins([1 2]))*0.5;
[X, Y] = meshgrid(xbins, ybins);
n = size(X,1);
Y(:,1:fix(end/2)*2) = ...
Y(:,1:fix(end/2)*2) + repmat([0 dy],[n,fix(n/2)]);
%% Map points to boxes
nix = isnan(xdata) | isnan(ydata);
xdata = xdata(~nix);
ydata = ydata(~nix);
% Which pair of columns?
dx = diff(xbins([1 2]));
foox = floor((xdata - xbins(1)) ./ dx)+1;
foox(foox > length(xbins)) = length(xbins);
% Which pair of rows?
% Use the first row, which starts without an offset, as the standard
fooy = floor((ydata - ybins(1)) ./ diff(ybins([1 2])))+1;
fooy(fooy > length(ybins)) = length(ybins);
% Which orientation
orientation = mod(foox,2) == 1;
% Map points to boxes
foo = [xdata - xbins(foox)', ydata - ybins(fooy)'];
% Which layer
layer = foo(:,2) > dy;
% Convert to block B format
toflip = layer == orientation;
foo(toflip,1) = dx - foo(toflip,1);
foo(layer==1,2) = foo(layer==1,2) - dy;
% Find closest corner
dist = sqrt(sum(foo.^2,2));
dist2 = sqrt(sum(bsxfun(@minus, [dx dy], foo).^2, 2));
topright = dist > dist2;
%% Map corners back to bins
% Which x bin?
x = foox + ~(orientation == (layer == topright));
x(x > length(xbins)) = length(xbins);
% Which y bin?
y = fooy + (layer & topright);
y(y > length(ybins)) = length(ybins);
ii = sub2ind(size(X), y, x);
%% Determine counts
counts = sum(bsxfun(@eq, ii, 1:numel(X)),1);
newplot;
xscale = diff(xbins([1 2]))*2/3;
yscale = diff(ybins([1 2]))*2/3;
theta = 0 : 60 : 360;
x = bsxfun(@plus, X(:), cosd(theta)*xscale)';
y = bsxfun(@plus, Y(:), sind(theta)*yscale)';
if showZeros
h = patch(x, y, counts, 'edgeColor', ec);
else
jj = counts > 0;
h = patch(x(:,jj), y(:,jj), counts(jj), 'edgeColor', ec);
end
%additional plotting (modified, SGB 2020-10-19)
hold on
if NV.reflineQ
refline(1,0)
end
axis(NV.axis);
if ~isempty(NV.title)
title(NV.title,'Interpreter',NV.Interpreter)
end
cb = colorbar('TickLabelInterpreter',NV.Interpreter);
cb.Label.String = NV.cblbl;
cb.Label.Interpreter = NV.Interpreter;
ax = gca;
ax.ColorScale = NV.cscale;
if ~isempty(NV.cbnds)
ax.CLim = NV.cbnds;
cb.Limits = NV.cbnds;
end
% label for figure tiles, e.g. '(a)', '(b)', '(c)', '(d)'
if ~isempty(NV.charlbl)
text(0.025,0.95,NV.charlbl,'Units','normalized','FontSize',12,'Interpreter',NV.Interpreter)
end
xlim(xlims);
ylim(ylims);
hold off
if nargout == 0
clear h;
end
%% Function: default_param
% Gordon Bean, March 2012
% Copied from https://github.com/brazilbean/bean-matlab-toolkit
% function params = default_param( params, varargin )
% if (iscell(params))
% params = get_params(params{:});
% end
% defaults = get_params(varargin{:});
%
% for f = fieldnames(defaults)'
% field = f{:};
% if (~isfield( params, lower(field) ))
% params.(lower(field)) = defaults.(field);
% end
% end
% end
%% Function: get_params - return a struct of key-value pairs
% Gordon Bean, January 2012
%
% Usage
% params = get_params( ... )
%
% Used to parse key-value pairs in varargin - returns a struct.
% Converts all keys to lower case.
%
% Copied from https://github.com/brazilbean/bean-matlab-toolkit
% function params = get_params( varargin )
% params = struct;
%
% nn = length(varargin);
% if (mod(nn,2) ~= 0)
% error('Uneven number of parameters and values in list.');
% end
%
% tmp = reshape(varargin, [2 nn/2]);
% for kk = 1 : size(tmp,2)
% params.(lower(tmp{1,kk})) = tmp{2,kk};
% end
% end
end