Esempio n. 1
0
def plot_bumps_1d(Y,
                  subsampling=20,
                  labels=None,
                  labels_palette='hls',
                  ax=None):
    if ax is None:
        ax = plt.gca()

    Y_subsampled = Y[:, ::subsampling]

    ax.plot(Y_subsampled)
    ax.set_xticks([])

    if labels is not None:
        labels = np.sort(labels)
        unique_labels = np.unique(labels)

        segments = []
        for lab in unique_labels:
            subset = np.where(labels == lab)[0]
            segments.append((subset[0] - 0.5, subset[-1] + 0.5))

        offset = -0.1 * Y_subsampled.max()
        h_segments = [((s[0], offset), (s[1], offset)) for s in segments]

        colors = sns.color_palette(labels_palette, n_colors=len(unique_labels))

        hlc = LineCollection(h_segments, colors=colors)
        hlc.set_linewidth(5)
        hlc.set_clip_on(False)
        ax.add_collection(hlc)
Esempio n. 2
0
def plot_matrix(mat,
                cmap='gray_r',
                labels=None,
                which_labels='both',
                labels_palette='Set1',
                ax=None,
                colorbar_labelsize=None):
    if ax is None:
        ax = plt.gca()

    values = np.unique(mat)

    vmin = values.min()
    vmax = values.max()

    plt_image = ax.imshow(mat,
                          interpolation='none',
                          cmap=cmap,
                          vmin=vmin,
                          vmax=vmax)
    ax.grid(False)
    ax.tick_params(axis='both',
                   which='both',
                   bottom=False,
                   top=False,
                   left=False,
                   right=False,
                   labelbottom=False,
                   labelleft=False)

    cbar = plt.colorbar(plt_image,
                        orientation='horizontal',
                        pad=.05,
                        fraction=.05,
                        ax=ax)
    cbar.ax.tick_params(labelrotation=90)
    if colorbar_labelsize is not None:
        cbar.ax.tick_params(labelsize=colorbar_labelsize)

    if labels is not None:
        labels = np.sort(labels)
        unique_labels = np.unique(labels)

        segments = []
        for lab in unique_labels:
            subset = np.where(labels == lab)[0]
            segments.append((subset[0] - 0.5, subset[-1] + 0.5))

        offset = -0.05 * mat.shape[0]
        h_segments = [((s[0], offset), (s[1], offset)) for s in segments]
        v_segments = [((offset, s[0]), (offset, s[1])) for s in segments]

        colors = sns.color_palette(labels_palette, n_colors=len(unique_labels))

        if which_labels != 'both' and which_labels != 'horizontal'\
                and which_labels != 'vertical':
            raise ValueError('Wrong value for which_labels')

        if which_labels == 'both' or which_labels == 'horizontal':
            hlc = LineCollection(h_segments, colors=colors)
            hlc.set_linewidth(5)
            hlc.set_clip_on(False)
            ax.add_collection(hlc)
        if which_labels == 'both' or which_labels == 'vertical':
            vlc = LineCollection(v_segments, colors=colors)
            vlc.set_linewidth(5)
            vlc.set_clip_on(False)
            ax.add_collection(vlc)