-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot2D2D.m
More file actions
302 lines (296 loc) · 10.5 KB
/
plot2D2D.m
File metadata and controls
302 lines (296 loc) · 10.5 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
function [f] = plot2D2D(X,Y,U,V,varargin)
%PLOT2D2D function to create a plot with a 2D data and a 2D colormap.
% ====== INPUTS ==========================
% X coordinates
% Y coordinates
% U Value 1 (if angle, use circular 2D map)
% V Value 2 (no angle option)
%
% ====== Optional inputs =================
% 'Colormap'
% -- Normal maps --
% 'bramm' regular 2D map
% 'schumann' regular 2D map
% 'steiger' regular 2D map
% 'teuling' regular 2D map (default)
% 'ziegler' regular 2D map
%
% -- Circular maps -- U is interpreted as angle, cmap is labeled in deg
% 'ChromaConst' circular 2D map (u = angle) (recommended)
% 'chromaMax' circular 2D map (u = angle) (can cause misleading
% edges, but is more vivid)
% 'complex' circular 2D map
%
% -- Custom map --
% 'custom' requires own map 'custom.png'
%
% 'umin' (overwritten for circular colormaps)
% 'umax' (overwritten for circular colormaps)
% 'vmin'
% 'vmax'
%
% 'ulabel'
% string
% 'vlabel'
% string
%
% 'AngleUnit' Only used with a circular colormap
% - 'rad' (default)
% - 'degree'
%
% 'ColormapPlot'
% true (default) Colormap will be added with lables and ticks
% false No colormap
%
% 'ColormapLocation'
% 'New fig' (default)
% 'South' Subplot south
% 'East' Subplot east
%
% ====== Credit ==========================
% This code has been adapted from a first version from Dave B:
% https://nl.mathworks.com/matlabcentral/profile/authors/14836566
% The colormaps bramm, schumann, steiger, teuling, ziegler are based on
% the work presented in
% Explorative Analysis of 2D Color Maps
% Steiger, M., et al., Proceedings of WSCG (23), 151-160,
% Eurographics Assciation, Vaclav Skala - Union Agency, 2015
% Source: https://github.com/dominikjaeckle/Color2D
% The colormaps chroma_const and chroma_max are based on the work
% presented in this github:
% https://github.com/endolith/complex_colormap
% Author: Marcus Becker, Date: 2021 - 11 - 23
%
% ====== Examples ========================
% E1 - random data:
% plot2D2D(rand(10),rand(10),rand(10),rand(10))
%
% E2 - angle data:
% [X,Y] = meshgrid(linspace(0,10),linspace(0,10))
% Phi = X;
% M = sin(X).*sin(Y);
% plot2D2D(X,Y,Phi,M,'Colormap','ChromaConst')
%
% E3 - dynamic system - Van der Pol oscillator
% [X1,X2] = meshgrid(linspace(-3.5,3.5),linspace(-3.5,3.5));
% X1_d = X1-1/3*X1.^3-X2; X2_d = X1;
% phi = atan2(X2_d(:),X1_d(:));
% mag = sqrt(X1_d(:).^2 + X2_d(:).^2);
% plot2D2D(X1,X2,phi,mag,'Colormap','ChromaConst','ulabel','Direction','vlabel','Magnitude')
% plot2D2D(X1,X2,phi,mag,'Colormap','ChromaMax','ulabel','Direction','vlabel','Magnitude')
% plot2D2D(X1,X2,phi,mag,'Colormap','Complex','ulabel','Direction','vlabel','Magnitude','ColormapLocation','East')
%
% E4 - dynamic system - saddle point
% [X1,X2] = meshgrid(linspace(-9,9),linspace(-4,4));
% X1_d = X2; X2_d = -sin(X1) -0.3*X2;
% plot2D2D(X1,X2,X1_d,X2_d,'Colormap','schumann','ulabel','x1 d/dt','vlabel','x2 d/dt')
%% Default parameters
colormap = 'teuling';
umin = min(U(:));
umax = max(U(:));
vmin = min(V(:));
vmax = max(V(:));
ulabel = '';
vlabel = '';
angleunit = 'rad';
colormapplot = true;
colormaplocation = 'newfig';
angledata = false;
%% Correct depending on arguments passed
if nargin>3
%varargin is used
for i=1:2:length(varargin)
%go through varargin which is build in pairs and assign variable
%stored in the first entry with the value stored in the second
%entry.
if isnumeric(varargin{i+1})
%Value is a number -> for 'eval' a string is needed, so convert
%num2str
eval([lower(varargin{i}) '=' num2str(varargin{i+1}) ';']);
else
%Value is a string, can be used as expected
stringVar = varargin{i+1};
eval([lower(varargin{i}) '= stringVar;']);
clear stringVar
end
end
end
%% Set limits
ulim = [umin,umax];
vlim = [vmin,vmax];
%% Load a colormap and store in a matrix of RGB values
% Note:
% Longterm it would be favourable to switch to a mathematical description
% of the colormaps. Many 2D maps are presented in
% https://github.com/igd-iva/colormap-explorer/tree/master/colormaps/src/main/java/de/fhg/igd/iva/colormaps/impl
% And would need translation from Java to Matlab.
% They are all published under the apache license 2.0:
% http://www.apache.org/licenses/LICENSE-2.0
switch replace(lower(colormap),' ','')
case 'teuling'
try
im = imread('teuling.png');
catch
error(['The map teuling.png is unavailable. See comments for '...
'map source to download again and replace file.'])
end
case 'schumann'
try
im = imread('schumann.png');
catch
error(['The map schumann.png is unavailable. See comments for '...
'map source to download again and replace file.'])
end
case 'steiger'
try
im = imread('steiger.png');
catch
error(['The map steiger.png is unavailable. See comments for '...
'map source to download again and replace file.'])
end
case 'ziegler'
try
im = imread('ziegler.png');
catch
error(['The map ziegler.png is unavailable. See comments for '...
'map source to download again and replace file.'])
end
case 'bremm'
try
im = imread('bremm.png');
catch
error(['The map bremm.png is unavailable. See comments for '...
'map source to download again and replace file.'])
end
case 'chromaconst' % ========== Circular color map
try
im = imread('ChromaConst.jpg');
catch
error(['The map ChromaConst.jpg is unavailable. See comments for '...
'map source to download again and replace file.'])
end
angledata = true;
ulim = [0,360];
case 'chromamax' % ============ Circular color map
try
im = imread('ChromaMax.jpg');
catch
error(['The map ChromaMax.jpg is unavailable. See comments for '...
'map source to download again and replace file.'])
end
angledata = true;
ulim = [0,360];
case 'complex' % ============== Circular color map
try
im = imread('Complex.png');
catch
error(['The map ChromaMax.jpg is unavailable. See comments for '...
'map source to download again and replace file.'])
end
angledata = true;
ulim = [0,360];
case 'custom'
try
im = imread('custom.png');
catch
error('The map custom.png is unavailable.')
end
otherwise
error(['Colormap ' colormap ' unknown. See comments for '...
'available maps.'])
end
rgb = double(reshape(im, [], 3))./255;
%% Mapping
% Normalize data (according to format)
if angledata
if strcmp(angleunit,'rad')
u_n = mod(U(:),2*pi)/(2*pi);
else
u_n = mod(U(:),360)/360;
end
else
u_n = rescale(U(:));
end
v_n = rescale(V(:));
% interpolate to find a color for each x and y, mapping the range of the
% colormap onto [0 1]
c_column = round(interp1(linspace(0,1,size(im,2)), 1:size(im,2), u_n));
c_row = round(interp1(linspace(0,1,size(im,1)), 1:size(im,1), v_n));
rgb_row = sub2ind(size(im),c_row,c_column,ones(size(c_row))); % this is the same as the index into im of red values
c = rgb(rgb_row,:);
%% Plotting
DT = delaunay(X,Y);
f = figure;
if colormapplot
switch colormaplocation
case 'south'
subplot(2,1,2)
image(im)
axis equal
set(gca,'YDir','normal')
xlim([0,size(im,2)])
ylim([0,size(im,1)])
xticks([0,.25,.5,.75,1]*size(im,2))
xticklabels({num2str(ulim(1)),...
num2str((ulim(2)-ulim(1))/4+ulim(1)),...
num2str((ulim(2)-ulim(1))/2+ulim(1)),...
num2str((ulim(2)-ulim(1))*3/4+ulim(1)),...
num2str(ulim(2))})
yticks([0,.25,.5,.75,1]*size(im,1))
yticklabels({num2str(vlim(1)),...
num2str((vlim(2)-vlim(1))/4+vlim(1)),...
num2str((vlim(2)-vlim(1))/2+vlim(1)),...
num2str((vlim(2)-vlim(1))*3/4+vlim(1)),...
num2str(vlim(2))})
subplot(2,1,1)
case 'east'
subplot(1,2,2)
image(im)
axis equal
xlabel(ulabel)
ylabel(vlabel)
set(gca,'YDir','normal')
xlim([0,size(im,2)])
ylim([0,size(im,1)])
xticks([0,.25,.5,.75,1]*size(im,2))
xticklabels({num2str(ulim(1)),...
num2str((ulim(2)-ulim(1))/4+ulim(1)),...
num2str((ulim(2)-ulim(1))/2+ulim(1)),...
num2str((ulim(2)-ulim(1))*3/4+ulim(1)),...
num2str(ulim(2))})
yticks([0,.25,.5,.75,1]*size(im,1))
yticklabels({num2str(vlim(1)),...
num2str((vlim(2)-vlim(1))/4+vlim(1)),...
num2str((vlim(2)-vlim(1))/2+vlim(1)),...
num2str((vlim(2)-vlim(1))*3/4+vlim(1)),...
num2str(vlim(2))})
subplot(1,2,1)
otherwise
f2 = figure;
image(im)
axis equal
xlabel(ulabel)
ylabel(vlabel)
set(gca,'YDir','normal')
xlim([0,size(im,2)])
ylim([0,size(im,1)])
xticks([0,.25,.5,.75,1]*size(im,2))
xticklabels({num2str(ulim(1)),...
num2str((ulim(2)-ulim(1))/4+ulim(1)),...
num2str((ulim(2)-ulim(1))/2+ulim(1)),...
num2str((ulim(2)-ulim(1))*3/4+ulim(1)),...
num2str(ulim(2))})
yticks([0,.25,.5,.75,1]*size(im,1))
yticklabels({num2str(vlim(1)),...
num2str((vlim(2)-vlim(1))/4+vlim(1)),...
num2str((vlim(2)-vlim(1))/2+vlim(1)),...
num2str((vlim(2)-vlim(1))*3/4+vlim(1)),...
num2str(vlim(2))})
figure(f);
end
end
patch('Faces',DT,'Vertices',[X(:),Y(:)],'FaceVertexCData',c,...
'EdgeColor','none','FaceColor','interp')
xlim([min(X(:)),max(X(:))])
ylim([min(Y(:)),max(Y(:))])
end