-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pej_Plot_Dist.m
executable file
·71 lines (60 loc) · 2.36 KB
/
Pej_Plot_Dist.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
% This function makes a violin distribution plots of the columns in "data"
% place at position "x", with "color", in the "direction", and full or
% "Dashed".
% Example:
% Pej_Plot_Dist([4 4], repmat(X,1,2), [1 0 0],[-1; 1]);
% this plots X in a symmetric violin at point 4, in red.
% NOTE: if you want to plot some log-normal data, just set the figure YScal
% to log and it goes automatically:
% figure;
% set(gca, 'YScale', 'log');
% Pej_Plot_Dist([4 4], repmat(X,1,2), [1 0 0],[-1; 1]);
% Written by pejman.m@gmail.com
% 2013 Sept.
% -----------------
function Pej_Plot_Dist(x,data, Color, Direction, Dashed, Scale)
if nargin<2; data = x; x = 1:size(data,2); end
MidPoints = median(data);
if strcmpi(get(gca, 'YScale'), 'log')
%% estimate densities from the log(data), and plot it back on a semilogY scale.
data = log(data);
isLog = true;
else
isLog = false;
end
N = size(data,2);
if nargin<3 || isempty(Color); Color = repmat([.6,0,.05], N, 1); end
if nargin<4 || isempty(Direction); Direction = ones(N,1); end
if nargin<5 || isempty(Dashed); Dashed = false; end
if nargin<6 || isempty(Scale); Scale = nan; end
if size(Color,1) == 1; Color = repmat(Color ,N,1); end
if size(Direction,1)== 1; Direction = repmat(Direction,N,1); end
if length(Scale) == 1; Scale = repmat(Scale ,N,1); end
for i = 1:N
X975 = quantile(data(:,i), .975);
X025 = quantile(data(:,i), .025);
[f,xi] = ksdensity(data(:,i),linspace(X025, X975, 300));
if isnan(Scale(i))
f = f/max(f) * .4;
else
f = f * Scale(i);
end
if Dashed
tmpF = [f; f];
tmpF(1,1:2:end)=0; tmpF(2,2:2:end)=0;
tmpXi= [xi;xi];
f = tmpF(:)';
xi = tmpXi(:)';
end
f(xi>X975) = []; f(xi<X025) = [];
xi(xi>X975) = []; xi(xi<X025) = [];
if isLog
xi = exp(xi);
end
hold on
% fill(x(i) + Direction(i) * [0 f 0],[xi(1) xi xi(end) ], Color(i,:), 'edgecolor', Color(i,:), 'facecolor', 'none');
patch(x(i) + Direction(i) * [0 f 0 0],[xi(1) xi xi(end) xi(1)], Color(i,:), 'edgecolor', Color(i,:), 'linestyle', 'none');%, 'facecolor', 'none');
plot(x(i), MidPoints(i), 'o', 'color', [1 1 1], 'linewidth', .5 , 'markersize', 6, 'markerfacecolor', 0*[1 1 1])
plot(x(i), MidPoints(i), '+', 'color', [1 1 1], 'linewidth', 1 , 'markersize', 4 )
end
end