-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_distribution_strategies.m
231 lines (212 loc) · 8.46 KB
/
plot_distribution_strategies.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
220
221
222
223
224
225
226
227
228
229
230
231
function plot_distribution_strategies(distributions, varargin)
%PLOT_DISTRIBUTION_STRATEGIES Summary of this function goes here
% Detailed explanation goes here
addpath(fullfile(fileparts(mfilename('fullpath')), './extern/cm_and_cb_utilities'));
addpath(fullfile(fileparts(mfilename('fullpath')), './extern/'));
[mean_row, mean_col, row_labels, col_labels, ticks, ticks_labels, markers, ordered, widths, cm, bh, avgh, ar] = ...
process_options(varargin, ...
'MeanRow', 0, 'MeanColumn', 0, 'RowLabels', {}, 'ColumnLabels', {}, ...
'Ticks', [], 'TicksLabels', {}, 'Markers', {}, 'Ordered', 0, ...
'Widths', [], 'ColorMap', g_config.CLASSES_COLORMAP, 'BarHeight', 0.8, ...
'AverageBarsHeight', 0, 'AspectRatio', 1);
ncol = length(distributions) + mean_col;
% sanity checks
assert(~(mean_col && ordered) && ~(mean_row && ordered));
% we need the length of the longest distribution to make sure that all
% columns are of the same width
max_len = 0;
if ordered
% we have a fixed number of bins
max_len = size(distributions{1}, 2);
if ~isempty(widths)
assert(length(widths) == max_len);
max_len = sum(widths);
end
else
% variable length, look for the maximum
for i = 1:length(distributions)
max_len = max(max_len, max(sum(distributions{i}, 2)));
end
end
% maximum number of bars
nbars = 0;
for i = 1:length(distributions)
nbars = max(nbars, size(distributions{i}, 1));
end
if mean_row
nbars = nbars + 1;
end
% number of classes
if ~ordered
nclasses = size(distributions{i}, 2);
else
un = [];
for i = 1:length(distributions)
tmp = distributions{i};
un = [un, tmp(:)'];
end
nclasses = max(un);
end
l = 0.96;
b = 0.025;
if ~isempty(row_labels) && length(row_labels{1}) > 1
ib = 0.025;
else
ib = 0.01;
end
w = (l - 2*b - (ncol - 1)*ib)/ncol;
h = (l - 2*b - avgh)*ar;
% need to compute the "total" distribution for the mean column
tot = [];
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'visible','on','Color','w', 'PaperPosition', [0.1 0 12 8],...
'PaperSize', [12 8],'PaperUnits', 'centimeters'); %Position plot at left hand corner with width 14cm and height 7cm.
axes('Position',[b b l l]); % "parent" axes
axis off;
for i = 1:ncol
is_mean_col = i == ncol && mean_col;
if is_mean_col
distr = tot / (ncol - 1);
else
distr = distributions{i};
if mean_col
if i == 1
tot = distr;
else
tot = tot + distr;
end
end
end
% create an axes inside the parent axes for the ii-the barh
sa = axes('Position', [b + w*(i - 1) + ib*(i - 1), b + 0.05 + avgh, w, h]); % position the ii-th barh
if mean_row
% one additional row for the mean
vals = zeros(size(distr, 1) + 1, size(distr, 2));
m = mean(distr);
vals(1, :) = m;
vals(2:size(vals, 1), :) = distr;
else
vals = distr;
end
n = size(vals, 1);
vals(size(vals, 1) + 1:nbars, :) = zeros(nbars - size(vals, 1), size(distr, 2));
if ordered
% rescale colormap
if size(cm, 1) > nclasses
cm = cmapping(nclasses, cm);
end
wbin = widths;
if isempty(wbin)
wbin = ones(1, size(distr, 2));
end
nbins = length(wbin);
for k = 1:nbars
tmp = nan(1, nbins);
tmp(vals(k, :) >= 0) = wbin(vals(k, :) >= 0);
barh([k, k + 1], [tmp; zeros(1, nbins)], bh, 'Stacked');
% color the patches
P = findobj(gca, 'type', 'patch');
for l = 1:nbins
if vals(k, l) > 0
set(P(nbins - l + 1), 'edgecolor', cm(vals(k, l), :), 'facecolor', cm(vals(k, l), :));
elseif vals(k, l) == 0
set(P(nbins - l + 1), 'edgecolor', [1, 1, 1], 'facecolor', [1, 1, 1]);
end
end
hold on;
end
else
vals(vals(:) <= 0) = nan;
barh(1:n, vals, bh, 'Stacked', 'Parent', sa);
colormap(cm);
end
set(gca,'box','off');
set(gca,'XLim', [0, max_len]);
if ~isempty(ticks)
set(gca, 'XTick', ticks);
end
if ~isempty(ticks_labels)
set(gca, 'XTickLabel', ticks_labels, 'FontSize', 6);
end
if ~isempty(row_labels)
if i == 1 || iscell(row_labels{1})
% set labels
if mean_row
lbls = {'AVG'};
else
lbls = {};
end
if iscell(row_labels{1})
nl = length(row_labels{i});
lbls = [lbls, row_labels{i}];
else
lbls = [lbls, row_labels];
nl = length(row_labels);
end
set(gca,'YTick', 1:nl, 'YTickLabel', lbls, 'FontSize', 8 );
else
set(gca,'YTick', []);
end
else
set(gca,'YTick', []);
end
hold on;
if ~isempty(col_labels)
if mean_col && i == ncol
text(max_len / 2, nbars + 2, 'Average', 'FontSize', 6, 'HorizontalAlignment','center');
else
text(max_len / 2, nbars + 2, col_labels{i}, 'FontSize', 6, 'HorizontalAlignment','center');
end
end
if ~isempty(markers) && ~is_mean_col
% mark cases where animal found the platform
m = markers{i};
for j = 1:length(m)
if m(j)
if mean_row
off = 1;
else
off = 0;
end
hold on;
if ordered
text(sum( (distr(j, :) >= 0).*widths) + sum(widths)/50, j + off, 'x', 'FontSize', 6, 'FontWeight', 'bold');
else
text(sum(distr(j, :)) + max_len/100, j + off, 'x', 'FontSize', 6, 'FontWeight', 'bold');
end
end
end
end
% set(gca, 'FontSize', 6, 'LineWidth', 0.8);
if avgh > 0
% create an axes inside the parent axes for the ii-the barh
sa = axes('Position', [b + w*(i - 1) + ib*(i - 1), b + 0.05, w, avgh - 0.02]);
if ordered
% rescale colormap
if size(cm, 1) > nclasses
cm = cmapping(nclasses, cm);
end
wbin = widths;
if isempty(wbin)
wbin = ones(1, size(distr, 2));
end
nbins = length(wbin);
tmp = zeros(nclasses, nbins);
for k = 1:nbars
for j = 1:nbins
if vals(k, j) > 0
tmp(vals(k, j), j) = tmp(vals(k, j), j) + 1;
end
end
end
bar((1:nbins)*3.35 - ones(1, nbins)*1.6, tmp', 'Stacked');
set(gca,'box','off');
axis off;
else
%vals(vals(:) <= 0) = nan;
%barh(1:n, vals, bh, 'Stack', 'Parent', sa);
%colormap(cm);
end
end
end
end