-
Notifications
You must be signed in to change notification settings - Fork 0
/
drv_pylab.py
488 lines (373 loc) · 13.5 KB
/
drv_pylab.py
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# drv_pylab.py
#
# Copyright 2009 charley <charley@hosts-137-205-164-145.phys.warwick.ac.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# This file contain a number of drivers classes to matplotlib.
from matplotlib.pyplot import figure
from matplotlib import cm
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost
from matplotlib import rcParams
from numpy import linspace
from numpy import logspace
from numpy import sign
from numpy import average
from numpy import dtype
from numpy import real
from numpy import imag
from numpy import abs
from numpy import angle
from numpy import log10
from numpy import unwrap
from numpy import arctan2
from numpy import imag
from numpy import real
from scipy.signal import freqz
from misc import unitPrefix, prettyunit
def plotcomplexpolar(metaAry, size = (10, 7.5), dpi = 75, grid = True, legend = 0, fontsize = 15):
"""
metaArray function to do a simple 1D plot of complex array as magnitude and phase angle.
legend:
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
"""
if legend is None:
legend = 0
axis = metaAry['range']
mag = abs(metaAry.data)
pha = angle(metaAry.data, deg=True)
# Load the plotting ranges and units
x0 = axis['begin'][0]
x1 = axis['end'][0]
my0 = min(mag)
my1 = max(mag)
py0 = min(pha)
py1 = max(pha)
xunit = axis['unit'][0]
myunit = metaAry['unit']
pyunit = 'Degree'
# Leave 10% margin in the y axis
mmean = average((my0, my1))
mreach = abs(my0-my1) / 2 / 0.9
my0 = sign(my0-mmean) * mreach + mmean
my1 = sign(my1-mmean) * mreach + mmean
pmean = average((py0, py1))
preach = abs(py0-py1) / 2 / 0.9
py0 = sign(py0-pmean) * preach + pmean
py1 = sign(py1-pmean) * preach + pmean
# Apply unit prefix if unit is defined
xunit, x0, x1, xscale = prettyunit(xunit, x0, x1)
myunit, my0, my1, myscale = prettyunit(myunit, my0, my1)
pyunit, py0, py1, pyscale = prettyunit(pyunit, py0, py1)
if myscale != 1:
mag = mag * myscale
if pyscale != 1:
pha = pha.copy() * pyscale
xlabl = lbl_repr(axis['label'][0], xunit)
mylabl = lbl_repr(metaAry['label'], myunit, "Magnitude")
pylabl = lbl_repr(metaAry['label'], pyunit, "Phase angle")
title = metaAry['name']
fig = figure(figsize=size, dpi = dpi)
host = SubplotHost(fig, 111)
fig.add_subplot(host)
par = host.twinx()
if axis['log'][0] is False:
x = linspace(x0, x1, len(metaAry))
else:
raise NotImplemented, "Log axis is not yet implemented."
host.plot(x, mag, 'b-', label=lbl_repr(axis['label'][0], '', "Magnitude"))
par.plot(x, pha, 'r--', label=lbl_repr(axis['label'][0], '', "Phase"))
host.grid(grid)
host.set_xlabel(xlabl, fontsize=fontsize)
host.set_ylabel(mylabl, fontsize=fontsize)
par.set_ylabel(pylabl, fontsize=fontsize)
host.set_xlim([x0, x1])
host.set_ylim([my0, my1])
par.set_ylim([py0, py1])
if fontsize is not None:
host.set_title(title, fontsize=int(fontsize*1.3))
else:
host.set_title(title)
if legend >= 0:
host.legend(loc=legend)
return fig, host, par
def plotcomplex(metaAry, size = (10, 7.5), dpi = 75, grid = True, legend = 0, fontsize = 15):
"""
metaArray function to do a simple 1D plot of complex array as real and imaginary parts.
legend:
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
"""
if legend is None:
legend = 0
axis = metaAry['range']
rdata = metaAry.data.real
idata = metaAry.data.imag
# Load the plotting ranges and units
x0 = axis['begin'][0]
x1 = axis['end'][0]
ry0 = min(rdata)
ry1 = max(rdata)
iy0 = min(idata)
iy1 = max(idata)
xunit = axis['unit'][0]
ryunit = metaAry['unit']
iyunit = metaAry['unit']
# Leave 10% margin in the y axis
rmean = average((ry0, ry1))
rreach = abs(ry0-ry1) / 2 / 0.9
ry0 = sign(ry0-rmean) * rreach + rmean
ry1 = sign(ry1-rmean) * rreach + rmean
imean = average((iy0, iy1))
ireach = abs(iy0-iy1) / 2 / 0.9
iy0 = sign(iy0-imean) * ireach + imean
iy1 = sign(iy1-imean) * ireach + imean
# Apply unit prefix if unit is defined
xunit, x0, x1, xscale = prettyunit(xunit, x0, x1)
ryunit, ry0, ry1, ryscale = prettyunit(ryunit, ry0, ry1)
iyunit, iy0, iy1, iyscale = prettyunit(iyunit, iy0, iy1)
if ryscale != 1:
rdata = rdata.copy() * ryscale
if iyscale != 1:
idata = idata.copy() * iyscale
xlabl = lbl_repr(axis['label'][0], xunit)
rylabl = lbl_repr(metaAry['label'], ryunit, "Real part")
iylabl = lbl_repr(metaAry['label'], iyunit, "Imaginary part")
title = metaAry['name']
fig = figure(figsize=size, dpi = dpi)
host = SubplotHost(fig, 111)
fig.add_subplot(host)
par = host.twinx()
if axis['log'][0] is False:
x = linspace(x0, x1, len(metaAry))
else:
raise NotImplemented, "Log axis is not yet implemented."
host.plot(x, rdata, 'b-', label=lbl_repr(axis['label'][0], '', "Real"))
par.plot(x, idata, 'r--', label=lbl_repr(axis['label'][0], '', "Imaginary"))
host.grid(grid)
host.set_xlabel(xlabl, fontsize=fontsize)
host.set_ylabel(rylabl, fontsize=fontsize)
par.set_ylabel(iylabl, fontsize=fontsize)
host.set_xlim([x0, x1])
host.set_ylim([ry0, ry1])
par.set_ylim([iy0, iy1])
if fontsize is not None:
host.set_title(title, fontsize=int(fontsize*1.3))
else:
host.set_title(title)
if legend >= 0:
host.legend(loc=legend)
return fig, host, par
def plot1d(metaAry, size = (10, 7.5), dpi = 75, grid = True, legend = None, fontsize = 15,\
fig = None, ax = None, label = None):
"""
metaArray function to do a simple 1D plot.
legend:
'best' 0
'upper right' 1
'upper left' 2
'lower left' 3
'lower right' 4
'right' 5
'center left' 6
'center right' 7
'lower center' 8
'upper center' 9
'center' 10
label Label for the legend display, default to metaAry['range']['label'][0]
"""
if metaAry.dtype is dtype('complex'):
return plotcomplex(metaAry, size = size, dpi = dpi, grid = grid, legend = legend, fontsize = fontsize)
if legend is None:
legend = -1
axis = metaAry['range']
data = metaAry.data
# Load the plotting ranges and units
x0 = axis['begin'][0]
x1 = axis['end'][0]
y0 = min(metaAry.data)
y1 = max(metaAry.data)
xunit = axis['unit'][0]
yunit = metaAry['unit']
# Leave 10% margin in the y axis
mean = average((y0, y1))
reach = abs(y0-y1) / 2 / 0.9
y0 = sign(y0-mean) * reach + mean
y1 = sign(y1-mean) * reach + mean
# Apply unit prefix if unit is defined
xunit, x0, x1, xscale = prettyunit(xunit, x0, x1)
yunit, y0, y1, yscale = prettyunit(yunit, y0, y1)
if yscale != 1:
data = data.copy() * yscale
xlabl = lbl_repr(axis['label'][0], xunit)
ylabl = lbl_repr(metaAry['label'], yunit)
title = metaAry['name']
# check if object is 1D metaArray object
if fig is None:
fig = figure(figsize=size, dpi = dpi)
if ax is None:
ax = fig.add_subplot(111)
else:
x00, x01 = ax.get_xlim()
y00, y01 = ax.get_ylim()
x0 = min((x0, x00))
y0 = min((y0, y00))
x1 = max((x1, x01))
y1 = max((y1, y01))
if axis['log'][0] is False:
x = linspace(x0, x1, len(metaAry))
else:
raise NotImplemented
if label is None:
label = axis['label'][0]
ax.plot(x, data, label=label)
ax.grid(grid)
ax.set_xlabel(xlabl, fontsize=fontsize)
ax.set_ylabel(ylabl, fontsize=fontsize)
ax.set_xlim([x0, x1])
ax.set_ylim([y0, y1])
if fontsize is not None:
ax.set_title(title, fontsize=int(fontsize*1.3))
else:
ax.set_title(title)
if legend >= 0:
ax.legend(loc=legend)
return fig, ax
def plot2d(metaAry, size = (10, 7.5), dpi = 75, fontsize = 15, cmap = None, \
nticks = 5, aspect_ratio = 1.0, corient = 'vertical', cformat = None,
vmin = None, vmax = None):
"""
metaArray function to do a simple 2D plot.
cmap Colour map, default is pyplot.cm.spectral
nticks Number of ticks in the colour bar
aspect_ratio Aspect ratio of the plot {float|'ij'|'xy'}
float: Fixed aspect ratio by the given number
'ij': Same aspect ratio as ij space
'xy': Same aspect ratio as xy space
corient Colorbar orientation ('vertical'|'horizontal')
cformat Colorbar format [ None | format string | Formatter object ]
vmin Minimum value for the colour scale
vmax Maximum value for the coloir scale
"""
if cmap is None:
cmap = cm.spectral
if corient is not 'horizontalt':
corient = 'vertical'
axis = metaAry['range']
data = metaAry.data
x0 = axis['begin'][0]
x1 = axis['end'][0]
y0 = axis['begin'][1]
y1 = axis['end'][1]
if vmin is None:
v0 = metaAry.data.min()
else:
v0 = vmin
if vmax is None:
v1 = metaAry.data.max()
else:
v1 = vmax
xunit = axis['unit'][0]
yunit = axis['unit'][1]
vunit = metaAry['unit']
# Apply unit prefix if unit is defined
xunit, x0, x1, xscale = prettyunit(xunit, x0, x1)
yunit, y0, y1, yscale = prettyunit(yunit, y0, y1)
vunit, v0, v1, vscale = prettyunit(vunit, v0, v1)
if vscale != 1:
data = data.copy() * vscale
xlabl = lbl_repr(axis['label'][0], xunit)
ylabl = lbl_repr(axis['label'][1], yunit)
vlabl = lbl_repr(metaAry['label'], vunit)
ticks = linspace(v0, v1, nticks)
ticks_lbl = []
for i in range(nticks):
ticks_lbl.append("%(val)0.4g" % {'val':ticks[i]})
# Aspect ration of the plot
if aspect_ratio == 'ij':
ratio = data.shape
ratio = float(ratio[1]) / ratio[0]
elif aspect_ratio == 'xy':
ratio = float(y1 - y0) / float(x1 - x0)
else:
try:
ratio = float(aspect_ratio)
except:
print "*** Warning! Unrecognisable aspect ratio spec. Using the default instead."
ratio = 1.0
ratio /= float(y1 - y0) / float(x1 - x0)
ratio = abs(ratio)
# Make plot with vertical (default) colorbar
fig = figure(figsize=size, dpi = dpi)
ax = fig.add_subplot(111)
extent = (x0, x1, y0, y1)
cax = ax.imshow(data.transpose()[::-1], cmap=cmap, extent=extent, interpolation = 'bicubic', vmin = v0, vmax = v1, aspect=ratio)
cbar = fig.colorbar(cax, ticks=ticks, orientation=corient, format=cformat)
# ax.set_size(fontsize)
ax.set_xlabel(xlabl, fontsize=fontsize) # Label font size
ax.set_ylabel(ylabl, fontsize=fontsize)
rcParams.update({'font.size': fontsize}) # Value font size
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar.ax.set_yticklabels(ticks_lbl)
cbar.set_label(vlabl, fontsize=fontsize)
if fontsize is not None:
ax.set_title(metaAry['name'], fontsize=int(fontsize*1.3))
else:
ax.set_title(metaAry['name'])
return fig, ax
def lbl_repr(label = None, unit = None, string = None):
"""
Format axis label and unit into a nice looking string
String: Additional string between label and unit.
"""
lbl = ''
try:
# Ignore label if it is not a string, for it can be None also
lbl += label
except TypeError:
pass
try:
# Append the additional arguement if exist
lbl += ' [' + string + ']'
except TypeError:
pass
try:
if unit == '':
pass # Unit less quantities
else:
lbl += ' (' + unit + ')'
except TypeError:
# Most likely unit is not defined, i.e. not a string.
lbl += ' (Arb.)'
return lbl