Ejemplo n.º 1
0
def plot_lapaz_elevation(PROJ, ax4, bar_legend, grid_style, lala_lp, levels,
                         lolo_lp, ncmap, tab10, topo_ds, white_grid_color, xs1,
                         xs2, ys1, ys2):
    ax = ax4
    fa.get_ax_lapaz(ax=ax,
                    chc_lp_legend=False,
                    plot_cities=False,
                    lalo_extent=[xs1, xs2, ys1, ys2],
                    lola_ticks=[lolo_lp, lala_lp],
                    draw_labels=False,
                    grid_alpha=1,
                    grid_color=white_grid_color,
                    grid_style=grid_style,
                    lake_face_color=crt.feature.COLORS['water'])
    ax.set_xticks([-69, -68, -67], crs=PROJ)
    slice_ = {
        'lat': slice(
            ys1,
            ys2,
        ),
        'lon': slice(
            xs1,
            xs2,
        )
    }
    band_ = topo_ds['Band1'].loc[slice_] / 1000
    band_.name = bar_legend
    mappable = band_.plot(
        cmap=ncmap,
        ax=ax,
        levels=levels,
        subplot_kws={'transform': PROJ},
        # center=2000,
        norm=Normalize(0, 5),
        add_colorbar=False,
        rasterized=True)
    ax.set_xlabel(None)
    ax.set_ylabel(None)
    ax.scatter([co.CHC_LON], [co.CHC_LAT],
               facecolors='none',
               edgecolors=tab10(0))
    ax.scatter([co.LPB_LON], [co.LPB_LAT],
               facecolors='none',
               edgecolors=tab10(1))
    return mappable
Ejemplo n.º 2
0
def plot_general_lapaz(ds1):
    cl = co.CPer
    c1 = ds1[cl].sum([co.RL, co.ZM])
    c2 = ds1[cl].sum([co.ZM, co.RL, co.TH_CENTER])
    ar = c1 / c2
    # ar = c1
    ar = ar.isel(**{co.R_CENTER: slice(0, -3)})
    ax = fa.get_ax_lapaz(fig_args={'figsize': (5, 5)})
    fa.logpolar_plot(ar, name=co.CPer, ax=ax, perM=.95, perm=.01)
    # ax.set_xlim(-75, -60)
    # ax.set_ylim(-25, -7)
    return ax
Ejemplo n.º 3
0
def plot_clust_in_lapaz(_n, dscc):
    for _f in range(_n):
        ax = fa.get_ax_lapaz()
        ax.set_title(str(_f))
        _ds = dscc.loc[{co.CLUS_LENGTH_DIM: _n}][
            {co.R_CENTER: slice(1, 23)}]
        _ds = _ds.drop(co.KMEAN_OBJ)
        _ds = _ds.where(_ds[co.FLAG] == _f)
        _ds = _ds.sum([co.RL, co.ZM])

        _ds = _ds[[co.CONC]]

        _cm = fa.get_custom_cmap([*ucp.cc, *ucp.cc, *ucp.cc][_f][:3])

        if _ds[co.CONC].max().item() != 0:
            fa.logpolar_plot(_ds, ax=ax, patch_args={'cmap': _cm},
                             colorbar=False)
        fa.add_chc_lpb(ax)
    boo = self.merged_ds[co.ClusFlag] == clus
    #     fig,ax = plt.subplots()
    import warnings
    warnings.simplefilter('ignore')
    ar = self.merged_ds.where(boo)[co.CPer].mean(dim=[co.RL])
    fa.logpolar_plot(ar,
                     ax,
                     name=co.CPer,
                     perM=.15,
                     quantile=False,
                     colorbar=False,
                     patch_args={'cmap': fa.get_custom_cmap(FLP.COLORS[i])})

# %%
i = 0
ax = fa.get_ax_lapaz()
for i in range(len_clus):
    clus = self.cluster_flags[i]
    boo = self.merged_ds[co.ClusFlag] == clus
    #     fig,ax = plt.subplots()
    import warnings
    warnings.simplefilter('ignore')
    ar = self.merged_ds.where(boo)[co.CCPer].mean(dim=[co.RL])
    fa.logpolar_plot(ar,
                     ax,
                     name=co.CCPer,
                     perM=.05,
                     quantile=False,
                     colorbar=False,
                     patch_args={'cmap': fa.get_custom_cmap(FLP.COLORS[i])})
    fa.add_chc_lpb(ax)
Ejemplo n.º 5
0
def plot_lapaz_domains(PROJ, ax3, dark_grid_color, grid_style, lala_lp,
                       lolo_lp, tab10, xs1, xs2, ys1, ys2):
    ax = ax3
    fa.get_ax_lapaz(ax=ax,
                    chc_lp_legend=False,
                    plot_cities=False,
                    lalo_extent=[xs1, xs2, ys1, ys2],
                    lola_ticks=[lolo_lp, lala_lp],
                    draw_labels=False,
                    grid_style=grid_style,
                    grid_color=dark_grid_color,
                    grid_alpha=1)
    # noinspection PyArgumentList
    ax.set_xticks([-69, -68, -67], crs=PROJ)
    ax.set_xlabel(r'longitude [$\degree$]')
    # noinspection PyArgumentList
    ax.set_yticks([-16, -17], crs=PROJ)
    ax: plt.Axes
    ax.set_ylabel(r'latitude [$\degree$]')
    # ax.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20)
    # ax.annotate('latitude',
    #             (0, 1),
    #             (-2, -2),
    #             xycoords='axes fraction',
    #             textcoords='offset points',
    #             horizontalalignment='right',
    #             verticalalignment='top',
    #             # color=ucp.cc[3]
    #             )

    x1, x2, y1, y2 = -69, -67.3, -17.2, -15.6
    map_add_rectangle(ax, x1, x2, y1, y2, color=ucp.cc[3])
    # noinspection PyProtectedMember
    ax.annotate('D04', (x1, y1), (0, -2),
                xycoords=PROJ._as_mpl_transform(ax),
                textcoords='offset points',
                horizontalalignment='left',
                verticalalignment='top',
                color=ucp.cc[3])
    ax.scatter([co.CHC_LON], [co.CHC_LAT], facecolor=tab10(0))
    ax.scatter([co.LPB_LON], [co.LPB_LAT], facecolor=tab10(1))
    # noinspection PyProtectedMember
    ax.annotate(
        'CHC',
        [co.CHC_LON, co.CHC_LAT],
        xytext=[1, 2],
        xycoords=PROJ._as_mpl_transform(ax),
        textcoords='offset points',
        horizontalalignment='right',
        verticalalignment='bottom',
    )
    # noinspection PyProtectedMember
    ax.annotate(
        'LPB',
        [co.LPB_LON, co.LPB_LAT],
        xytext=[0, -3],
        xycoords=PROJ._as_mpl_transform(ax),
        textcoords='offset points',
        horizontalalignment='right',
        verticalalignment='top',
    )
    ax: plt.Axes
    # noinspection PyProtectedMember
    ax.annotate('Lake TCC', [-69, -16],
                xytext=[2, 4],
                xycoords=PROJ._as_mpl_transform(ax),
                textcoords='offset points',
                horizontalalignment='left',
                verticalalignment='bottom',
                color=crt.feature.COLORS['water'])
Ejemplo n.º 6
0
def plot_pathways(geo_df,
                  lon_range=15,
                  lat_range=13,
                  centroid_to_plot=None,
                  chc_lp_legend=False,
                  ax_bolivia=True,
                  ax=None,
                  min_font_size=5,
                  labs_to_plot='all',
                  background_alpha=0):
    npdf = geo_df
    if centroid_to_plot is None:
        centroid_to_plot = ['LR', 'MR']
    if ax is None:
        ucp.set_dpi(300)
        f_width = 7.25
        figsize = (f_width, f_width / 1.6)
        _, ax = plt.subplots(subplot_kw=dict(projection=crt.crs.PlateCarree()),
                             figsize=figsize)
    ax: plt.Axes
    if ax_bolivia:
        fa.get_ax_bolivia(
            ax=ax,
            plot_cities=False,
            map_res='50m',
            lake_face_color='none',
            map_line_alpha=.5,
            chc_lp_legend=chc_lp_legend,
            grid_alpha=0,
            draw_labels=False,
            xlab_top=False,
            xlab_bot=False,
            ylab_left=False,
            ylab_right=False,
        )
    else:
        fa.get_ax_lapaz(ax=ax,
                        plot_cities=False,
                        lake_face_color='none',
                        map_line_alpha=.5,
                        chc_lp_legend=chc_lp_legend,
                        grid_alpha=0,
                        draw_labels=False,
                        y_left_lab=False)

    npdf['text_xy'] = [[[0, 0]]] * len(npdf)
    npdf.loc['07_LR', 'text_xy'] = [[[-5, -12]]]
    npdf.loc['08_LR', 'text_xy'] = [[[-5, 7]]]
    npdf.loc['10_LR', 'text_xy'] = [[[8, 0]]]
    npdf.loc['11_MR', 'text_xy'] = [[[-13, 7]]]
    npdf.loc['02_MR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['04_MR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['05_MR', 'text_xy'] = [[[5, 0]]]
    npdf.loc['09_MR', 'text_xy'] = [[[-25, -8]]]
    npdf.loc['08_SM', 'text_xy'] = [[[-25, -10]]]
    npdf.loc['12_SM', 'text_xy'] = [[[-3, 7]]]
    npdf.loc['03_SM', 'text_xy'] = [[[5, -4]]]
    npdf.loc['06_SM', 'text_xy'] = [[[-7, -10]]]
    npdf.loc['11_SR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['02_SR', 'text_xy'] = [[[-3, 7]]]
    npdf.loc['12_SR', 'text_xy'] = [[[-12, 7]]]
    npdf.loc['04_SR', 'text_xy'] = [[[4, -10]]]
    npdf.loc['07_SR', 'text_xy'] = [[[-17, -12]]]
    npdf.loc['10_SR', 'text_xy'] = [[[-10, 5]]]
    lrpdf = npdf[npdf['range'].isin(centroid_to_plot)]
    for l, r in lrpdf.iterrows():
        ax.scatter(r['lon'],
                   r['lat'],
                   s=40,
                   c=[r['range_color']],
                   marker=r['range_marker'],
                   zorder=20,
                   edgecolors=['k'],
                   linewidth=1)

    if labs_to_plot == 'all':
        lab_lrpdf = lrpdf
    else:
        lab_lrpdf = npdf[npdf['range'].isin(labs_to_plot)]

    for l, r in lab_lrpdf.iterrows():
        ax.annotate(
            r.name,
            xy=r[['lon', 'lat']],
            xytext=r['text_xy'][0],
            textcoords='offset points',
            zorder=12,
            backgroundcolor=[1, 1, 1, background_alpha],
            # bbox=dict(boxstyle="round",
            #           alpha=0,
            #           linewidth=0,
            #           facecolor='w'
            #           ),
            alpha=.5,
            fontsize=min_font_size,
            arrowprops=dict(arrowstyle='-', alpha=.5))
    # ax.set_ylim(-15,15LR)
    import cartopy.mpl.geoaxes
    ax: crt.mpl.geoaxes.GeoAxesSubplot
    _xx, _yy = lon_range, lat_range
    extent = get_extent(_xx, _yy)
    ax.set_extent(extent, crt.crs.PlateCarree())

    # plt.show()

    return ax
def plot_pathways(npdf,
                  dac_sum,
                  conc_name,
                  lon_range=15,
                  lat_range=13,
                  centroid_to_plot=None,
                  chc_lp_legend=False,
                  q_level=.8,
                  ax_bolivia=True,
                  ax=None,
                  min_fs=5,
                  add_patch=True,
                  labs_to_plot='all',
                  background_alpha=0):
    if centroid_to_plot is None:
        centroid_to_plot = ['LR', 'MR']
    if ax is None:
        ucp.set_dpi(300)
        f_width = 7.25
        figsize = (f_width, f_width / 1.6)
        _, ax = plt.subplots(subplot_kw=dict(projection=crt.crs.PlateCarree()),
                             figsize=figsize)
    ax: plt.Axes
    if ax_bolivia:
        fa.get_ax_bolivia(
            ax=ax,
            plot_cities=False,
            map_res='50m',
            lake_face_color='none',
            map_line_alpha=.5,
            chc_lp_legend=chc_lp_legend,
            grid_alpha=0,
            draw_labels=False,
        )
    else:
        fa.get_ax_lapaz(ax=ax,
                        plot_cities=False,
                        lake_face_color='none',
                        map_line_alpha=.5,
                        chc_lp_legend=chc_lp_legend,
                        grid_alpha=0,
                        draw_labels=False,
                        y_left_lab=False)
    # gpdf = npdf.groupby('range')
    for l, r in npdf.iterrows():
        _q = q_level
        l = r['cluster_i']

        res = get_res_quan(dac_sum, l, _q, conc_name=conc_name)

        pols = res.apply(lambda r: sh_polygon_from_row(r), axis=1)
        u = cascaded_union(pols)
        patch = PolygonPatch(u,
                             alpha=.7,
                             facecolor=r['c6_colors'],
                             edgecolor=r['c6_colors'],
                             linewidth=1,
                             zorder=11)
        if add_patch:
            ax.add_patch(patch)
    npdf['text_xy'] = [[[0, 0]]] * len(npdf)
    npdf.loc['07_LR', 'text_xy'] = [[[-5, -12]]]
    npdf.loc['08_LR', 'text_xy'] = [[[-5, 7]]]
    npdf.loc['10_LR', 'text_xy'] = [[[8, 0]]]
    npdf.loc['11_MR', 'text_xy'] = [[[-13, 7]]]
    npdf.loc['02_MR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['04_MR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['05_MR', 'text_xy'] = [[[5, -10]]]
    npdf.loc['09_MR', 'text_xy'] = [[[-25, -8]]]
    npdf.loc['08_SM', 'text_xy'] = [[[-23, -15]]]
    npdf.loc['12_SM', 'text_xy'] = [[[-3, 7]]]
    npdf.loc['03_SM', 'text_xy'] = [[[5, -4]]]
    npdf.loc['06_SM', 'text_xy'] = [[[-7, -10]]]
    npdf.loc['11_SR', 'text_xy'] = [[[-10, 7]]]
    npdf.loc['02_SR', 'text_xy'] = [[[-3, 7]]]
    npdf.loc['12_SR', 'text_xy'] = [[[-12, 7]]]
    npdf.loc['04_SR', 'text_xy'] = [[[4, -10]]]
    npdf.loc['07_SR', 'text_xy'] = [[[-17, -12]]]
    npdf.loc['10_SR', 'text_xy'] = [[[-30, 0]]]
    _x = npdf[co.R_CENTER] * np.sin(npdf[co.TH_CENTER])
    _y = npdf[co.R_CENTER] * np.cos(npdf[co.TH_CENTER])
    npdf['lat'] = _y + co.CHC_LAT
    npdf['lon'] = _x + co.CHC_LON
    lrpdf = npdf[npdf['range'].isin(centroid_to_plot)]
    for l, r in lrpdf.iterrows():
        ax.scatter(r['lon'],
                   r['lat'],
                   s=40,
                   c=[r['range_color']],
                   marker=r['range_marker'],
                   zorder=20,
                   edgecolors=['k'],
                   linewidth=1)

    if labs_to_plot == 'all':
        lab_lrpdf = lrpdf
    else:
        lab_lrpdf = npdf[npdf['range'].isin(labs_to_plot)]

    for l, r in lab_lrpdf.iterrows():
        ax.annotate(
            r.name,
            xy=r[['lon', 'lat']],
            xytext=r['text_xy'][0],
            textcoords='offset points',
            zorder=12,
            backgroundcolor=[1, 1, 1, background_alpha],
            # bbox=dict(boxstyle="round",
            #           alpha=0,
            #           linewidth=0,
            #           facecolor='w'
            #           ),
            alpha=.5,
            fontsize=min_fs,
            arrowprops=dict(arrowstyle='-', alpha=.5))
    # ax.set_ylim(-15,15LR)
    import cartopy.mpl.geoaxes
    ax: crt.mpl.geoaxes.GeoAxesSubplot
    _xx, _yy = lon_range, lat_range
    extent = get_extent(_xx, _yy)
    ax.set_extent(extent, crt.crs.PlateCarree())

    # plt.show()

    return ax
Ejemplo n.º 8
0
def clus_plot(
        i_lab, ds: xr.Dataset,
        conc_lab=None,
        ds_lab=None,
        figure_size=15,
        dpi=400,
        fig_title: str = None,
        conc_lab_ts=None,
        df_prop=None
):
    if conc_lab is None:
        conc_lab = ds.attrs['chosen_smooth']
    if conc_lab_ts is None:
        conc_lab_ts = conc_lab

    r_dis_km: xr.DataArray = ds[co.R_CENTER] * 100
    r_dis_km = r_dis_km.assign_attrs({
        'units': 'km',
        'long_name': 'radial distance from CHC'
    })
    # return r_dis_km
    ds = ds.assign_coords(**{R_DIS_KM: r_dis_km})

    if ds_lab is None:
        ds_lab = ds[[conc_lab_ts, conc_lab]].where(ds['lab'] == i_lab)
    ds_lab = ds_lab.assign_coords(**{R_DIS_KM: r_dis_km})
    # print(ds_lab.attrs)
    cols = 10
    rows = 10
    f_len = figure_size
    f: plt.Figure = plt.figure(
        figsize=(f_len, cols / rows * f_len),
        dpi=dpi
    )
    xn = 2
    yn = 4
    xp = .07
    yp = .05
    ww = (1 - xp * 3) / xn
    hh = (1 - yp * 5) / yn
    # yy = .29
    # xx = .4
    # xxL = .55
    # ypos = .35
    l_lpb = ((2 * xp) + ww, (2 * yp) + hh, .8 * ww, hh)
    l_bol = ((2 * xp) + ww, yp, .8 * ww, hh)
    l_ts = (xp, 1 - yp - hh, 1 - 2 * xp, hh)
    l_per_inf = (xp, (3 * yp) + (2 * hh), ww, hh)
    l_far = (xp, yp, 1.2 * ww, hh)
    l_zoom = (xp, 2 * yp + hh, 1.2 * ww, hh)

    ax_vertical_far_: plt.Axes = f.add_axes(l_far)
    da_lab = ds_lab[conc_lab]
    da_lab.load()
    plot_vertical_ax(ax_vertical_far_, conc_lab, ds, da_lab)
    ax_vertical_far_.scatter(0, 5240, c='blue', label='CHC')
    ax_vertical_far_.legend()
    ax_vertical_far_.grid(True)
    ax_vertical_far_.set_axisbelow(False)

    ax_vertical_zoom = f.add_axes(l_zoom)
    plot_vertical_ax(ax_vertical_zoom, conc_lab, ds, da_lab,
                     xlim=(0, 200))
    ax_vertical_zoom.scatter(0, 5240, c='blue', label='CHC')
    ax_vertical_zoom.legend()
    ax_vertical_zoom.grid(True)
    ax_vertical_zoom.set_axisbelow(False)

    carree = crt.crs.PlateCarree()

    ax_lpb: cartopy.mpl.geoaxes.GeoAxesSubplot = \
        f.add_axes(l_lpb, projection=carree)

    cc_ = da_lab.sum([co.RL, co.ZM], keep_attrs=True)
    ax_lpb = fa.get_ax_lapaz(ax=ax_lpb)
    print(cc_.attrs)
    fa.logpolar_plot(cc_, name=conc_lab, ax=ax_lpb)

    ax_bol = f.add_axes(l_bol, projection=carree)
    lo_la_bol_range = 15
    lo_la_limits = [
        co.CHC_LON - lo_la_bol_range,
        co.CHC_LON + lo_la_bol_range,
        co.CHC_LAT - lo_la_bol_range,
        co.CHC_LAT + lo_la_bol_range,
    ]
    ax_bol = fa.get_ax_bolivia(ax=ax_bol, lola_extent=lo_la_limits)

    fa.logpolar_plot(cc_, name=conc_lab, ax=ax_bol)

    # noinspection PyUnusedLocal
    ax_ts = f.add_axes(l_ts)

    plot_lab_time_series(da_lab=ds_lab[conc_lab_ts], ax_ts=ax_ts)

    ax_per_inf = f.add_axes(l_per_inf)
    if df_prop is not None:
        plot_cluster_summary_figure(
            df_prop,
            'SRR [%]',
            'distance from CHC [km]',
            # save_fig=True,
            fig_save_name='dis_vs_srr_influence.pdf',
            xy_locs=([-30, 5], [0, 9], [500, 11], [950, 9]),
            y_range=(0, 13),
            add_vertical_lines=True,
            add_cluster_group_label=False,
            ax=ax_per_inf
        )
        y = df_prop.set_index('cluster_i')['SRR [%]'][i_lab]
        x = df_prop.set_index('cluster_i')['distance from CHC [km]'][i_lab]
        # ax_per_inf.scatter(x,y,c='k',s=10,marker='o')
        ax_per_inf.scatter(x, y, s=200, marker='o', facecolors='none',
                           edgecolors='k')

    # todo fix this
    else:
        plot_concentration_influence_div_by_cluster(
            ds=ds, conc_lab=conc_lab, i_lab=i_lab,
            ax=ax_per_inf
        )

    plt.show()
    if fig_title is None:
        f.suptitle(f'cluster: {i_lab}', y=.95)
    else:
        f.suptitle(fig_title, y=1)
    f.tight_layout()
    log.ger.debug('show plot')
    f.show()
    return f