예제 #1
0
def cycles():
    B = dict(rh.iteritems())
    B.update({'obs': RH})
    B = pd.Panel(B)
    fig = plt.figure(figsize=(10, 6), subplotpars=SubplotParams(left=.08))
    for k in range(2):
        sm = cm.ScalarMappable(
            norm=colors.Normalize(vmin=0, vmax={
                0: 30,
                1: 60
            }[k]))
        for j, s in enumerate(['obs', 'd02', 'd03_0_00', 'd03_0_12']):
            ax = plt.subplot(2, 4, 4 * k + j + 1)
            if k == 0: ax.set_title(s)
            b = B[s].apply(partial(pow, {0: 24 * 365.25, 1: 24}[k])).dropna()
            D = pd.concat((sta.loc[b.index, ('lon', 'lat')], b),
                          axis=1).dropna()
            sm.set_array(D.iloc[:, -1])
            sm.set_cmap('gnuplot')
            for i, a in D.iterrows():
                map.plot(a['lon'],
                         a['lat'],
                         'o',
                         color=sm.to_rgba(a.iloc[-1]),
                         latlon=True)
            map.drawcoastlines()
            map.drawparallels(range(-32, -28, 1),
                              labels=[max(0, 1 - j), 0, 0, 0])
            map.drawmeridians(range(-72, -69, 1), labels=[0, 0, 0, k])
            if j == 3:
                bb = ax.get_position()
                ax = fig.add_axes([bb.x1 + 0.02, bb.y0, 0.02, bb.y1 - bb.y0])
                plt.colorbar(sm, cax=ax)
예제 #2
0
def plot_portfolios(
    portfolios: pd.DataFrame, color: str='brg',
    fsize: tuple=(12, 10), is_return: bool= False,
    save: bool=False
):
    """Plota os portfólios no plano vol x ret, destacando em azul o de
    mínima volatilidade e em vermelho o de máximo índice de Sharpe.

    Args:
        portfolios (pd.DataFrame): df contendo os portfólios.
        color (str, optional): palette de cores. Padrão: 'brg'.
        fsize (tuple, optional): tamanho do plot. Padrão: (12, 10).
        save (bool, optional): se True, salva um png do plot, com dpi=200,
        de nome 'portfolios_hd' em 'save_path'.
    """
    plt.figure(figsize=fsize)
    cor = color
    ax = sns.scatterplot(
        x='Volatilidade', y='Retorno',
        hue='Ind. Sharpe', data=portfolios,
        palette=cor
    )

    norm = plt.Normalize(
        0,
        portfolios['Ind. Sharpe'].max()
    )

    sm = plt.cm.ScalarMappable(cmap=cor, norm=norm)
    sm.set_array([])

    ax.get_legend().remove()
    ax.figure.colorbar(sm)

    port_min_vol = find_port_min_vol(portfolios).T
    port_max_sr = find_port_max_sr(portfolios).T
    plt.scatter(
        x=port_max_sr['Volatilidade'],
        y=port_max_sr['Retorno'], c='red',
        marker='o', s=200
    )
    plt.scatter(
        x = port_min_vol['Volatilidade'],
        y = port_min_vol['Retorno'], c='blue',
        marker='o', s=200
    )

    plt.title('Portfólios')

    if save:
        plt.savefig(save_path + 'portfolios.png', dpi=200)

    if not is_return:
        plt.show()
    else:
        return ax
예제 #3
0
def mappable_from_values(values, cmap=DEFAULT_CONT_COLORMAP, **kwargs):
    """
    Create a scalar mappable object from an array of values.

    Returns
    -----------
    :class:`matplotlib.cm.ScalarMappable`
    """
    sm = plt.cm.ScalarMappable(cmap=cmap)
    sm.set_array(values)
    return sm
예제 #4
0
def bias():
    fig = plt.figure(figsize=(10, 6), subplotpars=SubplotParams(right=.88))
    for j, s in enumerate(['d02', 'd03_0_00', 'd03_0_12']):
        ax = plt.subplot(2, 3, j + 1)
        ax.set_title(s)
        b = (rh[s] - RH).mean()
        D = pd.concat((sta.loc[b.index, ('lon', 'lat')], b), axis=1).dropna()
        m = 30
        sm = cm.ScalarMappable(norm=colors.Normalize(vmin=-m, vmax=m))
        sm.set_array(D.iloc[:, -1])
        sm.set_cmap('coolwarm')
        for i, a in D.iterrows():
            map.plot(a['lon'],
                     a['lat'],
                     'o',
                     color=sm.to_rgba(a.iloc[-1]),
                     latlon=True)
        map.drawcoastlines()
        map.drawparallels(range(-32, -28, 1), labels=[max(0, 1 - j), 0, 0, 0])
        map.drawmeridians(range(-72, -69, 1), labels=[0, 0, 0, 1])

        if j == 2:
            bb = ax.get_position()
            ax = fig.add_axes([bb.x1 + 0.02, bb.y0, 0.02, bb.y1 - bb.y0])
            plt.colorbar(sm, cax=ax)
            ax.set_title('$\Delta$RH', usetex=True)

        ax = plt.subplot(2, 3, j + 4)
        p = pd.concat((b, dz[j]), axis=1)
        plt.plot(p[0], p[1], 'o')
        ax.set_xlabel('$\Delta$RH model-station', usetex=True)
        ax.grid()
        if j == 1: ax.set_yticklabels([])
        if j == 2:
            ax.yaxis.set_ticks_position('right')
            ax.yaxis.set_label_position('right')
        if j != 1:
            ax.set_ylabel('$\Delta$z model-station', usetex=True)
예제 #5
0
def rms():
    fig = plt.figure(figsize=(10, 6))
    for j, s in enumerate(['d02', 'd03_0_00', 'd03_0_12']):
        ax = plt.subplot(1, 3, j + 1)
        ax.set_title(s)
        b = (((rh[s] - RH)**2).mean()**.5).dropna()

        D = pd.concat((sta.loc[b.index, ('lon', 'lat')], b), axis=1).dropna()
        sm = cm.ScalarMappable(norm=colors.Normalize(vmin=8, vmax=40))
        sm.set_array(D.iloc[:, -1])
        sm.set_cmap('gnuplot')
        for i, a in D.iterrows():
            map.plot(a['lon'],
                     a['lat'],
                     'o',
                     color=sm.to_rgba(a.iloc[-1]),
                     latlon=True)
        map.drawcoastlines()
        map.drawparallels(range(-32, -28, 1), labels=[max(1 - j, 0), 0, 0, 0])
        map.drawmeridians(range(-72, -69, 1), labels=[0, 0, 0, 1])
        if j == 2:
            bb = ax.get_position()
            ax = fig.add_axes([bb.x1 + 0.02, bb.y0, 0.02, bb.y1 - bb.y0])
            plt.colorbar(sm, cax=ax)
                 big_data['active'],
                 align='center',
                 ecolor='black',
                 capsize=10,
                 color=colors)
fig.set_size_inches(25, 10)
#plt.axhline(y=y, color='slategrey', linestyle='-')
ax.set_ylabel('active_cases')
ax.set_xlabel('statecode')
ax.set_xticks(x_pos)
ax.set_xticklabels(labels)
ax.yaxis.grid(True)
ax.xaxis.grid(False)
ax.set_ylim([0, 20000])
sm = ScalarMappable(cmap=my_cmap)
sm.set_array([])
ax.set_title('active cases vs state')
cbar = plt.colorbar(sm)
cbar.set_label('color represents active cases by population ratio(normalized)',
               rotation=270,
               labelpad=25)

big_data.columns

big_data = big_data[[
    'state', 'statecode', 'confirmed', 'active', 'deaths', 'recovered',
    'population', 'density', 'poverty', 'hdi'
]]

big_data.head()
예제 #7
0
def scores_scatterplot(x, y, table=None, distance_gradient=True, label_pos=0, label_neg=0,
                       distance:pd.Series=None, min_label_dist=0.5, min_label_diff=0.5,
                        labels=None, formatters=None, dist_name = None, gradient=1, ax=None) -> plt.Axes:
    """Produce biplot of 2 essentiality series.
    args:
        x, y:
            str that point to column labels, or
            pd.Series with shared index that gives x and y values
            to be plotted.

        table:
            Score table from e.g. .tabulate_scores() (optional)

        labels:
            list of gene names to be labeled on the plot

        mahal_gradient:
            if True points will be colored by mahal value.

        label_pos, label_neg:
            Pass an int or True to label the most distant top or bottom points.

        minlabel:
            minimum abs(mahal) for labels to be applied by label_pos/neg

        formatters:
            list of tuples as below. format_dict is passed to
            plt.plot(..., **format_dict).
                [(list_of_genes, format_dict), ...]

    returns fig, ax if ax=None, otherwise the supplied ax is returnd."""

    # required changes;
    #   distance optionally supplied
    #   optional filter by abs(x-y)>threshold (use minlabel...?)
    #   remove refs to jacks_score


    fig=None
    if ax is None:
        fig, ax = plt.subplots(1, 1, figsize=(8,8))

    if table is None:
        x_score = x
        y_score = y
    else:
        cols = table[x].columns
        for k in ('jacks_score', 'lfc'):
            if k in cols:
                break
        else:
            raise ValueError('No score column found in '+str(cols))
        x_score = table[x][k]
        y_score = table[y][k]

    if labels is None:
        labels = []
    else:
        labels = copy(labels)

    # deal with any nans
    nans = (x_score.isna() | y_score.isna())
    x_score, y_score = x_score.loc[~nans], y_score.loc[~nans]

    pos_genes, neg_genes = [], []
    if distance_gradient:
        if distance is None:
            _, _, distance = mahal_nocov(
                x_score, table[x].stdev, y_score, table[y].stdev, line_ig=(0, gradient)
            )
        # no nans
        distance = distance.loc[~distance.isna()]
        # get the genes to be labelled first
        distance = distance.sort_values()

        # so that most distant points are plotted on top, order x and y by distance
        x_score = x_score[distance.index]
        y_score = y_score[distance.index]

        # get most distance in the pos and neg directions
        # get most distance in the pos and neg directions
        if label_pos is True:
            label_pos = np.inf
        if label_neg is True:
            label_neg = np.inf

        # get top N labels, filtered for those passing distance and x-y difference thresholds
        diff_mask = abs(x_score - y_score) > min_label_diff
        pos_genes = distance.loc[(distance > min_label_dist) & diff_mask].tail(label_pos).index
        neg_genes = distance.loc[(distance < 0 - min_label_dist) & diff_mask].head(label_neg).index


        # for gradient we don't care about pos/neg
        # but we do want the order to match x_score as the index will be lost
        distance = distance.reindex(x_score.index).abs()
        max_dist = max(distance)

        # normalised mahaladonis for color scaling
        if distance_gradient is True:
            min_dist = 0
        else:
            min_dist = distance_gradient
        norm_dist = (distance-min_dist)/(max_dist-min_dist)
        colrs = cm.viridis(norm_dist) # loses the gene name indicies
        # RGBA
        grey = (0.68, 0.68, 0.68, 1)
        for i, m in enumerate(norm_dist):
            # m == mahal-minmahal
            if m < 0:
                colrs[i] = grey
    else:
        colrs = None

    labels.extend(pos_genes)
    labels.extend(neg_genes)
    #print(labels)
    if not formatters:
        # all genes with empty format spec
        formatters = [(x_score.index, {})]
    for genes, formats in formatters:
        if 'marker' not in formats:
            formats['marker'] ='o'
        plt.scatter(x_score.loc[genes], y_score.loc[genes], c=colrs, **formats)

    # plot a square Axes with orientation lines
    lim = ax.axis()
    minlim = min(lim[0], lim[2])
    maxlim = max(lim[1], lim[3])
    plt.plot([minlim, maxlim], [minlim, maxlim], 'k--', alpha=0.2)
    plt.plot([0, 0], [minlim, maxlim], 'g--', zorder=0)
    plt.plot([minlim, maxlim], [0, 0], 'g--', zorder=0)

    if colrs is not None:
        sm = plt.cm.ScalarMappable(cmap=cm.get_cmap('viridis'))
        sm.set_array([])
        cb = plt.colorbar(sm, fraction=0.03, pad=0.01, aspect=5)

        cb.set_ticks([0.0, 1.0])
        cb.set_ticklabels([str(round(min_dist, 2)),
                           str(round(max_dist, 2))])
        if dist_name is not None:
            cb.ax.set_ylabel(dist_name)

    # labels
    txt = []
    # label significants and control
    if labels:
        for lab in set(labels):
            #txt.append(plt.text(x_score[lab], y_score[lab], lab))
            txt.append(
                plt.annotate(
                    lab,
                    (x_score[lab], y_score[lab]),
                    #bbox=dict(boxstyle='round,pad=0.1', fc='yellow', alpha=0.3),
                    arrowprops=dict(arrowstyle='->', color='#ff8533' )
                )
            )

    # label axes
    if table is not None:
        plt.xlabel(x+' '+k)
        plt.ylabel(y +' '+k)

    avoid_points = True if distance_gradient is True else False
    if avoid_points:
        adjust_text(txt, x_score, y_score, force_points=(0.2,0.25))
    else:
        adjust_text(txt, expand_text=(1.1, 1.2), )

    if fig is not None:
        return ax
    else:
        return fig, ax