def plot_extras(self): # In[ ]: # g = sns.PairGrid(d_bin_v_others.loc[:,["R2", "one_minus_cdf_BH", "SNP-pairs", "contigs_per_bin",]]) # g.map_upper(plt.scatter) # g.map_lower(sns.kdeplot, cmap="Blues_d") # g.map_diag(sns.kdeplot, lw=3, legend=False) d_bin_vars = self.d.d_bin_v_others.loc[:, ["R2", "one_minus_cdf_BH", "SNP-pairs", "contigs_per_bin", ]] sns.palplot(sns.cubehelix_palette(8, start=.5, rot=-.75)) # In[ ]: my_cmap = sns.cubehelix_palette(40, start=.5, rot=-.75, as_cmap=True) cc = sns.mpl.colors.ColorConverter() marginal_color = cc.to_rgb(arg=my_cmap.colors[int(255 * 1)]) # In[ ]: # sns.jointplot('SNP-pairs','R2',d_bin_vars, kind='kde', # joint_kws=dict(shade=True, # cmap=my_cmap, # n_levels=40 # ), # marginal_kws=dict(shade=True, color=my_cmap.colors[int(256*0.66)]) # ) g = sns.JointGrid('SNP-pairs', 'R2', d_bin_vars) g.plot_marginals(sns.distplot, kde=False, color=marginal_color) g.plot_joint(sns.kdeplot, shade=True, cmap=my_cmap, n_levels=40); # In[ ]: # sns.jointplot('one_minus_cdf_BH','R2',d_bin_vars, kind='kde', # joint_kws=dict(shade=True, # cmap=my_cmap, # n_levels=40 # ), # marginal_kws=dict(shade=True, color=my_cmap.colors[int(256*0.66)]) # ) g = sns.JointGrid('one_minus_cdf_BH', 'R2', d_bin_vars) g.plot_marginals(sns.distplot, kde=False, color=marginal_color) g.plot_joint(sns.kdeplot, shade=True, cmap=my_cmap, n_levels=40, alpha=1); # In[ ]: sns.jointplot('contigs_per_bin', 'SNP-pairs', d_bin_vars, kind='kde', joint_kws=dict(shade=True, cmap=my_cmap, n_levels=8 ), marginal_kws=dict(shade=True, color=my_cmap.colors[int(256 * 0.66)]) )
def grafico1(): #sns.jointplot(x='SepalLengthCm',y='SepalWidthCm',data=iris) grid = sns.JointGrid(x='SepalLengthCm', y='SepalWidthCm', data=iris) g = grid.plot_joint(sns.scatterplot, hue='Species', data=iris) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-setosa', 'SepalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-versicolor', 'SepalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-virginica', 'SepalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-setosa', 'SepalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-versicolor', 'SepalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-virginica', 'SepalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) plt.show() #sns.jointplot(x='PetalLengthCm',y='PetalWidthCm',data=iris) grid = sns.JointGrid(x='PetalLengthCm', y='PetalWidthCm', data=iris) g = grid.plot_joint(sns.scatterplot, hue='Species', data=iris) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-setosa', 'PetalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-versicolor', 'PetalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-virginica', 'PetalLengthCm'], ax=g.ax_marg_x, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-setosa', 'PetalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-versicolor', 'PetalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) sns.kdeplot(iris.loc[iris['Species'] == 'Iris-virginica', 'PetalWidthCm'], ax=g.ax_marg_y, vertical=True, legend=False) plt.show()
def plot_dist(): df = np.log10(rv_planets.loc[:, ['pl_msinij', 'pl_orbsmax'] ]) df.columns = ['m', 'a'] df.m += np.log10(u.Mjup.to('Mearth')) x = 'm'; y = 'a' xlim = df.m.describe()[['min','max']] + np.array([-0.15, .15]) ylim = df.a.describe()[['min','max']] + np.array([-0.05, .05]) #hexbin params C = None; mincnt=1 n_colors=5; vmin = vmax = None cbar_label = 'Counts' g = sns.JointGrid(data=df, x=x, y=y, xlim=xlim, ylim=ylim) g.set_axis_labels(xlabel=r'log $m$ ($M_\oplus$)', ylabel='log $a$ (au)') cmap = ListedColormap(sns.color_palette('Blues_d', n_colors).as_hex()[::-1]) g.plot_joint(plt.hexbin, gridsize=50, mincnt=mincnt, C=C, vmin=vmin, vmax=vmax, cmap=cmap) g.plot_marginals(sns.distplot, hist=False, kde=True, rug=False, kde_kws={'shade': True})#, kde_kws={'cut':0, 'bw':0.4}) cax = g.fig.add_axes([1, .095, .03, .75]) cbar = plt.colorbar(cax=cax) cbar.ax.set_ylabel(cbar_label) cbar.set_ticks([1,2,3,4,5]) g.savefig('../figures/real_world_dist.png') plt.show()
def scatter_plot_with_missing_completed(X_obs, X_complete, replace_nan_by_min=True): # scatter plot with historgams, nan are completed by its true value row_with_missing = [any(np.isnan(x)) for x in X_obs] # use X_complete informations values_miss_0 = X_complete[:, 0][np.isnan(X_obs[:, 0])] values_miss_1 = X_complete[:, 1][np.isnan(X_obs[:, 1])] X_missing = X_complete[row_with_missing] g = sns.JointGrid(X_obs[:, 0], X_obs[:, 1]) # g.set_axis_labels('X [:,0]','X [:,1]') # g.set_ylabel('X[:,1]') sns.kdeplot(X_obs[:, 0][~np.isnan(X_obs[:, 0])], ax=g.ax_marg_x) sns.kdeplot(X_obs[:, 1][~np.isnan(X_obs[:, 1])], ax=g.ax_marg_y, vertical=True, label='observed') g.ax_joint.plot(X_obs[:, 0], X_obs[:, 1], "o", alpha=.7) sns.kdeplot(X_missing[:, 0], ax=g.ax_marg_x) sns.kdeplot(X_missing[:, 1], ax=g.ax_marg_y, vertical=True, label='missing (completed)') g.ax_joint.plot(X_missing[:, 0], X_missing[:, 1], "*", alpha=.7) plt.legend() plt.tight_layout()
def plot_dist(x, y): ''' plotting environment we frequently use to visualise the sampling algorithms.''' # style for seaborn sns.set(style="ticks", color_codes=True) # plot contour g = (sns.JointGrid(x, y, size=10).plot_joint(sns.kdeplot, n_levels=6, cmap="BuPu", shade=True, shade_lowest=True)) # plot marginals g = g.plot_marginals(sns.kdeplot, color="b", shade=True, shade_lowest=True) # add scatter plot of data g = g.plot_joint(plt.scatter, c="b", s=30, linewidth=1) plt.plot( x, y, linestyle='-', marker='o', alpha=0.4, ) # set axis labels g.set_axis_labels("$X$", "$Y$") plt.show(g)
def snsscatter_2vars(options, pb, pa, obsReal, var1, var2, cl, savefig=False): b1 = pb.stack[var1][cl, :] b2 = pb.stack[var2][cl, :] o1 = obsReal.data[var1][cl] o2 = obsReal.data[var2][cl] a1 = pa.stack[var1][cl, :] a2 = pa.stack[var2][cl, :] p = sns.JointGrid(x=b1, y=b2) p = p.plot_joint(plt.scatter, s=40, alpha=0.5) plt.scatter(o1, o2, color='m', s=100, marker='*') p.ax_marg_x.hist(b1, alpha=0.5, color='b') p.ax_marg_y.hist(np.ma.masked_invalid(b2).compressed(), orientation='horizontal', alpha=0.5) p.ax_marg_x.hist(np.ma.masked_invalid(a1).compressed(), alpha=0.5, color='g') p.ax_marg_y.hist(np.ma.masked_invalid(a2).compressed(), orientation='horizontal', alpha=0.5, color='g') plt.scatter(a1, a2, color='g', alpha=0.5, s=40) plt.xlabel('class ' + str(cl) + ' ' + str(var1)) plt.ylabel('class ' + str(cl) + ' ' + str(var2)) plt.suptitle(cl) if savefig: plt.savefig('pie/snsscatter_' + var1 + '_' + var2 + '_' + str(cl) + '_' + str(options.dates) + '.png') plt.close()
def joint_plot(df_in, x, y, sample=None, height=10): df = df_in.sample(sample) if sample else df_in g = sns.JointGrid(x=x, y=y, data=df, height=height) g.plot_joint(corrfunc) # pearson correlation coeficient g.plot_joint(sns.regplot, scatter_kws={"s": 12}, line_kws={'color': 'tab:grey'}) g.plot_joint(sns.kdeplot, cmap="Reds", alpha=0.4) g.plot_marginals(sns.distplot, hist_kws={'edgecolor': 'w'})
def plot_stime(self, drugnames, filename=None, save=True): """ scatterplot of individual survival time """ #if ax is None: # ax = plt.gcf().gca() A = 't_' + drugnames[0] B = 't_' + drugnames[1] if A not in self._tdata.columns: self.gen_stime(drugnames[0]) if B not in self._tdata.columns: self.gen_stime(drugnames[1]) rho, p = scipy.stats.spearmanr(self._tdata[A], self._tdata[B]) xmax = np.max(self._tdata[[A, B]].max().values) g = sns.JointGrid(x=A, y=B, data=self._tdata, xlim=(-5, xmax + 5), ylim=(-5, xmax + 5)) g = g.plot_joint(sns.regplot) ax = plt.gcf().gca() ax.plot([0, xmax], [0, xmax], 'k-.', alpha=0.8) g = g.plot_marginals(sns.distplot, bins=int(xmax / 3)) g.annotate(scipy.stats.spearmanr) if filename is None: filename = 'corr_plot' + '_'.join( drugnames) + '_{:.4f}.pdf'.format(rho) if save: g.savefig(filename, dpi=150)
def main(): # change the line below to load an alternative data set (see the ../data directory) data_file = "../data/E14-properties.csv" print "Loading: ", data_file property_data = pd.read_csv(data_file) # remove entries where the type is unknown, to simplify things property_data = property_data[property_data.Type != "Unknown"] print "Dataset has been loaded with {0} rows".format(len(property_data)) # first, let's see the relationship between bedrooms and price, with a poly line of best fit g = sns.JointGrid(x="Bedrooms", y="Price", data=property_data) g.plot_joint(sns.regplot, order=2) g.plot_marginals(sns.distplot) # now let's use a Pairplot to see the interactions between variables g = sns.pairplot(property_data[["Price", "Bedrooms", "Remoteness", "Type"]], hue="Type", diag_kind="hist") for ax in g.axes.flat: plt.setp(ax.get_xticklabels(), rotation=45) # PairGrid gis like pairplot, but lets us control the individual plot types separately g = sns.PairGrid(property_data[["Price", "Bedrooms", "Remoteness", "Type"]], hue="Type") g.map_upper(sns.regplot) # Upper panels show Regression Lines g.map_lower(sns.residplot) # Lower Panels show Residuals g.map_diag(plt.hist) for ax in g.axes.flat: plt.setp(ax.get_xticklabels(), rotation=45) g.add_legend() g.set(alpha=0.5)
def _jointplot(x, y, data, xbins=None, ybins=None, xscale='linear', yscale='linear', annotate=False): if xbins is None: xlim = None else: xlim = (np.min(xbins), np.max(xbins)) if ybins is None: ylim = None else: ylim = (np.min(ybins), np.max(ybins)) g = sns.JointGrid(x, y, data, xlim=xlim, ylim=ylim) _ = g.ax_marg_x.hist(data[x], color='b', alpha=0.6, bins=xbins) _ = g.ax_marg_y.hist(data[y], color='r', alpha=0.6, orientation='horizontal', bins=ybins) g.plot_joint(plt.scatter, color='gray', edgecolor='black', alpha=0.6) ax = g.ax_joint ax.set_xscale(xscale) ax.set_yscale(yscale) g.ax_marg_x.set_xscale(xscale) g.ax_marg_y.set_yscale(yscale) if annotate: g = g.annotate(stats.pearsonr) return g
def circle_count_limiting( aggregate_df: pd.DataFrame, count_constraints: Sequence[int], param: str, reference_value_dict: Dict[str, float], example=True, ): """ Limit circle count and plot result for param. """ for count_constraint in count_constraints: df_constrained = aggregate_df.loc[ aggregate_df["circle_count"] <= count_constraint ] g = sns.JointGrid( data=df_constrained, x="area", y=param, ) g.plot(sns.scatterplot, sns.histplot) plt.title(f"<= {count_constraint} circles") if param in reference_value_dict: g.ax_joint.axhline(reference_value_dict[param], linestyle="--") if example: break
def histogram(ts): # Completed """ Computes and draws the histogram of the given time series. Plot the histogram vertically and side to side with a plot of the time series. """ ts = ts_to_list(ts) data = ts datax = [] for i in range(len(data)): # building the x-axis coordinates datax.append(i) sns.set() # JointGrid builds a grid of subplots (i.e multiple data reps.) g = sns.JointGrid(data=data, x=datax, y=data) g.ax_marg_y.set_axis_off() plt.subplots_adjust(top=.92) # Buffer for title g.fig.suptitle("Histogram") # Title # Plots a histogram and line representation. g.plot(sns.lineplot, sns.histplot, color="r") plt.grid() plt.show() ts = list_to_ts(ts) return ts
def correlate(method, df_demog, df_data, y_axis, savename, filename): #filename manip df_plot = pd.concat([df_data['Dist'], df_data['Group'], df_demog[y_axis]], axis=1) df_plot.dropna(inplace=True) df_HC = df_plot[df_plot['Group'] == 0] df_pat = df_plot[df_plot['Group'] != 0] df_pat[df_pat.columns.difference(["Group"])].columns c, p = stats.spearmanr(df_pat['Dist'], df_pat[y_axis]) r = r'$\rho$ = ' if method == "Pearson": c, p = stats.pearsonr(df_pat['Dist'], df_pat[y_axis]) r = 'r =' r_value = np.round(c, 2) p_value = np.round(p, 3) fig, ax = plt.subplots(1,1,figsize=(4,4)) sns.set(style='white', font_scale=1.5) g = sns.JointGrid(x=df_pat['Dist'], y=df_pat[y_axis]) g = g.plot_joint(sns.regplot, color="#c54630") plt.xlabel("Anomaly score",size=28) plt.ylabel(y_axis,size=28) g = g.plot_marginals(sns.distplot, kde=False, bins=12, color="#c54630") g.fig.text(0.65, 0.22,r+ str(r_value)+'\np = '+str(p_value), fontsize=16) #add text #g.fig.text(0.44, 0.80,"Group", fontsize=22) #add text plt.tight_layout() g.savefig('figures/'+y_axis+'_'+savename+'.png', dpi=200) st.write(g.fig) plt.close(fig)
def plot_tth_v_n_frames(self, save_path): scatter_outcomes_tth_v_n_frames = { 'trial_id': [], 'n_frames': [], 'tth': [] } for vo in self.videos: scatter_outcomes_tth_v_n_frames['trial_id'].append( vo.get_video_id()) scatter_outcomes_tth_v_n_frames['tth'].append( vo.outcomes_data['Trial TTH']) scatter_outcomes_tth_v_n_frames['n_frames'].append( vo.get_video_length()) outcomes_compare_df = pd.DataFrame(scatter_outcomes_tth_v_n_frames) # ax = scatterplot(data=outcomes_compare_df, x='tth', y='n_frames') grid = sns.JointGrid(outcomes_compare_df.tth, outcomes_compare_df.n_frames, space=0, size=6, ratio=50) grid.plot_joint(plt.scatter) plt.plot([0, 300], [0, 250], linewidth=2) grid.savefig(save_path)
def plot_p_vs_median_jointplot(df, text, file_path): jt_plt = sns.JointGrid(height=10, ratio=5, xlim=(-1.0, 1), ylim=(-0.02, 1.02)) x, y = df["median_scores"], df["p_values"] sns.scatterplot(x=x, y=y, ax=jt_plt.ax_joint, hue=df["dose"]) sns.histplot(x=x, multiple="stack", hue=df["dose"], ax=jt_plt.ax_marg_x, legend=False) sns.histplot(y=y, multiple="stack", hue=df["dose"], ax=jt_plt.ax_marg_y, legend=False) jt_plt.fig.suptitle("P-values vs Median scores for " + text, size=16) jt_plt.fig.subplots_adjust(top=.95) jt_plt.ax_joint.axhline(0.05, ls='--', c='black') jt_plt.ax_joint.text(-0.80, 0.07, "Significance level (0.05)") jt_plt.set_axis_labels("Median scores of pairwise correlation btw cpds", "Non-parametric P-values") plt.savefig(file_path) plt.show()
def plot_2d(): x, y = np.random.normal(loc=0, scale=1, size=(2, 10000)) import seaborn as sns g = sns.JointGrid(x=x, y=y, size=4) g.plot_joint(sns.kdeplot, cmap="Purples_d") g.plot_marginals(sns.kdeplot, color="m", shade=True) plt.show()
def scatter_plot(TPM_log, TPMs, name_1, name_2, lim_plot, organism): g1 = sns.JointGrid('rep1', y='rep2', data=TPM_log, height=6) g1 = g1.plot_joint(plt.scatter, edgecolor="black", linewidth=0.5) # calculate pearson's correlation coefficient stat = lambda a, b: stats.pearsonr(TPMs.rep1, TPMs.rep2) # add label with pearson's correlation coefficient g1 = g1.annotate( stat, template="{stat}: {val:.4f}", stat="Pearson's r", loc="upper left", fontsize=15) # {stat}: {val:.2f} (p = {p:.3g}) with p-value g1.ax_marg_x.set_axis_off() g1.ax_marg_y.set_axis_off() plt.xlabel(r'$\mathrm{log_{10}TPM}$' + '\n' + '\n' + name_1.split('_TPM')[0], fontsize=15, labelpad=5) plt.ylabel(name_2.split('_TPM')[0] + '\n' + '\n' + r'$\mathrm{log_{10}TPM}$', fontsize=15, labelpad=5) plt.xlim(-0.15, lim_plot) plt.ylim(-0.15, lim_plot) plt.title(organism, fontsize=15, fontweight="bold") plt.tick_params(axis="both", labelsize=15) plt.subplots_adjust(bottom=0.1) # save plot plt.savefig('scatter_plot_' + name_1 + '_' + name_2 + '_' + organism + '.pdf', dpi=300, bbox_inches='tight') plt.close(plt.gcf())
def plot_generalization_error(train_cors, test_cors, args): plot_min = min(train_cors.min().min(), test_cors.min().min()) - 0.01 if np.min(train_cors.values) > 0.999: train_cors += np.random.randn(train_cors.shape[1]) / 500 g = sns.JointGrid(train_cors.values.flatten(), test_cors.values.flatten(), xlim=(plot_min, 1.01), ylim=(plot_min, 1.01), height=9) g = g.plot_joint(sns.kdeplot, shade=True, shade_lowest=False, bw=0.01, cut=0) g = g.plot_marginals(sns.distplot, kde=False) g.ax_joint.tick_params(pad=3.9) g.ax_joint.plot([-1, 2], [-1, 2], linewidth=1.7, linestyle='--', color='#550000', alpha=0.6) g.ax_joint.set_xlabel('Training Correlation', fontsize=22, weight='semibold') g.ax_joint.set_ylabel('Testing Correlation', fontsize=22, weight='semibold') g.savefig( os.path.join(plot_dir, '__'.join([args.expr_source, args.cohort]), args.model_name.split('__')[0], "{}__generalization.svg".format( args.model_name.split('__')[1])), dpi=300, bbox_inches='tight', format='svg' ) plt.close()
def level_scatter_chart(self, x_name, y_name, data, isdownload=False, download_path=None): plt.figure(figsize=(15.0, 10.0)) g = sns.JointGrid(x=x_name, y=y_name, data=data) g = g.plot_joint(plt.scatter, color='g', s=40, edgecolor='white', alpha=.5) # 绘制散点图 plt.grid(linestyle='--') # 网格 g.plot_marginals(sns.distplot, kde=True, color='g') if isdownload: # 要放到 show() 前 if download_path is None: download_path = self.all_download_path else: # 判断路径是否存在,不存在先新建 z = download_path.count("/") if z <= 1: pass else: file_d = download_path.replace( f"{download_path.split('/')[-1]}", '') if not os.path.exists(file_d): # 新建文件夹 os.makedirs(file_d) plt.savefig(download_path) print('保存成功{}'.format(download_path)) plt.show()
def plot_bivariates(stats): colors = sns.husl_palette(stats['rider id'].max() + 1, l=.7) riders = np.unique(stats['rider id']) proxy_lines = [] for rid in riders: c = colors[rid - 1] l = matplotlib.lines.Line2D([], [], linestyle='', marker='o', markerfacecolor=c, label='rider {}'.format(rid)) proxy_lines.append(l) grids = [] for yf in yfields[:-1]: name, unit = yf x = stats['starting velocity'] y = stats[name] g = sns.JointGrid(x=x, y=y) g.plot_marginals(sns.distplot, kde=False, color=sns.xkcd_palette(['charcoal'])[0]) g.plot_joint(plt.scatter, color=list(map(lambda x: colors[x - 1], stats['rider id']))) g.ax_joint.legend(handles=proxy_lines, ncol=2, title='pearson r = {:.2g}, p = {:.2g}'.format( *scipy.stats.pearsonr(x, y))) g.set_axis_labels('starting velocity [m/s]', '{} [{}]'.format(name, unit)) g.fig.suptitle('scatterplots of steering events') g.fig.set_size_inches(12.76, 7.19) # fix size for pdf save grids.append(g) return grids
def plot(tig_table, taxa_level, max_depth, out_prefix, min_depth): if max_depth: plotdf = tig_table[tig_table["Depth"] < max_depth] plotdf = tig_table[tig_table["Depth"] > min_depth] else: plotdf = tig_table[tig_table["Depth"] > min_depth] grid = sns.JointGrid(x='GC', y='Depth', data=plotdf) taxalist = list(set(list(plotdf[taxa_level]))) taxalist.sort() g = grid.plot_joint(sns.scatterplot, hue=taxa_level, data=plotdf, markers=["."]) for taxon in taxalist: sns.distplot(plotdf.loc[plotdf[taxa_level] == taxon, "Depth"], kde=False, ax=g.ax_marg_y, vertical=True, bins=range(0, int(max(plotdf["Depth"])), 3), axlabel=False) sns.distplot(plotdf.loc[plotdf[taxa_level] == taxon, 'GC'], ax=g.ax_marg_x, bins=np.arange(0.1, 0.8, 0.01), kde=False, axlabel=False) g.savefig(out_prefix + ".plot.png")
def buildKDEPlots(embedded_array, n_trial_data, min_nFrames): start = 0 for i, (strain, nFlies) in enumerate(n_trial_data.items()): print(f'{i+1}/{len(n_trial_data)}') nPoints = min_nFrames * nFlies range_i = range(start, start + nPoints) # Get TSNE data for current strain embedded_strain = embedded_array[range_i, :] # Create figure plt.figure() # Plot general joint distribution g = sns.JointGrid(embedded_array[:, 0], embedded_array[:, 1]) g.plot_joint(sns.kdeplot) # Show distribution of given strain sns.kdeplot(embedded_strain[:, 0], embedded_strain[:, 1], shade=True, cmap="Reds") # Save figure g.fig.suptitle(strain) plt.savefig('kde_' + strain + '.png') # Update range of points to be taken for next strain start += nPoints
def _joint_grid(col_x, col_y, col_k, df, k_is_color=False, scatter_alpha=.85): def colored_kde(x, y, c=None): def kde(*args, **kwargs): args = (x, y) if c is not None: kwargs['c'] = c kwargs['alpha'] = scatter_alpha sns.kdeplot(*args, **kwargs) return kde g = sns.JointGrid(x=col_x, y=col_y, data=df) color = None legends = [] for name, df_group in df.groupby(col_k): legends.append(name) if k_is_color: color = name g.plot_joint(colored_kde(df_group[col_x], df_group[col_y], color), ) sns.kdeplot(df_group[col_x].values, ax=g.ax_marg_x, color=color, shade=True) sns.kdeplot(df_group[col_y].values, ax=g.ax_marg_y, color=color, shade=True, vertical=True) plt.legend(legends)
def makejoint(x, y, color, title): '''Plot x vs y where x are experimental values and y are predictions''' color_rgb = matplotlib.colors.colorConverter.to_rgb(color) colors = [ seaborn.utils.set_hls_values(color_rgb, l=l) for l in np.linspace(1, 0, 12) ] cmap = blend_palette(colors, as_cmap=True) g = sns.JointGrid(x=x, y=y) #g = g.plot_joint(plt.hexbin, cmap=cmap) g = g.plot_joint(plt.scatter, marker='.', color=color, alpha=.25, s=2) g = g.plot_joint(sns.kdeplot, shade=True, cmap=cmap, shade_lowest=False) g = g.plot_marginals(sns.distplot, color=color) g.ax_joint.set_xlabel('Experiment') g.ax_joint.set_ylabel('Prediction') plt.suptitle(title, verticalalignment='center') s = scipy.stats.spearmanr(x, y)[0] rmse = np.sqrt(np.mean(np.square(x - y))) ax = g.ax_joint ax.set_ylim(0, 12) ax.set_xlim(0, 12) #ax.plot(x,y,'.',color=color,alpha=.25,markersize=2) ax.text(0.5, 0.05, 'Spearman = %.3f, RMSE = %.3f' % (s, rmse), fontsize=18, transform=ax.transAxes, horizontalalignment='center') plt.savefig('%s.pdf' % title, bbox_inches='tight')
def plot(self, regression=True, seaborn=False, ax=None, title=None, name=None, **scatter_kw): if ax is not None: self.ax = ax title = name or self.name or title if seaborn: import seaborn as sns sns.set_context(rc={"figure.figsize": (7, 5)}) g = sns.JointGrid(self.got_label, self.expect_label, data=self.data) g.plot(sns.regplot, sns.distplot, spearmanr) print("Pearson's r: {0}".format(self.pearson)) else: if self.ax is None: self.ax = pl.figure().add_subplot(111) self.ax.scatter(self.got, self.expect, lw=0, alpha=0.5, **scatter_kw) self.ax.set_xlabel(self.got_label) self.ax.set_ylabel(self.expect_label) # Keeps the plot region tight against the data (allow 5% of the # data-range for padding so that points in the scatter plot aren't # partially clipped.) xeps = 0.05 * self.got.ptp() self.ax.set_xlim(self.got.min() - xeps, self.got.max() + xeps) yeps = 0.05 * self.expect.ptp() self.ax.set_ylim(self.expect.min() - yeps, self.expect.max() + yeps) if title is not None: self.ax.set_title(title) if regression: self.regression_line() return self
def joint_plot(filename, df, x, y, max_samples=None, alpha=0.5, xlim=None, ylim=None): """ Plot scatterplot from a dataframe column pair and optionally save the plot """ if max_samples is not None: df = df.sample(min(len(df.index), max_samples)) g = sns.JointGrid() x, y = df[x], df[y] sns.scatterplot(x=x, y=y, alpha=alpha, marker='+', ax=g.ax_joint) sns.kdeplot(x=x, ax=g.ax_marg_x) sns.kdeplot(y=y, ax=g.ax_marg_y) xlabel = get_label(x.name) ylabel = get_label(y.name) g.set_axis_labels(xlabel, ylabel) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) if filename is not None: plt.savefig(filename, bbox_inches='tight') plt.close()
def JointPlotTest(model, img_ids, log_df,img_dim, batch_size=256,title="", scale = False): np.random.seed(0) test_generator = DataGenerator( img_ids, log_df, batch_size=batch_size, dim=img_dim, shuffle=False ) scaler = preprocessing.StandardScaler(with_mean=False).fit( log_df.loc[train_img_ids, 'steering'].values.reshape((-1, 1))) test_img, test_commands = test_generator.__getitem__(0) # returns batch 0 if scale: test_preds = scaler.inverse_transform([model.predict(test_img)]) else: test_preds = model.predict(test_img) # https://seaborn.pydata.org/generated/seaborn.JointGrid.html#seaborn.JointGrid test_data = pd.DataFrame(np.transpose([test_commands, test_preds.reshape(-1)])) test_data.columns = ["Ground truth steering commands", "Predicted steering commands"] grid = sns.JointGrid(x="Ground truth steering commands", y="Predicted steering commands", data=test_data, xlim=(-1, 1), ylim=(-1, 1)) grid = grid.plot_joint(sns.regplot) grid.plot_marginals(sns.distplot, kde=False) grid.annotate(metrics.mean_squared_error, template="{stat}: {val:.4f}", stat="$MSE$"); plt.subplots_adjust(top=0.9) grid.fig.suptitle(title + " regression plot") print("===============================================================") print(title + " metrics") print("MSE = ", metrics.mean_squared_error(test_commands,test_preds)) print("MAE = ", metrics.mean_absolute_error(test_commands, test_preds)) print("===============================================================")
def plt_draw_2D(data, str_t, str_x, str_y): index = [str_x, str_y] diction = {index[i]: data[:, i] for i in range(2)} sns.JointGrid(str_x, str_y, diction).plot(sns.regplot, sns.distplot) plt.xlim((None, None)) plt.ylim((None, None)) plt.show()
def multi_joint_plot(col_x, col_y, col_k, df, scatter_alpha=.5, palette='Blues'): """ seaborn joint plot for multiple data sets plotted separately adapted from: https://stackoverflow.com/questions/35920885/how-to-overlay-a-seaborn-jointplot-with-a-marginal-distribution-histogram-fr """ def colored_scatter(x, y, c): def scatter(*args, **kwargs): args = (x, y) kwargs['c'] = c kwargs['alpha'] = scatter_alpha kwargs['s'] = 1 plt.scatter(*args, **kwargs) return scatter g = sns.JointGrid(x=col_x, y=col_y, data=df) unique_labels = df.loc[:, col_k].unique() unique_labels.sort() colors = sns.color_palette(palette, len(unique_labels)) legends = [] for ui, ul in enumerate(unique_labels): legends.append(ul+1) df_group = df.loc[df.loc[:, col_k] == ul, :] color = colors[ui] g.plot_joint(colored_scatter(df_group[col_x], df_group[col_y], color)) try: sns.distplot(df_group[col_x].values, ax=g.ax_marg_x, color=color, hist=False) sns.distplot(df_group[col_y].values,ax=g.ax_marg_y, color=color, vertical=True, hist=False) except: print('Cannot plot distributions in scatterplot due to statsmodels bug') # # Do also global Hist # sns.distplot(df[col_x].values, ax=g.ax_marg_x, color='grey') # sns.distplot(df[col_y].values.ravel(), ax=g.ax_marg_y, color='grey', vertical=True) plt.legend(legends)
def sns_demo2(): # plot_joint() + ax_marg_x.hist() + ax_marg_y.hist() # 设置风格 sns.set_style('white') # 导入数据 tips = sns.load_dataset('tips') print(tips.head()) # 创建一个绘图表格区域,设置好x,y对应数据 g = sns.JointGrid(x='total_bill', y='tip', data=tips) g.plot_joint(plt.scatter, color='m', edgecolor='white') # 设置框内图表,scatter # g = g.plot_joint(plt.scatter, color='g', s=40, edgecolor='white') # 绘制散点图 # g = g.plot_joint(sns.kdeplot, cmap = 'Reds_r') #绘制密度图 g.ax_marg_x.hist(tips['total_bill'], color='b', alpha=.6, bins=np.arange(0, 60, 3)) # 设置x轴为直方图,注意bins是数组 g.ax_marg_y.hist(tips['tip'], color='r', alpha=.6, orientation='horizontal', bins=np.arange(0, 12, 1)) # 设置x轴直方图,注意需要orientation参数 from scipy import stats g.annotate(stats.pearsonr)