Beispiel #1
0
def mon_boxplot(ts,
                col='all',
                dtype='flow',
                fun='mean',
                min_yrs=10,
                export_path='',
                export_name='flow_boxplot.png'):
    """

    """
    from os import path
    from calendar import month_abbr
    import seaborn as sns
    import matplotlib.pyplot as plt
    from core.ts import w_resample
    from core.ts.sw import flow_stats
    from pandas import Series

    ylabel_dict = {
        'flow': 'Mean Flow $(m^{3}/s)$',
        'precip': 'Precipitation $(mm/month)$',
        'PET': 'Potential Evapotranspiration $(mm/month)$',
        'diff': 'Precipitation - PET $(mm/month)$'
    }
    sns.set_style("whitegrid")
    sns.set_context('poster')
    col_dict = {
        'flow': sns.color_palette("pastel")[0],
        'precip': sns.color_palette("pastel")[0],
        'PET': sns.color_palette("pastel")[1],
        'diff': sns.light_palette("blue")[0]
    }
    color = col_dict[dtype]

    if type(ts) is Series:
        ts2 = ts
    else:
        stats1 = flow_stats(ts)
        index1 = stats1.loc['Tot data yrs', :] >= min_yrs
        sites = ts.columns[index1]
        ts2 = ts[sites]
    tsm = w_resample(ts2, period='month', fun=fun)
    tsm['month_num'] = tsm.index.month
    tsm['month'] = tsm['month_num'].apply(lambda x: month_abbr[x])

    if type(ts) is Series:
        fig, ax = plt.subplots(figsize=(15, 10))
        sns.boxplot(x='month', y=0, data=tsm, color=color, showfliers=False)
        plt.ylabel(ylabel_dict[dtype])
        plt.xlabel('Month')
        plt.tight_layout()
        plot2 = ax.get_figure()
        plot2.savefig(path.join(export_path, export_name))
    elif col is 'all':
        for i in sites:
            fig, ax = plt.subplots(figsize=(15, 10))
            sns.boxplot(x='month',
                        y=i,
                        data=tsm,
                        color=color,
                        showfliers=False)
            plt.ylabel(ylabel_dict[dtype])
            plt.xlabel('Month')
            plt.tight_layout()
            plot2 = ax.get_figure()
            plot2.savefig(path.join(export_path, str(i) + '_' + export_name))
    elif type(col) is list:
        for i in col:
            fig, ax = plt.subplots(figsize=(15, 10))
            sns.boxplot(x='month', y=i, color=color, data=tsm)
            plt.ylabel(ylabel_dict[dtype])
            plt.xlabel('Month')
            plt.tight_layout()
            plot2 = ax.get_figure()
            plot2.savefig(path.join(export_path, str(i) + '_' + export_name))
Beispiel #2
0
#### Plot
data = concat([g_flow[69650], r_flow], axis=1).dropna()
data.columns = data.columns.astype('str')
sns.regplot(x=log(data[r_sites]), y=log(data[g_sites]))
lm = sns.regplot(x='69618', y='69650', data=data, truncate=True)
lm.axes.set_ylim(0, 20)
lm.axes.set_xlim(0, 20)

###############################
#### Export results

predam1 = new_ts.loc[new_ts[69650].first_valid_index():, 69650]
predam2 = predam1.copy()
predam2[predam2 > (reg1.loc[69650].max_y * 1.5)] = nan

stats1 = flow_stats(predam1)
stats2 = flow_stats(predam2)

malf1 = malf7d(predam1, intervals=[10, 20, 30])

stats_both = concat(
    [concat([stats1, malf1], axis=1),
     concat([stats2, malf1], axis=1)])
stats_both.index = ['all_flows', 'constrained_flows']

stats_both.to_csv(stats_export2, header=True)
predam1.to_csv(ts_export2, header=True)

#plt1 = hydrograph_plot(predam1)
plt2 = reg_plot(r_flow[69618],
                g_flow[69650],
Beispiel #3
0
def dual_mon_boxplot(ts1,
                     ts2,
                     col='all',
                     dtype='flow',
                     fun='mean',
                     min_yrs=10,
                     export_path='',
                     export_name='flow_dual_boxplot.png'):
    """

    """
    from calendar import month_abbr
    from os import path
    from pandas import concat, melt, DatetimeIndex
    import seaborn as sns
    import matplotlib.pyplot as plt
    from core.ts import w_resample
    from core.ts.sw import flow_stats

    ### Set base parameters
    ylabel_dict = {
        'flow': 'Mean Flow $(m^{3}/s)$',
        'precip': 'Precipitation $(mm/month)$',
        'ET': 'Evapotranspiration $(mm/month)$'
    }
    sns.set_style("whitegrid")
    sns.set_context('poster')
    cat = ['Recorded', 'Naturalised']

    ### Process data
    ts1 = ts1.dropna(axis=1, how='all')
    stats1 = flow_stats(ts1)
    index1 = stats1.loc[:, 'Tot data yrs'] >= min_yrs
    sites = ts1.columns[index1.values]
    ts1_sel = ts1[sites]
    ts2_sel = ts2[sites]
    ts1_selm = w_resample(ts1_sel, period='month', fun=fun)
    ts2_selm = w_resample(ts2_sel, period='month', fun=fun)
    ts_all = concat([ts1_selm, ts2_selm], axis=1, join='inner')

    ### Make the figures
    if col is 'all':
        for i in sites:
            ts_site = ts_all[i]
            ts_site.columns = cat
            ts_site2 = melt(ts_site.reset_index(),
                            id_vars='Time',
                            value_vars=cat,
                            var_name='type')
            ts_site2['month_num'] = DatetimeIndex(ts_site2.Time).month
            ts_site2['month'] = ts_site2['month_num'].apply(
                lambda x: month_abbr[x])
            ts_site2 = ts_site2.sort_values(['month_num', 'type'],
                                            ascending=[True, False],
                                            axis='index')
            fig, ax = plt.subplots(figsize=(15, 10))
            sns.boxplot(x='month',
                        y='value',
                        hue='type',
                        data=ts_site2,
                        showfliers=False)
            plt.ylabel(ylabel_dict[dtype])
            plt.xlabel('Month')
            plt.legend()
            plt.tight_layout()
            plot2 = ax.get_figure()
            plot2.savefig(path.join(export_path, str(i) + '_' + export_name))
    elif type(col) is list:
        for i in col:
            ts_site = ts_all[i]
            ts_site.columns = cat
            ts_site2 = melt(ts_site.reset_index(),
                            id_vars='Time',
                            value_vars=cat,
                            var_name='type')
            ts_site2['month_num'] = DatetimeIndex(ts_site2.Time).month
            ts_site2['month'] = ts_site2['month_num'].apply(
                lambda x: month_abbr[x])
            ts_site2 = ts_site2.sort_values(['month_num', 'type'],
                                            ascending=[True, False],
                                            axis='index')
            fig, ax = plt.subplots(figsize=(15, 10))
            sns.boxplot(x='month',
                        y='value',
                        hue='type',
                        data=ts_site2,
                        showfliers=False)
            plt.ylabel(ylabel_dict[dtype])
            plt.xlabel('Month')
            plt.legend()
            plt.tight_layout()
            plot2 = ax.get_figure()
            plot2.savefig(path.join(export_path, str(i) + '_' + export_name))