Exemple #1
0
def plot_mfc_budget(mfc_budget, index, year, legend=True,
                    legend_kw={'fontsize' : 9, 'loc' : 'upper left',
                               'handlelength' : 2.5},
                    dashes=[6, 2], netprecip=False):
    ts = mfc_budget.sel(year=year)
    ind = index.sel(year=year)
    days = ts['day'].values
    styles = {'PRECTOT' : {'color' : 'k', 'linestyle' : '--', 'dashes' : dashes},
              'EVAP' : {'color' : 'k'},
              'MFC' : {'color' : 'k', 'linewidth' : 2},
              'dw/dt' : {'color' : '0.7', 'linewidth' : 2}}
    if netprecip:
        styles['P-E'] = {'color' : 'b', 'linewidth' : 2}
    for nm in styles:
        plt.plot(days, ts[nm], label=nm, **styles[nm])
    plt.axvline(ind['onset'], color='k')
    plt.axvline(ind['retreat'], color='k')
    plt.xlabel('Day of Year')
    plt.ylabel('mm/day')
    ax1 = plt.gca()
    ax2 = plt.twinx()
    plt.sca(ax2)
    plt.plot(days, ind['tseries'], 'r', alpha=0.6, linewidth=2, label='CMFC')
    atm.fmt_axlabels('y', 'mm', color='r', alpha=0.6)
    if legend:
        atm.legend_2ax(ax1, ax2, **legend_kw)
    return ax1, ax2
def lineplot(sectors, ax1=None, y1_label='', y2_label='', title='',
             latmin=None, latmax=None,
             legend_opts = {'fontsize' : 9, 'loc' : 'lower center',
                            'handlelength' : 2.5, 'frameon' : False},
             ax2_color='r', ax2_alpha=0.5, row=1, nrow=1, y1_lims=None):
    if ax1 is None:
        ax1 = plt.gca()

    ax1_fmts = [{'color' : 'k', 'linestyle' : 'dashed'}, {'color' : 'k'},
                {'color' : 'k', 'linewidth' : 1.5}]
    ax2_fmts = [{'linewidth' : 2, 'alpha' : ax2_alpha, 'color' : ax2_color}]
    lat = atm.get_coord(sectors, 'lat')
    ax1.set_title(title)
    if row < nrow - 1:
        ax1.set_xticklabels([])
    else:
        ax1.set_xlabel('Latitude')
    if y1_lims is not None:
        ax1.set_ylim(y1_lims)
    ax1.set_ylabel(y1_label)
    ax1.grid(True)
    i1, i2 = 0, 0
    for key in sectors.data_vars:
        var = sectors[key]
        if var.attrs['axis'] == 1:
            ax, fmts = ax1, ax1_fmts[i1]
            i1 += 1
        else:
            if i2 == 0:
                ax2 = ax1.twinx()
            ax, fmts = ax2, ax2_fmts[i2]
            i2 += 1
        ax.plot(lat, var, label=key, **fmts)

    if latmin is not None:
        ax1.set_xlim(latmin, latmax)

    if legend_opts is not None:
        legend_opts['ncol'] = i1 + i2
        if i2 > 0:
            atm.legend_2ax(ax1, ax2, **legend_opts)
        else:
            ax1.legend(**legend_opts)
    if i2 > 0:
        # ax2.set_ylabel(y2_label, color=ax2_color, alpha=ax2_alpha)
        for t1 in ax2.get_yticklabels():
            t1.set_color(ax2_color)

    plt.draw()
    return None
Exemple #3
0
def plotyy(data1, data2=None, xname='dayrel', data1_styles=None,
           y2_opts={'color' : 'r', 'alpha' : 0.6, 'linewidth' : 2},
           xlims=None, xticks=None, ylims=None, yticks=None, y2_lims=None,
           xlabel='', y1_label='', y2_label='', legend=False,
           legend_kw={'fontsize' : 9, 'handlelength' : 2.5},
           x0_axvlines=None, grid=True):
    """Plot data1 and data2 together on different y-axes."""

    data1, data2 = atm.to_dataset(data1), atm.to_dataset(data2)

    for nm in data1.data_vars:
        if data1_styles is None:
            plt.plot(data1[xname], data1[nm], label=nm)
        elif isinstance(data1_styles[nm], dict):
            plt.plot(data1[xname], data1[nm], label=nm, **data1_styles[nm])
        else:
            plt.plot(data1[xname], data1[nm], data1_styles[nm], label=nm)
    atm.ax_lims_ticks(xlims, xticks, ylims, yticks)
    plt.grid(grid)
    if x0_axvlines is not None:
        for x0 in x0_axvlines:
            plt.axvline(x0, color='k')
    plt.xlabel(xlabel)
    plt.ylabel(y1_label)
    axes = [plt.gca()]

    if data2 is not None:
        plt.sca(plt.gca().twinx())
        for nm in data2.data_vars:
            plt.plot(data2[xname], data2[nm], label=nm, **y2_opts)
        if y2_lims is not None:
            plt.ylim(y2_lims)
        if 'linewidth' in y2_opts:
            y2_opts.pop('linewidth')
        atm.fmt_axlabels('y', y2_label, **y2_opts)
        atm.ax_lims_ticks(xlims, xticks)
    axes = axes + [plt.gca()]

    if legend:
        if data2 is None:
            plt.legend(**legend_kw)
        else:
            atm.legend_2ax(axes[0], axes[1], **legend_kw)

    return axes
def lineplots(data1, data2=None, data1_style=None, xlims=None, xticks=None,
              ylims=None, yticks=None, length=None, legend=False,
              legend_kw={'fontsize' : 9, 'handlelength' : 2.5},
              y2_lims=None, y2_opts={'color' : 'r', 'alpha' : 0.6},
              y1_label='', y2_label='', grp=None):

    data1, data2 = to_dataset(data1), to_dataset(data2)

    for nm in data1.data_vars:
        if data1_style is None:
            plt.plot(data1['dayrel'], data1[nm], label=nm)
        else:
            plt.plot(data1['dayrel'], data1[nm], data1_style[nm], label=nm)
    fmt_axes(xlims, xticks, ylims, yticks)
    plt.grid(True)
    plt.axvline(0, color='k')
    if length is not None:
        plt.axvline(length, color='k')
    if grp is not None and grp.row == grp.ncol - 1:
        plt.xlabel('Rel Day')
    plt.ylabel(y1_label)
    axes = [plt.gca()]

    if data2 is not None:
        plt.sca(plt.gca().twinx())
        for nm in data2.data_vars:
            plt.plot(data2['dayrel'], data2[nm], label=nm, linewidth=2,
                     **y2_opts)
        if y2_lims is not None:
            plt.ylim(y2_lims)
        atm.fmt_axlabels('y', y2_label, **y2_opts)
    axes = axes + [plt.gca()]

    if legend:
        if data2 is None:
            plt.legend(**legend_kw)
        else:
            atm.legend_2ax(axes[0], axes[1], **legend_kw)

    return axes