-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathplot.py
More file actions
465 lines (388 loc) · 15.7 KB
/
plot.py
File metadata and controls
465 lines (388 loc) · 15.7 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
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
"""Plot sound fields etc."""
from __future__ import division
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from matplotlib.collections import PatchCollection, LineCollection
from matplotlib.colors import ListedColormap
from mpl_toolkits import axes_grid1
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from . import util
from . import defs
def _register_cmap_clip(name, original_cmap, alpha):
"""Create a color map with "over" and "under" values."""
from matplotlib.colors import LinearSegmentedColormap
cdict = plt.cm.datad[original_cmap]
cmap = LinearSegmentedColormap(name, cdict)
cmap.set_over([alpha * c + 1 - alpha for c in cmap(1.0)[:3]])
cmap.set_under([alpha * c + 1 - alpha for c in cmap(0.0)[:3]])
plt.cm.register_cmap(cmap=cmap)
# The 'coolwarm' colormap is based on the paper
# "Diverging Color Maps for Scientific Visualization" by Kenneth Moreland
# http://www.sandia.gov/~kmorel/documents/ColorMaps/
_register_cmap_clip('coolwarm_clip', 'coolwarm', 0.7)
def _register_cmap_transparent(name, color):
"""Create a color map from a given color to transparent."""
from matplotlib.colors import colorConverter, LinearSegmentedColormap
red, green, blue = colorConverter.to_rgb(color)
cdict = {'red': ((0, red, red), (1, red, red)),
'green': ((0, green, green), (1, green, green)),
'blue': ((0, blue, blue), (1, blue, blue)),
'alpha': ((0, 0, 0), (1, 1, 1))}
cmap = LinearSegmentedColormap(name, cdict)
plt.cm.register_cmap(cmap=cmap)
_register_cmap_transparent('blacktransparent', 'black')
def virtualsource_2d(xs, ns=None, type='point', ax=None):
"""Draw position/orientation of virtual source."""
xs = np.asarray(xs)
ns = np.asarray(ns)
if ax is None:
ax = plt.gca()
if type == 'point':
vps = plt.Circle(xs, .05, edgecolor='k', facecolor='k')
ax.add_artist(vps)
for n in range(1, 3):
vps = plt.Circle(xs, .05+n*0.05, edgecolor='k', fill=False)
ax.add_artist(vps)
elif type == 'plane':
ns = 0.2 * ns
ax.arrow(xs[0], xs[1], ns[0], ns[1], head_width=0.05,
head_length=0.1, fc='k', ec='k')
def reference_2d(xref, size=0.1, ax=None):
"""Draw reference/normalization point."""
xref = np.asarray(xref)
if ax is None:
ax = plt.gca()
ax.plot((xref[0]-size, xref[0]+size), (xref[1]-size, xref[1]+size), 'k-')
ax.plot((xref[0]-size, xref[0]+size), (xref[1]+size, xref[1]-size), 'k-')
def secondarysource_2d(x0, n0, grid=None):
"""Simple plot of secondary source locations."""
x0 = np.asarray(x0)
n0 = np.asarray(n0)
ax = plt.gca()
# plot only secondary sources inside simulated area
if grid is not None:
x0, n0 = _visible_secondarysources_2d(x0, n0, grid)
# plot symbols
for x00 in x0:
ss = plt.Circle(x00[0:2], .05, edgecolor='k', facecolor='k')
ax.add_artist(ss)
def secondarysourcecontour_2d(x0, a0=True, ax=None):
"""Draw contour of secondary source distribution as polygon.
Parameters
----------
x0 : (N, 3) array_like
Secondary source positions.
a0 : bool or (N,) array_like, optional
Active secondary sources.
ax : Axes object, optional
The loudspeakers are plotted into this `matplotlib.axes.Axes`
object or -- if not specified -- into the current axes.
"""
x0 = util.asarray_of_rows(x0)
a0 = util.asarray_1d(a0)
# colormap with black and gray to indicate active parts
cmap = ListedColormap(np.stack((.5*np.ones(3), np.zeros(3))))
# setup line collection with secondary source contour
x0 = np.append(x0, x0[0:1, :], axis=0)
points = np.array([x0[:, 0], x0[:, 1]]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, cmap=cmap)
lc.set_linewidth(2)
# set color of line to indicate active parts
a0 = np.append(a0, a0[0])
active = np.logical_and(a0[0:-1], a0[1:])
lc.set_array(active)
# add collection of lines to current axis
if ax is None:
ax = plt.gca()
plt.gca().add_collection(lc)
def loudspeaker_2d(x0, n0, a0=0.5, size=0.08, show_numbers=False, grid=None,
ax=None):
"""Draw loudspeaker symbols at given locations and angles.
Parameters
----------
x0 : (N, 3) array_like
Loudspeaker positions.
n0 : (N, 3) or (3,) array_like
Normal vector(s) of loudspeakers.
a0 : float or (N,) array_like, optional
Weighting factor(s) of loudspeakers.
size : float, optional
Size of loudspeakers in metres.
show_numbers : bool, optional
If ``True``, loudspeaker numbers are shown.
grid : triple of array_like, optional
If specified, only loudspeakers within the *grid* are shown.
ax : Axes object, optional
The loudspeakers are plotted into this `matplotlib.axes.Axes`
object or -- if not specified -- into the current axes.
"""
x0 = util.asarray_of_rows(x0)
n0 = util.asarray_of_rows(n0)
a0 = util.asarray_1d(a0).reshape(-1, 1)
# plot only secondary sources inside simulated area
if grid is not None:
x0, n0 = _visible_secondarysources_2d(x0, n0, grid)
# normalized coordinates of loudspeaker symbol (see IEC 60617-9)
codes, coordinates = zip(*(
(Path.MOVETO, [-0.62, 0.21]),
(Path.LINETO, [-0.31, 0.21]),
(Path.LINETO, [0, 0.5]),
(Path.LINETO, [0, -0.5]),
(Path.LINETO, [-0.31, -0.21]),
(Path.LINETO, [-0.62, -0.21]),
(Path.CLOSEPOLY, [0, 0]),
(Path.MOVETO, [-0.31, 0.21]),
(Path.LINETO, [-0.31, -0.21]),
))
coordinates = np.column_stack([coordinates, np.zeros(len(coordinates))])
coordinates *= size
patches = []
for x00, n00 in util.broadcast_zip(x0, n0):
# rotate and translate coordinates
R = util.rotation_matrix([1, 0, 0], n00)
transformed_coordinates = np.inner(coordinates, R) + x00
patches.append(PathPatch(Path(transformed_coordinates[:, :2], codes)))
# add collection of patches to current axis
p = PatchCollection(patches, edgecolor='0', facecolor=np.tile(1 - a0, 3))
if ax is None:
ax = plt.gca()
ax.add_collection(p)
if show_numbers:
for idx, (x00, n00) in enumerate(util.broadcast_zip(x0, n0)):
x, y = x00[:2] - 1.2 * size * n00[:2]
ax.text(x, y, idx + 1, horizontalalignment='center',
verticalalignment='center')
def _visible_secondarysources_2d(x0, n0, grid):
"""Determine secondary sources which lie within *grid*."""
x, y = util.as_xyz_components(grid[:2])
idx = np.where((x0[:, 0] > x.min()) & (x0[:, 0] < x.max()) &
(x0[:, 1] > y.min()) & (x0[:, 1] < x.max()))
idx = np.squeeze(idx)
return x0[idx, :], n0[idx, :]
def loudspeaker_3d(x0, n0, a0=None, w=0.08, h=0.08):
"""Plot positions and normals of a 3D secondary source distribution."""
fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(111, projection='3d')
ax.quiver(x0[:, 0], x0[:, 1], x0[:, 2], n0[:, 0],
n0[:, 1], n0[:, 2], length=0.1)
plt.xlabel('x (m)')
plt.ylabel('y (m)')
plt.title('Secondary Sources')
fig.show()
def soundfield(p, grid, xnorm=None, cmap='coolwarm_clip', vmin=-2.0, vmax=2.0,
xlabel=None, ylabel=None, colorbar=True, colorbar_kwargs={},
ax=None, **kwargs):
"""Two-dimensional plot of sound field.
Parameters
----------
p : array_like
Sound pressure values (or any other scalar quantity if you
like). If the values are complex, the imaginary part is
ignored.
Typically, *p* is two-dimensional with a shape of *(Ny, Nx)*,
*(Nz, Nx)* or *(Nz, Ny)*. This is the case if
`sfs.util.xyz_grid()` was used with a single number for *z*,
*y* or *x*, respectively.
However, *p* can also be three-dimensional with a shape of *(Ny,
Nx, 1)*, *(1, Nx, Nz)* or *(Ny, 1, Nz)*. This is the case if
:func:`numpy.meshgrid` was used with a scalar for *z*, *y* or
*x*, respectively (and of course with the default
``indexing='xy'``).
.. note:: If you want to plot a single slice of a pre-computed
"full" 3D sound field, make sure that the slice still
has three dimensions (including one singleton
dimension). This way, you can use the original *grid*
of the full volume without changes.
This works because the grid component corresponding to
the singleton dimension is simply ignored.
grid : triple or pair of numpy.ndarray
The grid that was used to calculate *p*, see
`sfs.util.xyz_grid()`. If *p* is two-dimensional, but
*grid* has 3 components, one of them must be scalar.
xnorm : array_like, optional
Coordinates of a point to which the sound field should be
normalized before plotting. If not specified, no normalization
is used. See `sfs.util.normalize()`.
Returns
-------
AxesImage
See :func:`matplotlib.pyplot.imshow`.
Other Parameters
----------------
xlabel, ylabel : str
Overwrite default x/y labels. Use ``xlabel=''`` and
``ylabel=''`` to remove x/y labels. The labels can be changed
afterwards with :func:`matplotlib.pyplot.xlabel` and
:func:`matplotlib.pyplot.ylabel`.
colorbar : bool, optional
If ``False``, no colorbar is created.
colorbar_kwargs : dict, optional
Further colorbar arguments, see `add_colorbar()`.
ax : Axes, optional
If given, the plot is created on *ax* instead of the current
axis (see :func:`matplotlib.pyplot.gca`).
cmap, vmin, vmax, **kwargs
All further parameters are forwarded to
:func:`matplotlib.pyplot.imshow`.
See Also
--------
sfs.plot.level
"""
p = np.asarray(p)
grid = util.as_xyz_components(grid)
# normalize sound field wrt xnorm
if xnorm is not None:
p = util.normalize(p, grid, xnorm)
if p.ndim == 3:
if p.shape[2] == 1:
p = p[:, :, 0] # first axis: y; second axis: x
plotting_plane = 'xy'
elif p.shape[1] == 1:
p = p[:, 0, :].T # first axis: z; second axis: y
plotting_plane = 'yz'
elif p.shape[0] == 1:
p = p[0, :, :].T # first axis: z; second axis: x
plotting_plane = 'xz'
else:
raise ValueError("If p is 3D, one dimension must have length 1")
elif len(grid) == 3:
if grid[2].ndim == 0:
plotting_plane = 'xy'
elif grid[1].ndim == 0:
plotting_plane = 'xz'
elif grid[0].ndim == 0:
plotting_plane = 'yz'
else:
raise ValueError(
"If p is 2D and grid is 3D, one grid component must be scalar")
else:
# 2-dimensional case
plotting_plane = 'xy'
if plotting_plane == 'xy':
x, y = grid[[0, 1]]
elif plotting_plane == 'xz':
x, y = grid[[0, 2]]
elif plotting_plane == 'yz':
x, y = grid[[1, 2]]
if ax is None:
ax = plt.gca()
im = ax.imshow(np.real(p), cmap=cmap, origin='lower',
extent=[x.min(), x.max(), y.min(), y.max()],
vmax=vmax, vmin=vmin, **kwargs)
if xlabel is None:
xlabel = plotting_plane[0] + ' / m'
if ylabel is None:
ylabel = plotting_plane[1] + ' / m'
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
if colorbar:
add_colorbar(im, **colorbar_kwargs)
return im
def level(p, grid, xnorm=None, power=False, cmap=None, vmax=3, vmin=-50,
**kwargs):
"""Two-dimensional plot of level (dB) of sound field.
Takes the same parameters as `sfs.plot.soundfield()`.
Other Parameters
----------------
power : bool, optional
See `sfs.util.db()`.
"""
# normalize before converting to dB!
if xnorm is not None:
p = util.normalize(p, grid, xnorm)
L = util.db(p, power=power)
return soundfield(L, grid=grid, xnorm=None, cmap=cmap,
vmax=vmax, vmin=vmin, **kwargs)
def particles(x, trim=None, ax=None, xlabel='x (m)', ylabel='y (m)',
edgecolor='', **kwargs):
"""Plot particle positions as scatter plot"""
XX, YY = [np.real(c) for c in x[:2]]
if trim is not None:
xmin, xmax, ymin, ymax = trim
idx = np.where((XX > xmin) & (XX < xmax) & (YY > ymin) & (YY < ymax))
XX = XX[idx]
YY = YY[idx]
if ax is None:
ax = plt.gca()
ax.scatter(XX, YY, edgecolor=edgecolor, **kwargs)
if xlabel:
ax.set_xlabel(xlabel)
if ylabel:
ax.set_ylabel(ylabel)
def vectors(v, grid, cmap='blacktransparent', headlength=3, headaxislength=2.5,
ax=None, clim=None, **kwargs):
"""Plot a vector field in the xy plane.
Parameters
----------
v : triple or pair of array_like
x, y and optionally z components of vector field. The z
components are ignored.
If the values are complex, the imaginary parts are ignored.
grid : triple or pair of array_like
The grid that was used to calculate *v*, see
`sfs.util.xyz_grid()`. Any z components are ignored.
Returns
-------
Quiver
See :func:`matplotlib.pyplot.quiver`.
Other Parameters
----------------
ax : Axes, optional
If given, the plot is created on *ax* instead of the current
axis (see :func:`matplotlib.pyplot.gca`).
clim : pair of float, optional
Limits for the scaling of arrow colors.
See :func:`matplotlib.pyplot.quiver`.
cmap, headlength, headaxislength, **kwargs
All further parameters are forwarded to
:func:`matplotlib.pyplot.quiver`.
"""
v = util.as_xyz_components(v[:2]).apply(np.real)
X, Y = util.as_xyz_components(grid[:2])
speed = np.linalg.norm(v)
with np.errstate(invalid='ignore'):
U, V = v.apply(np.true_divide, speed)
if ax is None:
ax = plt.gca()
if clim is None:
v_ref = 1 / (defs.rho0 * defs.c) # reference particle velocity
clim = 0, 2 * v_ref
return ax.quiver(X, Y, U, V, speed, cmap=cmap, pivot='mid', units='xy',
angles='xy', headlength=headlength,
headaxislength=headaxislength, clim=clim, **kwargs)
def add_colorbar(im, aspect=20, pad=0.5, **kwargs):
"""Add a vertical color bar to a plot.
Parameters
----------
im : ScalarMappable
The output of `sfs.plot.soundfield()`, `sfs.plot.level()` or any
other `matplotlib.cm.ScalarMappable`.
aspect : float, optional
Aspect ratio of the colorbar. Strictly speaking, since the
colorbar is vertical, it's actually the inverse of the aspect
ratio.
pad : float, optional
Space between image plot and colorbar, as a fraction of the
width of the colorbar.
.. note:: The *pad* argument of
:meth:`matplotlib.figure.Figure.colorbar` has a
slightly different meaning ("fraction of original
axes")!
\**kwargs
All further arguments are forwarded to
:meth:`matplotlib.figure.Figure.colorbar`.
See Also
--------
matplotlib.pyplot.colorbar
"""
ax = im.axes
divider = axes_grid1.make_axes_locatable(ax)
width = axes_grid1.axes_size.AxesY(ax, aspect=1/aspect)
pad = axes_grid1.axes_size.Fraction(pad, width)
current_ax = plt.gca()
cax = divider.append_axes("right", size=width, pad=pad)
plt.sca(current_ax)
return ax.figure.colorbar(im, cax=cax, orientation='vertical', **kwargs)