我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用seaborn.cubehelix_palette()。
def __init__(self, parent): fig = Figure(figsize=(4, 4), dpi=100, tight_layout=True) super(DefaultGraph, self).__init__(fig) self.setParent(parent) sns.set(style="dark") for index, s in zip(range(9), np.linspace(0, 3, 10)): axes = fig.add_subplot(3, 3, index + 1) x, y = np.random.randn(2, 50) cmap = sns.cubehelix_palette(start=s, light=1, as_cmap=True) sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=axes) axes.set_xlim(-3, 3) axes.set_ylim(-3, 3) axes.set_xticks([]) axes.set_yticks([]) fig.suptitle("Activity Browser", y=0.5, fontsize=30, backgroundcolor=(1, 1, 1, 0.5)) self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) self.updateGeometry()
def __init__(self, parent, mlca, width=6, height=6, dpi=100): figure = Figure(figsize=(width, height), dpi=dpi, tight_layout=True) axes = figure.add_subplot(111) super(LCAResultsPlot, self).__init__(figure) self.setParent(parent) activity_names = [format_activity_label(next(iter(f.keys()))) for f in mlca.func_units] # From https://stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html cmap = sns.cubehelix_palette(8, start=.5, rot=-.75, as_cmap=True) hm = sns.heatmap( # mlca.results / np.average(mlca.results, axis=0), # Normalize to get relative results mlca.results, annot=True, linewidths=.05, cmap=cmap, xticklabels=["\n".join(x) for x in mlca.methods], yticklabels=activity_names, ax=axes, square=False, ) hm.tick_params(labelsize=8) self.setMinimumSize(self.size()) # sns.set_context("notebook")
def plot_heatmaps(data, mis, column_label, cont, topk=30, prefix=''): cmap = sns.cubehelix_palette(as_cmap=True, light=.9) m, nv = mis.shape for j in range(m): inds = np.argsort(- mis[j, :])[:topk] if len(inds) >= 2: plt.clf() order = np.argsort(cont[:,j]) subdata = data[:, inds][order].T subdata -= np.nanmean(subdata, axis=1, keepdims=True) subdata /= np.nanstd(subdata, axis=1, keepdims=True) columns = [column_label[i] for i in inds] sns.heatmap(subdata, vmin=-3, vmax=3, cmap=cmap, yticklabels=columns, xticklabels=False, mask=np.isnan(subdata)) filename = '{}/heatmaps/group_num={}.png'.format(prefix, j) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) plt.title("Latent factor {}".format(j)) plt.yticks(rotation=0) plt.savefig(filename, bbox_inches='tight') plt.close('all') #plot_rels(data[:, inds], map(lambda q: column_label[q], inds), colors=cont[:, j], # outfile=prefix + '/relationships/group_num=' + str(j), latent=labels[:, j], alpha=0.1)
def plot_crossval_auc(roc_curves): cmap = sns.cubehelix_palette(11) aucs = [] ax = plt.axes() for fold in roc_curves.keys(): (f, p) = roc_curves[fold] aucs.append(area_under_curve(f, p)) label_str = "fold {}, roc auc: {:.2f}".format(fold, aucs[-1]) ax.plot(f, p, label=label_str, color=cmap[fold]) ax.plot([0, 1], [0, 1], label="random, roc auc: 0.5", color="black") ax.legend(loc="lower right") plt.xlabel("False positive rate") plt.ylabel("True positive rate") plt.title( "ROC curves across 10 different validation folds(tiny convnet " "trained on small datasets)") plt.show()
def print_heatmap( points,label,id_map ): ''' points: N_samples * N_features label: (int) N_samples id_map: map label id to its name ''' # = sns.color_palette("RdBu_r", max(label)+1) #cNorm = colors.Normalize(vmin=0,vmax=max(label)) #normalise the colormap #scalarMap = cm.ScalarMappable(norm=cNorm,cmap='Paired') #map numbers to colors index = [id_map[i] for i in label] df = DataFrame( points, columns = list(range(points.shape[1])), index = index ) row_color = [current_palette[i] for i in label] cmap = sns.cubehelix_palette(as_cmap=True, rot=-.3, light=1) g = sns.clustermap( df,cmap=cmap,row_colors=row_color,col_cluster=False,xticklabels=False,yticklabels=False) #,standard_scale=1 ) return g.fig
def dendrogram(df, number_of_clusters=int(df.shape[1] / 1.2)): # Create Dendrogram agglomerated_features = FeatureAgglomeration(n_clusters=number_of_clusters) used_networks = np.arange(0, number_of_clusters, dtype=int) # Create a custom palette to identify the networks network_pal = sns.cubehelix_palette(len(used_networks), light=.9, dark=.1, reverse=True, start=1, rot=-2) network_lut = dict(zip(map(str, df.columns), network_pal)) # Convert the palette to vectors that will be drawn on the side of the matrix networks = df.columns.get_level_values(None) network_colors = pd.Series(networks, index=df.columns).map(network_lut) sns.set(font="monospace") # Create custom colormap cmap = sns.diverging_palette(h_neg=210, h_pos=350, s=90, l=30, as_cmap=True) cg = sns.clustermap(df.astype(float).corr(), cmap=cmap, linewidths=.5, row_colors=network_colors, col_colors=network_colors) plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0) plt.setp(cg.ax_heatmap.xaxis.get_majorticklabels(), rotation=90) plt.show()
def plot_heatmaps(data, labels, alpha, mis, column_label, cont, topk=20, prefix='', focus=''): cmap = sns.cubehelix_palette(as_cmap=True, light=.9) m, nv = mis.shape for j in range(m): inds = np.where(np.logical_and(alpha[j] > 0, mis[j] > 0.))[0] inds = inds[np.argsort(- alpha[j, inds] * mis[j, inds])][:topk] if focus in column_label: ifocus = column_label.index(focus) if not ifocus in inds: inds = np.insert(inds, 0, ifocus) if len(inds) >= 2: plt.clf() order = np.argsort(cont[:,j]) subdata = data[:, inds][order].T subdata -= np.nanmean(subdata, axis=1, keepdims=True) subdata /= np.nanstd(subdata, axis=1, keepdims=True) columns = [column_label[i] for i in inds] sns.heatmap(subdata, vmin=-3, vmax=3, cmap=cmap, yticklabels=columns, xticklabels=False, mask=np.isnan(subdata)) filename = '{}/heatmaps/group_num={}.png'.format(prefix, j) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) plt.title("Latent factor {}".format(j)) plt.savefig(filename, bbox_inches='tight') plt.close('all') #plot_rels(data[:, inds], list(map(lambda q: column_label[q], inds)), colors=cont[:, j], # outfile=prefix + '/relationships/group_num=' + str(j), latent=labels[:, j], alpha=0.1)
def plot_pairplots(data, labels, alpha, mis, column_label, topk=5, prefix='', focus=''): cmap = sns.cubehelix_palette(as_cmap=True, light=.9) plt.rcParams.update({'font.size': 32}) m, nv = mis.shape for j in range(m): inds = np.where(np.logical_and(alpha[j] > 0, mis[j] > 0.))[0] inds = inds[np.argsort(- alpha[j, inds] * mis[j, inds])][:topk] if focus in column_label: ifocus = column_label.index(focus) if not ifocus in inds: inds = np.insert(inds, 0, ifocus) if len(inds) >= 2: plt.clf() subdata = data[:, inds] columns = [column_label[i] for i in inds] subdata = pd.DataFrame(data=subdata, columns=columns) try: sns.pairplot(subdata, kind="reg", diag_kind="kde", size=5, dropna=True) filename = '{}/pairplots_regress/group_num={}.pdf'.format(prefix, j) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) plt.suptitle("Latent factor {}".format(j), y=1.01) plt.savefig(filename, bbox_inches='tight') plt.clf() except: pass subdata['Latent factor'] = labels[:,j] try: sns.pairplot(subdata, kind="scatter", dropna=True, vars=subdata.columns.drop('Latent factor'), hue="Latent factor", diag_kind="kde", size=5) filename = '{}/pairplots/group_num={}.pdf'.format(prefix, j) if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) plt.suptitle("Latent factor {}".format(j), y=1.01) plt.savefig(filename, bbox_inches='tight') plt.close('all') except: pass
def dendrogram(df, number_of_clusters, agglomerated_feature_labels): import seaborn as sns # Todo: Create Dendrogram # used networks are the labels occuring in agglomerated_features.labels_ # which corresponds to np.arange(0, number_of_clusters) # number_of_clusters = int(df.shape[1] / 1.2) # used_networks = np.arange(0, number_of_clusters, dtype=int) used_networks = np.unique(agglomerated_feature_labels) # used_networks = [1, 5, 6, 7, 8, 11, 12, 13, 16, 17] # In our case all columns are clustered, which means used_columns is true in every element # used_columns = (df.columns.get_level_values(None) # .astype(int) # .isin(used_networks)) # used_columns = (agglomerated_feature_labels.astype(int).isin(used_networks)) # df = df.loc[:, used_columns] # Create a custom palette to identify the networks network_pal = sns.cubehelix_palette(len(used_networks), light=.9, dark=.1, reverse=True, start=1, rot=-2) network_lut = dict(zip(map(str, df.columns), network_pal)) # Convert the palette to vectors that will be drawn on the side of the matrix networks = df.columns.get_level_values(None) # networks = agglomerated_feature_labels network_colors = pd.Series(networks, index=df.columns).map(network_lut) # plt.figure() # cg = sns.clustermap(df, metric="correlation") # plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0) sns.set(font="monospace") # Create custom colormap cmap = sns.diverging_palette(h_neg=210, h_pos=350, s=90, l=30, as_cmap=True) cg = sns.clustermap(df.astype(float).corr(), cmap=cmap, linewidths=.5, row_colors=network_colors, col_colors=network_colors) plt.setp(cg.ax_heatmap.yaxis.get_majorticklabels(), rotation=0) plt.setp(cg.ax_heatmap.xaxis.get_majorticklabels(), rotation=90) # plt.xticks(rotation=90) plt.show()