def plot_LDA(X_lda, y_lda, class_colors, exp_var, style, fig_size, label_pad,
             font_size, sids, dim=2, zangles=None, pt_size=250, out_fp=""):
    """
    Plot transformed LDA data.
    """
    cats = class_colors.keys()
    fig = plt.figure(figsize=fig_size)
    if dim == 3:
        try:
            assert X_lda.shape[1] >= 3
        except AssertionError:
            sys.exit("\nLinear Discriminant Analysis requires at least 4 groups of "
                     "samples to create a 3D figure. Please update group information or "
                     "use the default 2D view of the results.\n")
        if sids is not None:
            print("\nPoint annotations are available only for 2D figures.\n")
        ax = fig.add_subplot(111, projection="3d")
        ax.view_init(elev=zangles[1], azim=zangles[0])
        try:
            ax.set_zlabel("LD3 (Percent Explained Variance: {:.3f}%)".
                          format(exp_var[2]*100), fontsize=font_size, labelpad=label_pad)
        except:
            ax.set_zlabel("LD3", fontsize=font_size, labelpad=label_pad)
        for i, target_name in zip(range(len(cats)), cats):
            cat_x = X_lda[:, 0][y_lda == target_name]
            cat_y = X_lda[:, 1][y_lda == target_name]
            cat_z = X_lda[:, 2][y_lda == target_name]
            ax.scatter(xs=cat_x, ys=cat_y, zs=cat_z, label=target_name,
                       c=class_colors[target_name], alpha=0.85, s=pt_size, edgecolors="k",
                       zdir="z")
    else:
        ax = fig.add_subplot(111)
        for i, target_name in zip(range(len(cats)), cats):
            cat_x = X_lda[:, 0][y_lda == target_name]
            if X_lda.shape[1] == 1:
                cat_y = np.ones((cat_x.shape[0], 1)) + i
            else:
                cat_y = X_lda[:, 1][y_lda == target_name]
            ax.scatter(x=cat_x, y=cat_y, label=target_name, alpha=0.85, s=pt_size,
                       color=class_colors[target_name], edgecolors="k")
        # Annotate data points with sample IDs
        if sids is not None:
            for sample, point, group in zip(sids, X_lda, y_lda):
                try:
                    assert len(point) >= 2
                except AssertionError:
                    point = (point[0], cats.index(group)+1)
                finally:
                    ax.annotate(s=sample, xy=point[:2], xytext=(0, -15), ha="center",
                                va="center", textcoords="offset points")
    if X_lda.shape[1] == 1:
        plt.ylim((0.5, 2.5))
    try:
        ax.set_xlabel("LD1 (Percent Explained Variance: {:.3f}%)".
                      format(exp_var[0]*100), fontsize=font_size, labelpad=label_pad)
    except:
        ax.set_xlabel("LD1", fontsize=font_size, labelpad=label_pad)
    try:
        ax.set_ylabel("LD2 (Percent Explained Variance: {:.3f}%)".
                      format(exp_var[1]*100), fontsize=font_size, labelpad=label_pad)
    except:
        ax.set_ylabel("LD2", fontsize=font_size, labelpad=label_pad)

    leg = plt.legend(loc="best", scatterpoints=3, frameon=True, framealpha=1, fontsize=15)
    leg.get_frame().set_edgecolor('k')
    if dim == 2 and style:
        gu.ggplot2_style(ax)
        fc = "0.8"
    else:
        fc = "none"

    # save or display result
    if out_fp:
        plt.savefig(out_fp, facecolor=fc, edgecolor="none", dpi=300, bbox_inches="tight",
                    pad_inches=0.1)
    else:
        plt.show()
def plot_LDA ( X_lda , y_lda , class_colors , exp_var , style , fig_size , label_pad , font_size , sids , dim = 2 , zangles = None , pt_size = 250 , out_fp = "" ) : cats = class_colors . keys ( ) fig = plt . figure ( figsize = fig_size ) if dim == 3 : try : assert X_lda . shape [ 1 ] >= 3 except AssertionError : sys . exit ( "\nLinear Discriminant Analysis requires at least 4 groups of " "samples to create a 3D figure. Please update group information or " "use the default 2D view of the results.\n" ) if sids is not None : print ( "\nPoint annotations are available only for 2D figures.\n" ) ax = fig . add_subplot ( 111 , projection = "3d" ) ax . view_init ( elev = zangles [ 1 ] , azim = zangles [ 0 ] ) try : ax . set_zlabel ( "LD3 (Percent Explained Variance: {:.3f}%)" . format ( exp_var [ 2 ] * 100 ) , fontsize = font_size , labelpad = label_pad ) except : ax . set_zlabel ( "LD3" , fontsize = font_size , labelpad = label_pad ) for i , target_name in zip ( range ( len ( cats ) ) , cats ) : cat_x = X_lda [ : , 0 ] [ y_lda == target_name ] cat_y = X_lda [ : , 1 ] [ y_lda == target_name ] cat_z = X_lda [ : , 2 ] [ y_lda == target_name ] ax . scatter ( xs = cat_x , ys = cat_y , zs = cat_z , label = target_name , c = class_colors [ target_name ] , alpha = 0.85 , s = pt_size , edgecolors = "k" , zdir = "z" ) else : ax = fig . add_subplot ( 111 ) for i , target_name in zip ( range ( len ( cats ) ) , cats ) : cat_x = X_lda [ : , 0 ] [ y_lda == target_name ] if X_lda . shape [ 1 ] == 1 : cat_y = np . ones ( ( cat_x . shape [ 0 ] , 1 ) ) + i else : cat_y = X_lda [ : , 1 ] [ y_lda == target_name ] ax . scatter ( x = cat_x , y = cat_y , label = target_name , alpha = 0.85 , s = pt_size , color = class_colors [ target_name ] , edgecolors = "k" ) if sids is not None : for sample , point , group in zip ( sids , X_lda , y_lda ) : try : assert len ( point ) >= 2 except AssertionError : point = ( point [ 0 ] , cats . index ( group ) + 1 ) finally : ax . annotate ( s = sample , xy = point [ : 2 ] , xytext = ( 0 , - 15 ) , ha = "center" , va = "center" , textcoords = "offset points" ) if X_lda . shape [ 1 ] == 1 : plt . ylim ( ( 0.5 , 2.5 ) ) try : ax . set_xlabel ( "LD1 (Percent Explained Variance: {:.3f}%)" . format ( exp_var [ 0 ] * 100 ) , fontsize = font_size , labelpad = label_pad ) except : ax . set_xlabel ( "LD1" , fontsize = font_size , labelpad = label_pad ) try : ax . set_ylabel ( "LD2 (Percent Explained Variance: {:.3f}%)" . format ( exp_var [ 1 ] * 100 ) , fontsize = font_size , labelpad = label_pad ) except : ax . set_ylabel ( "LD2" , fontsize = font_size , labelpad = label_pad ) leg = plt . legend ( loc = "best" , scatterpoints = 3 , frameon = True , framealpha = 1 , fontsize = 15 ) leg . get_frame ( ) . set_edgecolor ( 'k' ) if dim == 2 and style : gu . ggplot2_style ( ax ) fc = "0.8" else : fc = "none" if out_fp : plt . savefig ( out_fp , facecolor = fc , edgecolor = "none" , dpi = 300 , bbox_inches = "tight" , pad_inches = 0.1 ) else : plt . show ( )
Plot transformed LDA data.