-
Notifications
You must be signed in to change notification settings - Fork 2
/
scatter_kde.m
45 lines (42 loc) · 1.4 KB
/
scatter_kde.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
function h = scatter_kde(x, y, varargin)
% Scatter plot where each point is colored by the spatial density of nearby
% points. The function use the kernel smoothing function to compute the
% probability density estimate (PDE) for each point. It uses the PDE has
% color for each point.
%
% Input
% x <Nx1 double> position of markers on X axis
% y <Nx1 double> posiiton of markers on Y axis
% varargin can be used to send a set of instructions to the scatter function
% Supports the MarkerSize parameter
% Does not support the MarkerColor parameter
%
% Output:
% h returns handles to the scatter objects created
%
% Example
% % Generate data
% x = normrnd(10,1,1000,1);
% y = x*3 + normrnd(10,1,1000,1);
% % Plot data using probability density estimate as function
% figure(1);
% scatter_kde(x, y, 'filled', 'MarkerSize', 100);
% % Add Color bar
% cb = colorbar();
% cb.Label.String = 'Probability density estimate';
%
% author: Nils Haentjens
% created: Jan 15, 2018
% Use Kernel smoothing function to get the probability density estimate (c)
c = ksdensity([x,y], [x,y]);
if nargin > 2
% Set Marker Size
i = find(strcmp(varargin, 'MarkerSize'),1);
if ~isempty(i); MarkerSize = varargin{i+1}; varargin(i:i+1) = [];
else MarkerSize = []; end
% Plot scatter plot
h = scatter(x, y, MarkerSize, c, varargin{:});
else
h = scatter(x, y, [], c);
end
end