我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用seaborn.PairGrid()。
def plotCorrelation(stats): #columnsToDrop = ['sleep_interval_max_len', 'sleep_interval_min_len', # 'sleep_interval_avg_len', 'sleep_inefficiency', # 'sleep_hours', 'total_hours'] #stats = stats.drop(columnsToDrop, axis=1) g = sns.PairGrid(stats) def corrfunc(x, y, **kws): r, p = scipystats.pearsonr(x, y) ax = plt.gca() ax.annotate("r = {:.2f}".format(r),xy=(.1, .9), xycoords=ax.transAxes) ax.annotate("p = {:.2f}".format(p),xy=(.2, .8), xycoords=ax.transAxes) if p>0.04: ax.patch.set_alpha(0.1) g.map_upper(plt.scatter) g.map_diag(plt.hist) g.map_lower(sns.kdeplot, cmap="Blues_d") g.map_upper(corrfunc) sns.plt.show()
def histogramnd(ax, data, **kwargs): """n-dimensional histogram seaborn based """ scatter_data_raw = data scatter_data_cols = ["x_%d" % (i,) for i in range(data.shape[1])] # prepare dataframe df = pd.DataFrame(scatter_data_raw, columns=scatter_data_cols) g = sns.PairGrid(df) # g.map_diag(plt.hist) g.map_diag(sns.kdeplot) g.map_offdiag(plt.hexbin, cmap="gray", gridsize=30, bins="log"); # logger.log(loglevel_debug, "dir(g)", dir(g)) # print g.diag_axes # print g.axes # for i in range(data.shape[1]): # for j in range(data.shape[1]): # 1, 2; 0, 2; 0, 1 # if i == j: # continue # # column gives x axis, row gives y axis, thus need to reverse the selection for plotting goal # # g.axes[i,j].plot(df["%s%d" % (self.cols_goal_base, j)], df["%s%d" % (self.cols_goal_base, i)], "ro", alpha=0.5) # g.axes[i,j].plot(df["x_%d" % (j,)], df["x_%d" % (i,)], "ro", alpha=0.5) plt.show() # run sns scattermatrix on dataframe # plot_scattermatrix(df, ax = None)
def bubble(data,x,y,hue,bsize,palette='Reds',xscale='linear',yscale='linear',title='',suptitle=0): if suptitle == 0: suptitle = bsize sns.set(style="whitegrid") sns.set_context("notebook", font_scale=3, rc={"lines.linewidth": 0.3}) sns.set_color_codes("bright") size = (1500 / float(data[bsize].max())) size = data[bsize] * size g = sns.PairGrid(data, hue=hue, palette=palette, y_vars=y, x_vars=x, size=12, aspect=3) g.map(plt.scatter, s=size); g.set(xscale=xscale) g.set(yscale=yscale) g.add_legend(title=hue, bbox_to_anchor=(0.9, 0.6)) plt.title(title, fontsize=48, y=1.12, color="gray"); plt.suptitle("size = " + suptitle, verticalalignment='top', fontsize=35, y=1.01, x=0.48, color="gray") plt.xlabel(x, fontsize=38, labelpad=30, color="gray"); plt.ylabel(y, fontsize=38, labelpad=30, color="gray"); plt.tick_params(axis='both', which='major', pad=25) plt.axhline(linewidth=2.5, color="black"); plt.axvline(linewidth=2.5, color="black"); plt.ylim(0,); plt.xlim(0,);
def bubble(data,x,y,hue,bsize,palette='Reds',xscale='linear',yscale='linear',title='',suptitle=0,xlim_start=0,ylim_start=0,xlim_end=0,ylim_end=0): import seaborn as sns import matplotlib.pyplot as plt """ x > should be int or float y > should be int or float hue > should be boolean or category bsize > should be int or float """ if suptitle == 0: suptitle = bsize y_modifier = (data[y].max() - data[y].min()) * 0.1 x_modifier = (data[x].max() - data[x].min()) * 0.1 if ylim_start == 0: ylim_start = data[y].min() if xlim_start == 0: xlim_start = data[x].min() if ylim_end == 0: ylim_end = data[y].max() + y_modifier if xlim_end == 0: xlim_end = data[x].max() + (x_modifier * 2) sns.set(style="whitegrid") sns.set_context("notebook", font_scale=3, rc={"lines.linewidth": 0.3}) sns.set_color_codes("bright") size = (1500 / float(data[bsize].max())) size = data[bsize] * size g = sns.PairGrid(data, hue=hue, palette=palette, y_vars=y, x_vars=x, size=12, aspect=3) g.map(plt.scatter, s=5000); g.set(xscale=xscale) g.set(yscale=yscale) g.add_legend(title=hue, bbox_to_anchor=(0.9, 0.6)) plt.title(title, fontsize=48, y=1.12, color="gray"); plt.suptitle("size = " + suptitle, verticalalignment='top', fontsize=35, y=1.01, x=0.48, color="gray") plt.xlabel(x, fontsize=38, labelpad=30, color="gray"); plt.ylabel(y, fontsize=38, labelpad=30, color="gray"); plt.tick_params(axis='both', which='major', pad=25) plt.axhline(linewidth=2.5, color="black"); plt.axvline(linewidth=2.5, color="black"); plt.ylim(ylim_start,ylim_end); plt.xlim(xlim_start,xlim_end);
def plot_kde_matrix(df, w, limits=None, colorbar=True): """ Plot a KDE matrix. Parameters ---------- df: Pandas Dataframe The rows are the observations, the columns the variables. w: np.narray The corresponding weights. colorbar: bool Whether to plot the colorbars or not. limits: dictionary, optional Dictionary of the form ``{"name": (lower_limit, upper_limit)}``. """ grid = sns.PairGrid(df, diag_sharey=False) if limits is None: limits = {} default = (None, None) def off_diagonal(x, y, **kwargs): df = pd.concat((x, y), axis=1) plot_kde_2d(df, w, x.name, y.name, xmin=limits.get(x.name, default)[0], xmax=limits.get(x.name, default)[1], ymin=limits.get(y.name, default)[0], ymax=limits.get(y.name, default)[1], ax=plt.gca(), title=False, colorbar=colorbar) def scatter(x, y, **kwargs): alpha = w / w.max() colors = np.zeros((alpha.size, 4)) colors[:, 3] = alpha plt.gca().scatter(x, y, color="k") plt.gca().set_xlim(*limits.get(x.name, default)) plt.gca().set_ylim(*limits.get(y.name, default)) def diagonal(x, **kwargs): df = pd.concat((x,), axis=1) plot_kde_1d(df, w, x.name, xmin=limits.get(x.name, default)[0], xmax=limits.get(x.name, default)[1], ax=plt.gca()) grid.map_diag(diagonal) grid.map_upper(scatter) grid.map_lower(off_diagonal) return grid