-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotContour.m
57 lines (50 loc) · 2.04 KB
/
plotContour.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
%% Helper function to simplify plotting
% Michael Nickerson 2020
% plotContour(x,y,z[, num, title, xla, yla, zla, fillState])
% Easy contour plotting
% x, y, z all vectors
function h = plotContour(x, varargin)
% Make x a column vector
if size(x,2) > size(x,1); x = x'; end
% If x multidimensional, use that for all possible
if size(x,2) > 1
y = x(:,2);
else
y = varargin{1}; varargin(1) = [];
end
if size(x,2) > 2
z = x(:,3);
else
z = varargin{1}; varargin(1) = [];
end
x = x(:,1);
% Make a z grid if not already in the right format
if size(z, 2) < 2
% First merge duplicate rows
[~, ii, iDup] = unique([x y], 'rows'); % Find duplicated rows
x = x(ii); y = y(ii); % Drop duplicated rows
zz = accumarray(iDup, z, [], @mean); % Merge z-values
% Get coordinate mapping and reduce x and y
[x, ~, izx] = unique(x); [y, ~, izy] = unique(y);
% Make a gridded z and assign correct values
[~,~,z] = ndgrid(x,y,NaN); iz = sub2ind(size(z), izx, izy);
z(iz) = zz;
end
% Generate plot
if (numel(varargin) >= 1)
figureSize(varargin{1}, 800, 650);
else
figureSize(figure(), 800, 650);
end
if (numel(varargin) >= 6) && (varargin{6} > 0)
[~,h] = contourf(x, y, z');
else
[~,h] = contour(x, y, z');
end
view(2);
if numel(varargin) >= 5; ylabel(colorbar, varargin{5}, 'FontSize', 16, 'FontName', 'Consolas', 'FontWeight', 'Bold'); zlabel(varargin{5}, 'FontSize', 16, 'FontName', 'Consolas', 'FontWeight', 'Bold'); end
if numel(varargin) >= 4; ylabel(varargin{4}, 'FontSize', 16, 'FontName', 'Consolas', 'FontWeight', 'Bold'); end
if numel(varargin) >= 3; xlabel(varargin{3}, 'FontSize', 16, 'FontName', 'Consolas', 'FontWeight', 'Bold'); end
if numel(varargin) >= 2; title(varargin{2}, 'FontSize', 20, 'FontName', 'Source Sans Pro', 'FontWeight', 'Bold'); end
drawnow; % Immediate display
end