Example #1
0
def plot_sds(freq,
             result,
             ax=None,
             fname=None,
             annotate=False,
             seismic_moment_method=None,
             seismic_moment_options={}):
    """Plot source displacement spectrum and fitted source model"""
    freq = np.array(freq)
    omM = np.array(result['sds'], dtype=np.float)
    if all(np.isnan(omM)):
        return
    fig = None
    obs = ('M0', 'fc', 'n', 'gamma')
    smo = seismic_moment_options

    def _get(k):
        return smo.get(k) or result.get(k)

    smo = {k: _get(k) for k in obs if _get(k) is not None}
    M0 = smo.get('M0')
    fc = smo.get('fc')
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    if seismic_moment_method == 'mean':
        ax.loglog(freq, omM, 'o-', color='gray', mec='gray')
        ax.loglog(freq[freq < fc], omM[freq < fc], 'o-k')
    elif seismic_moment_method in ('fit', 'robust_fit'):
        ax.loglog(freq, omM, 'ok')
        if M0 and fc:
            f = np.linspace(freq[0] / 1.5, freq[-1] * 1.5, 100)
            omM2 = source_model(f, **smo)
            ax.loglog(f, omM2, '-k')
    else:
        ax.loglog(freq, omM, 'o-k')
    if M0:
        ax.axhline(M0, ls='--', color='k')
    labels = OrderedDict(
        (('M0', r'M$_0$=%.1e Nm'), ('fc', r'f$_{\rm{c}}$=%.1f Hz'),
         ('n', 'n=%.1f'), ('gamma', r'$\gamma$=%.2f'), ('fit_error',
                                                        'err=%.2f')))
    labels = [
        labels[key] % np.float32(result[key]) for key in labels
        if key in result
    ]
    if len(labels) > 0 and annotate:
        va = annotate if annotate in ('top', 'bottom') else 'top'
        ypos = 1 if annotate == 'top' else 0
        ax.annotate('\n'.join(labels), (1, ypos), (-5, 5 - 10 * ypos),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va=va,
                    size='x-small')

    if fig and fname:
        _savefig(fig, fname=fname)
Example #2
0
def plot_sds(freq,
             result,
             ax=None,
             annotate=False,
             va='bottom',
             seismic_moment_method=None,
             seismic_moment_options={},
             cmap='viridis_r',
             vmin=None,
             vmax=None,
             max_nobs=None,
             **kwargs):
    """Plot source displacement spectrum and fitted source model"""

    kw = {
        's': 4 * MS**2,
        'marker': 'o',
        'zorder': 10,  #'linewidth': 0.5,
        'c': 'k'
    }
    if 'R' in result and cmap is not None:
        Rvals = np.array(list(result['R'].values()), dtype='float')
        nobs = np.sum(~np.isnan(Rvals), axis=0)
        if max_nobs is None:
            max_nobs = np.max(nobs)
        cmap = plt.get_cmap(cmap, max_nobs)
        if vmax is None:
            vmax = max_nobs + 0.5
        if vmin is None:
            vmin = 0.5
        norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
        kw.update({'c': nobs, 'norm': norm, 'cmap': cmap})
    freq = np.array(freq)
    omM = np.array(result['sds'], dtype=np.float)
    if all(np.isnan(omM)):
        return
    fig = None
    obs = ('M0', 'fc', 'n', 'gamma')
    smo = seismic_moment_options

    def _get(k):
        return smo.get(k) or result.get(k)

    smo = {k: _get(k) for k in obs if _get(k) is not None}
    M0 = smo.get('M0')
    fc = smo.get('fc')
    if ax is None:
        fig = plt.figure()
        ax = fig.add_subplot(111)
    ax.set_xscale('log')
    ax.set_yscale('log')
    if seismic_moment_method == 'mean':
        ax.plot(freq, omM, 'o-', color='gray', mec='gray')
        ax.plot(freq[freq < fc], omM[freq < fc], 'o-k')
        kw['c'] = nobs[freq < fc]
        kw['linewidth'] = 0.5
        sc = ax.scatter(freq[freq < fc], freq[freq < fc], **kw)
    elif seismic_moment_method in ('fit', 'robust_fit'):
        sc = ax.scatter(freq, omM, **kw)
        if M0 and fc:
            f = np.linspace(freq[0] / 1.5, freq[-1] * 1.5, 100)
            omM2 = source_model(f, **smo)
            ax.plot(f, omM2, '-k')
    else:
        kw['linewidth'] = 0.5
        sc = ax.scatter(freq, omM, **kw)

    if M0:
        ax.axhline(M0, ls='--', color='k')
    labels = OrderedDict(
        (('M0', r'M$_0$=%.1e Nm'), ('fc', r'f$_{\rm{c}}$=%.1f Hz'),
         ('n', 'n=%.1f'), ('gamma', r'$\gamma$=%.2f'), ('fit_error',
                                                        'err=%.2f')))
    labels = [
        labels[key] % np.float32(result[key]) for key in labels
        if key in result
    ]
    if len(labels) > 0 and annotate:
        ypos = 1 if va == 'top' else 0
        ax.annotate('\n'.join(labels), (1, ypos), (-5, 5 - 10 * ypos),
                    'axes fraction',
                    'offset points',
                    ha='right',
                    va=va,
                    size='x-small')

    if fig:
        _savefig(fig, **kwargs)
    return sc