# http://pyrocko.org - GPLv3 # # The Pyrocko Developers, 21st Century # ---|P------/S----------~Lg---------- # python 2/3
[0, 1, 0], [1, 0, 0]])
[0, 0, -1], [0, 1, 0]])
return num.sqrt(num.sum(points**2, axis=1))
(num.all(points[1:, :] == points[:-1, :], axis=1), [False]))
if not num.all(points[0, :] == points[-1, :]): points = num.vstack((points, points[0:1, :]))
return points
# assert num.all(points[:, axis] >= 0.0) or num.all(points[:, axis] <= 0.0)
(points2[1:, 0] - points2[:-1, 0]) * (points2[1:, 1] + points2[:-1, 1]))
* (points2[0, 1] + points2[-1, 1])
# cut sub-polygons and gather crossing point information
# get upward crossing points points[1:, axis] > 0.))[0] points[iup, :])
# get downward crossing points points[1:, axis] <= 0.))[0] points[idown+1, axis]) points[idown, :] - points[idown+1, :])
(phidown[i], ipath, idown[i], -1, pdown[i], [1, -1]))
ipath, icuts[i+1], points[icuts[i]+1:icuts[i+1]+1])
points[icuts[-1]+1:], points[:icuts[0]+1]))
else:
# assemble new sub-polygons else:
# separate hemispheres, force polygons closed, remove duplicate points # remove polygons with less than 3 points (4, when counting repeated # endpoint)
else:
len(crossings) == 0 or len(outs_upper) == 0 or len(outs_lower) == 0):
# check if we are cutting between holes outs_upper, key=lambda out: num.min(out[:, axis]))
outs_lower, key=lambda out: num.max(out[:, axis]))
(vp, vn, vt, ep, en, et), (vt, vp, vn, et, ep, en)]): # (vn, vt, vp, en, et, ep)]):
else:
[poly], 2, nonsimple=False, arcres=arcres)
pt_name, patches, patches_lower, patches_upper, lines, lines_lower, lines_upper))
pmean = num.mean(points, axis=0) return points + pmean*0.05
vp, vn, vt = eig[3:] for lab, v in [('P', vp), ('N', vn), ('T', vt)]: sign = num.sign(v[2]) + (v[2] == 0.0) axes.plot(sign*v[1], sign*v[0], 'o', color='black') axes.text(sign*v[1], sign*v[0], ' '+lab)
elif projection == 'orthographic': factor = None else: raise BeachballError( 'invalid argument for projection: %s' % projection)
elif projection == 'orthographic': points_out[:, 2] = num.sqrt(num.maximum(1.0 - rsqr, 0.0)) points_out[:, 1] = points[:, 1] points_out[:, 0] = points[:, 0] else: raise BeachballError( 'invalid argument for projection: %s' % projection)
'Allowed views are top, north, south, east and west'
dc=res[1][2], deviatoric=res[3][2])[mt_type]
axes.transData, axes.figure.dpi_scale_trans, position)
size = 12.
size = 1.0
else: raise BeachballError( 'invalid argument for size_units: %s' % size_units)
mt, beachball_type='deviatoric', position=(0., 0.), size=None, color_t='red', color_p='white', edgecolor='black', linewidth=2, projection='lambert', view='top'):
position = num.asarray(position, dtype=num.float) size = size or 1 mt = deco_part(mt, beachball_type, view)
eig = mt.eigensystem() if eig[0] == 0. and eig[1] == 0. and eig[2] == 0: raise BeachballError('eigenvalues are zero')
data = [] for (group, patches, patches_lower, patches_upper, lines, lines_lower, lines_upper) in eig2gx(eig):
if group == 'P': color = color_p else: color = color_t
for poly in patches_upper: verts = project(poly, projection)[:, ::-1] * size + \ position[NA, :] data.append((verts, color, color, 1.0))
for poly in lines_upper: verts = project(poly, projection)[:, ::-1] * size + \ position[NA, :] data.append((verts, 'none', edgecolor, linewidth)) return data
mt, axes, beachball_type='deviatoric', position=(0., 0.), size=None, zorder=0, color_t='red', color_p='white', edgecolor='black', linewidth=2, alpha=1.0, arcres=181, decimation=1, projection='lambert', size_units='points', view='top'):
''' Plot beachball diagram to a Matplotlib plot
:param mt: :py:class:`pyrocko.moment_tensor.MomentTensor` object or an array or sequence which can be converted into an MT object :param beachball_type: ``'deviatoric'`` (default), ``'full'``, or ``'dc'`` :param position: position of the beachball in data coordinates :param size: diameter of the beachball either in points or in data coordinates, depending on the ``size_units`` setting :param zorder: (passed through to matplotlib drawing functions) :param color_t: color for compressional quadrants (default: ``'red'``) :param color_p: color for extensive quadrants (default: ``'white'``) :param edgecolor: color for lines (default: ``'black'``) :param linewidth: linewidth in points (default: ``2``) :param alpha: (passed through to matplotlib drawing functions) :param projection: ``'lambert'`` (default), ``'stereographic'``, or ``'orthographic'`` :param size_units: ``'points'`` (default) or ``'data'``, where the latter causes the beachball to be projected in the plots data coordinates (axes must have an aspect ratio of 1.0 or the beachball will be shown distorted when using this). :param view: View the beachball from ``top``, ``north``, ``south``, ``east`` or ``west``. Useful for to show beachballs in cross-sections. Default is ``top``. '''
axes, size_units, position, size)
raise BeachballError('eigenvalues are zero')
lines, lines_lower, lines_upper) in eig2gx(eig, arcres):
else:
# plot "upper" features for lower hemisphere, because coordinate system # is NED
(Path(verts[::decimation]), color, color, linewidth)) else: (Path(verts[::decimation]), color, 'none', 0.0))
(Path(verts[::decimation]), 'none', edgecolor, linewidth))
paths, facecolors=facecolors, edgecolors=edgecolors, linewidths=linewidths, alpha=alpha, zorder=zorder, transform=transform)
view='top'):
en * num.cos(aphi)**2 + et * num.sin(aphi)**2) * num.sin(atheta)**2
mts, axes, best_mt=None, beachball_type='deviatoric', position=(0., 0.), size=None, zorder=0, color_t='red', color_p='white', edgecolor='black', best_color='red', linewidth=2, alpha=1.0, projection='lambert', size_units='data', grid_resolution=200, method='imshow', view='top'): ''' Plot fuzzy beachball from a list of given MomentTensors
:param mts: list of :py:class:`pyrocko.moment_tensor.MomentTensor` object or an array or sequence which can be converted into an MT object :param best_mt: :py:class:`pyrocko.moment_tensor.MomentTensor` object or an array or sequence which can be converted into an MT object of most likely or minimum misfit solution to extra highlight :param best_color: mpl color for best MomentTensor edges, polygons are not plotted
See plot_beachball_mpl for other arguments ''' raise BeachballError( 'size_units="points" not supported in ' 'plot_fuzzy_beachball_mpl_pixmap')
axes, size_units, position, size)
mts, grid_resolution=grid_resolution, projection=projection, beachball_type=beachball_type, mask=True, view=view)
'dummy', [color_p, color_t], N=ncolors)
axes.contourf( position[0] + y * size, position[1] + x * size, amps.T, levels=levels, cmap=cmap, transform=transform, zorder=zorder, alpha=alpha)
amps.T, extent=( position[0] + y[0] * size, position[0] + y[-1] * size, position[1] - x[0] * size, position[1] - x[-1] * size), cmap=cmap, transform=transform, zorder=zorder-0.1, alpha=alpha) else: assert False, 'invalid `method` argument'
# draw optimum edges [best_mt], grid_resolution=grid_resolution, projection=projection, beachball_type=beachball_type, mask=False)
position[0] + by * size, position[1] + bx * size, best_amps.T, levels=[0.], colors=[best_color], linewidths=linewidth, transform=transform, zorder=zorder, alpha=alpha)
position[0] + x * size, position[1] + y * size, linewidth=linewidth, color=edgecolor, transform=transform, zorder=zorder, alpha=alpha)
mt, axes, show='patches', beachball_type='deviatoric', view='top'):
mt = deco_part(mt, beachball_type, view) eig = mt.eigensystem()
for (group, patches, patches_lower, patches_upper, lines, lines_lower, lines_upper) in eig2gx(eig):
if group == 'P': color = 'blue' lw = 1 else: color = 'red' lw = 1
if show == 'patches': for poly in patches_upper: px, py, pz = poly.T axes.plot(*extr(poly).T, color=color, lw=lw, alpha=0.5)
if show == 'lines': for poly in lines_upper: px, py, pz = poly.T axes.plot(*extr(poly).T, color=color, lw=lw, alpha=0.5)
mt, axes, beachball_type='deviatoric', position=(0., 0.), size=None, zorder=0, color_t='red', color_p='white', edgecolor='black', linewidth=2, alpha=1.0, projection='lambert', size_units='data', view='top'):
raise BeachballError( 'size_units="points" not supported in plot_beachball_mpl_pixmap')
axes, size_units, position, size)
[mt], projection, beachball_type, grid_resolution=200, mask=False)
position[0] + y * size, position[1] + x * size, amps.T, levels=[-num.inf, 0., num.inf], colors=[color_p, color_t], transform=transform, zorder=zorder, alpha=alpha)
position[0] + y * size, position[1] + x * size, amps.T, levels=[0.], colors=[edgecolor], linewidths=linewidth, transform=transform, zorder=zorder, alpha=alpha)
position[0] + x * size, position[1] + y * size, linewidth=linewidth, color=edgecolor, transform=transform, zorder=zorder, alpha=alpha)
if __name__ == '__main__': import sys import os import matplotlib.pyplot as plt from pyrocko import model
args = sys.argv[1:]
data = [] for iarg, arg in enumerate(args):
if os.path.exists(arg): events = model.load_events(arg) for ev in events: if not ev.moment_tensor: logger.warn('no moment tensor given for event') continue
data.append((ev.name, ev.moment_tensor)) else: vals = list(map(float, arg.split(','))) mt = mtm.as_mt(vals) data.append(('%i' % (iarg+1), mt))
n = len(data)
ncols = 1 while ncols**2 < n: ncols += 1
nrows = ncols
fig = plt.figure() axes = fig.add_subplot(1, 1, 1, aspect=1.) axes.axison = False axes.set_xlim(-0.05 - ncols, ncols + 0.05) axes.set_ylim(-0.05 - nrows, nrows + 0.05)
for ibeach, (name, mt) in enumerate(data): irow = ibeach // ncols icol = ibeach % ncols plot_beachball_mpl( mt, axes, position=(icol*2-ncols+1, -irow*2+nrows-1), size_units='data')
axes.annotate( name, xy=(icol*2-ncols+1, -irow*2+nrows-2), xycoords='data', xytext=(0, 0), textcoords='offset points', verticalalignment='center', horizontalalignment='center', rotation=0.)
plt.show() |