def plot_error_covariance(samples, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts P = samples.kalman.P e = samples.x - samples.kalman.x # calculate outer product of error at each timestep E = e * e.transpose(0, 2, 1) _, rows, cols = P.shape color_calc = sns.husl_palette(rows * cols) color_true = sns.husl_palette(rows * cols, l=0.4) fig, axes = plt.subplots(rows, cols, sharex=True) axes = axes.ravel() for n in range(rows * cols): ax = axes[n] i = n // cols j = n % cols ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.set_title('P[{}, {}]'.format(i, j)) ax.plot(t, P[:, i, j], color=color_calc[n], label='estimate') ax.plot(t, E[:, i, j], color=color_true[n], label='true') ax.legend() title = 'Kalman error covariance P' _set_suptitle(fig, title, filename) return fig, axes
def plot_error_covariance(samples, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts P = samples.kalman.P e = samples.x - samples.kalman.x # calculate outer product of error at each timestep E = e * e.transpose(0, 2, 1) _, rows, cols = P.shape color_calc = sns.husl_palette(rows*cols) color_true = sns.husl_palette(rows*cols, l=0.4) fig, axes = plt.subplots(rows, cols, sharex=True) axes = axes.ravel() for n in range(rows*cols): ax = axes[n] i = n // cols j = n % cols ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.set_title('P[{}, {}]'.format(i, j)) ax.plot(t, P[:, i, j], color=color_calc[n], label='estimate') ax.plot(t, E[:, i, j], color=color_true[n], label='true') ax.legend() title = 'Kalman error covariance P' _set_suptitle(fig, title, filename) return fig, axes
def plot_population_at(population, centers, x, y, ax=None, return_patches=False): if ax is None: _, ax = plt.subplots() data = np.array(population) spacing = (centers[1] - centers[0]) * ColorDefaults.bar_spacing label = as_plot_title(population) try: points = zip(x, y) except TypeError: points = [[x, y]] colors = sns.husl_palette(1) else: colors = sns.husl_palette(len(x)) # Show response histogram ax.set_title("Population response: {}".format(label)) ax.set_xlabel("Tuning center") ax.set_ylabel("Activity") ax.set_xlim((np.min(centers), np.max(centers))) lines = [] bars = [] for (x, y), c in zip(points, colors): patches = ax.bar(centers, data[:, x, y], width=spacing, color=c, alpha=ColorDefaults.bar_alpha, label="{x}, {y}".format(x=x, y=y)) ax.legend() # Show decoded value on x-axis z = circular_decoder(population, centers, x, y) line, = ax.plot([z], [0], color=c, marker=11, markersize=11, clip_on=False, transform=BlendedGenericTransform( ax.transData, ax.transAxes)) lines.append(line) bars.append(patches) ax.set_xticks(np.linspace(0, np.pi, 8)) ax.set_xticklabels(TICKLABELS_0_PI) if return_patches: return ax, lines, bars else: return ax
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_distributions_categorical_target(df, target, cols=4, bins=50, alpha=0.3): num_vars = df.select_dtypes('number').columns if (len(num_vars) // cols) != 0: rows = (len(num_vars) // cols) + 1 else: rows = (len(num_vars) // cols) target_uniques = np.unique(df[target].values) palette = sns.husl_palette(len(target_uniques)) palette_dict = dict(zip(target_uniques, palette)) fig, ax = plt.subplots(rows, cols, figsize=(20, 10)) for variable, subplot in zip(num_vars.tolist(), ax.flatten()): for target_unique in target_uniques: sns.distplot( df.loc[lambda d: d[target] == target_unique][variable], ax=subplot, bins=bins, color=palette_dict[target_unique], hist_kws=dict(alpha=alpha)) fig.legend(labels=target_uniques) plt.tight_layout() plt.show()
def add_legend(df, ax=None): if ax is None: ax = plt.gca() dt = np.mean(df.index[1:] - df.index[:-1]) / np.timedelta64(1, 's') mapping, transmat = compute_transition_matrix(df.state) palette = sns.husl_palette(df.state.max(), l=0.7, s=.9) artists, labels = [], [] for state in sorted(df.state.unique()): # Ignore division by zero (handled later) with warnings.catch_warnings(): warnings.simplefilter('ignore') avg_duration = ( 1 / (1 - transmat[mapping[state], mapping[state]])) * dt if avg_duration == math.inf: avg_duration_str = '-' else: avg_duration_str = td_format(dt.timedelta(seconds=avg_duration)) artists.append( plt.Rectangle((0, 0), 1, 1, color=palette[state - 1], alpha=0.6)) labels.append('State {}\nAvg. duration\n{}'.format( state, avg_duration_str)) ax.legend(artists, labels, bbox_to_anchor=(0.5, -0.35), ncol=len(artists), loc='center')
def palette_generator(items): """ Generate a list of colors against 'husl' color separation against the number of keys given """ palette = sns.husl_palette(len(items)) # Convert colors from decimal to 0-255 rgb_palette = [ tuple(int(255 * channel) for channel in color) for color in palette ] # Convert to hex code and return hex_palette = ['#%02x%02x%02x' % color for color in rgb_palette] # Add distance between the colours by splitting the list and zipping them middle_index = len(items) // 2 keys = [] item_keys = list(items.keys()) for i in range(middle_index): keys.append(item_keys[i]) keys.append(item_keys[middle_index + i]) if len(items) % 2: keys.append(item_keys[-1]) print(items) palette_key = dict(zip(keys, hex_palette)) print(palette_key) return palette_key
def scatter(x, labels): labels_set = list(set(labels)) colors = np.array([labels_set.index(x) for x in labels]) palette = np.array(sns.husl_palette(len(labels_set))) f = plt.figure(figsize=(10, 10)) ax = plt.subplot(aspect='auto') sc = ax.scatter(x[:, 0], x[:, 1], lw=0, s=40, c=palette[colors.astype(np.int)]) plt.xlim(-50, 50) plt.ylim(-50, 50) ax.axis('off') ax.axis('tight') legends = [] leg_labels_processed = [] for i in range(len(colors)): if not labels[i] in leg_labels_processed: rgb = palette[colors.astype(np.int)[i]] legend = mpatches.Patch(color=rgb.tolist(), label=labels[i]) legends.append(legend) leg_labels_processed.append(labels[i]) lgd = plt.legend(handles=legends, bbox_to_anchor=[1, 1], fontsize=12) plt.savefig('./viz.png', dpi=120, bbox_extra_artists=(lgd, ), bbox_inches='tight')
def _get_colors(index, column, palette): """This method gets the colors for each index value. # Get species vals1, pal1, row_colors = _get_colors(dataframe.index, self.c_gen, 'nipy_spectral') vals2, pal2, col_colors = _get_colors(dataframe.columns, self.c_cat, 'tab20b') Parameters ---------- index : column : palette: Returns ------- """ # Get species species = index.get_level_values(column).unique().sort_values() netwpal = ['gray'] + sns.husl_palette(len(species), s=.45) #netwpal = ['gray'] + list(sns.color_palette('Set2', len(species)+1, .75)) #netwpal = ['gray'] + list(sns.color_palette(palette, len(species)+1, .45)) netwmap = dict(zip(map(str, species), netwpal)) # Convert the palette to vectors that will be drawn on the side of the matrix networks = index.get_level_values(column) network_colors = pd.Series(networks, index=index).map(netwmap) # Return return species, netwpal, network_colors
def _get_category_colors(values, cmap='tab20b', default='gray'): """This method creates the colors for the different elements in categorical feature vector. Parameters ---------- values : array-like The vector with the categorical values cmap: string-like The colormap to use default: string-like The color to be used for the first value. Note that this value needs to appear first on the the sorted list, as such it is recommended to set is as _default. Returns ------- """ # Get unique elements unique = np.unique(values) # Sort unique values unique.sort() # Create the palette (gray for _na) palette = [default] + sns.husl_palette(len(unique), s=.45) # Create mappers from category to color mapper = dict(zip(map(str, unique), palette)) # Create list with colors for each category. colors = pd.Series(values).map(mapper) # Return return colors
def save_br_chart(self, column, path): if type(self.woe_dicts[column].items()[0][0]) == str: woe_lists = sorted(self.woe_dicts[column].items(), key=self.sort_dict) else: woe_lists = sorted(self.woe_dicts[column].items(), key=lambda item: item[0]) tick_label = [i[0] for i in woe_lists] counts = [i[1][1] for i in woe_lists] br_data = [i[1][2] for i in woe_lists] x = range(len(counts)) fig, ax1 = plt.subplots(figsize=(12, 8)) my_palette = sns.color_palette(n_colors=100) sns.barplot(x, counts, ax=ax1, palette=sns.husl_palette(n_colors=20, l=.7)) plt.xticks(x, tick_label, rotation=30, fontsize=12) plt.title(column, fontsize=18) ax1.set_ylabel('count', fontsize=15) ax1.tick_params('y', labelsize=12) ax2 = ax1.twinx() ax2.plot(x, br_data, color='black') ax2.set_ylabel('bad rate', fontsize=15) ax2.tick_params('y', labelsize=12) plot_margin = 0.25 x0, x1, y0, y1 = ax1.axis() ax1.axis((x0 - plot_margin, x1 + plot_margin, y0 - 0, y1 * 1.1)) plt.savefig(path)
def plot2d_labels(X, labels, cluster_centers=np.empty(()), dpi=80, kmarkersize=10): """Create figure with a scatter plot of X colored by 'labels'""" # fig, ax = plt.subplots(nrows=1, ncols=1, figsize=plt.figaspect(0.6), dpi=90, facecolor='w', edgecolor='k') fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(10,10), dpi=dpi, facecolor='w', edgecolor='k') unique_labels = np.unique(labels) n_clusters_ = unique_labels.shape[0] colors = sns.husl_palette(n_clusters_) for k, col in zip(range(n_clusters_), colors): class_members = labels == k plt.plot(X[class_members,0], X[class_members,1], '.', markerfacecolor=tuple(col), markeredgecolor='none', label='Class %i'%(k)) if len(cluster_centers.shape)>0: plt.plot(cluster_centers[k,0], cluster_centers[k,1], 'X', markerfacecolor=tuple(col), markeredgecolor='k', markersize=kmarkersize, label='Class center #%i'%(k)) plt.axis('equal') plt.xlabel('dimension 1') plt.ylabel('dimension 2') plt.title('Number of clusters: %i' % n_clusters_) plt.legend() return fig, ax, colors
def visualizeSubstituentsGrid(mol, aIdx, molSize=(300,150), kekulize=True,): dists = Chem.GetDistanceMatrix(mol) idxChiral = Chem.FindMolChiralCenters(mol)[0][0] subs, sharedNeighbors, maxShell = determineAtomSubstituents(aIdx, mol, dists, False) colors = sns.husl_palette(len(subs), s=.6) mc = rdMolDraw2D.PrepareMolForDrawing(mol, kekulize=kekulize) count=0 svgs=[] labels=[] for sub in sorted(subs.values(), key= lambda x: _getSizeOfSubstituents(x, sharedNeighbors)): color = tuple(colors[count]) count+=1 atColors = {atom: color for atom in sub} bonds = getBondsSubstituent(mol, set(sub)) bnColors = {bond: color for bond in bonds} drawer = rdMolDraw2D.MolDraw2DSVG(molSize[0],molSize[1]) drawer.DrawMolecule(mc,highlightAtoms=atColors.keys(), highlightAtomColors=atColors,highlightBonds=bonds,highlightBondColors=bnColors) drawer.FinishDrawing() svg = drawer.GetDrawingText() svgs.append(svg.replace('svg:','')) labels.append("Substituent "+str(count)+" (#atoms: "+str(len(sub))+", size normed: "+ str(_getSizeOfSubstituents(sub, sharedNeighbors))+")") return _svgsToGrid(svgs, labels, svgsPerRow=len(svgs),molSize=molSize,fontSize=12)
def multiple_notegroup_heatmap(notegroup_list, chromatic=False, yticks=[], title=None): np_notegroup_list = np.array([ pt_utils.binary_notegroup_to_numpy_array(ng) for ng in notegroup_list ]) np_notegroup_list_clr = numpy_matrix_by_circleindex(np_notegroup_list) fig, ax = plt.subplots(figsize=(6, len(np_notegroup_list) / 2.0)) ticknames = pt_naming_conventions.circle_fifth_notes( ) if chromatic == False else pt_naming_conventions.chromatic_notes() # for this... hm... always want it? # if it's NOT chromatic (default) everything has to be reconfigured to be analyzed... # y_ticknames = [] sb.set(font_scale=1.4) sb.heatmap(np_notegroup_list_clr, ax=ax, mask=1 - np_notegroup_list, xticklabels=ticknames, yticklabels=yticks, linewidths=1, cmap=sb.husl_palette(12, h=hue, l=light, s=sat), cbar=False, vmin=0, vmax=1) ax.set_title(title, fontsize=16) plt.show() return np_notegroup_list
def set_plt_style(n): colors = sns.husl_palette(n) markers = ['o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd'] * (int(n / 13) + 1) linestyles = ['-', ':', '-.', '--', ':', '-.', '--'] * (int(n / 7) + 1) return({'color': colors, 'marker': markers, 'linestyle': linestyles})
def gaTrace(data,error,sortedKeys = None,ci_multipler = 1.95,fig=None,ax=None,typ='',colors=None,linestyle='-'): if(sortedKeys is None): sortedKeys = sorted(data.keys()) n = len(sortedKeys) seaborn.set_style('white') seaborn.set_context("paper", font_scale=3, rc={"lines.linewidth": 2.5}) if(colors is None): # colors = seaborn.cubehelix_palette(7, start=1, rot=-5,dark=.1, light=.5) colors = seaborn.husl_palette(n,l=.4) shaded = np.hstack([colors,0.15*np.ones((n,1))]) if(fig is None or ax is None): fig,ax = plt.subplots(figsize=(7,7)) for i,k in enumerate(sortedKeys): u = data[k]['Max fitness'] ci = error[k]['Max fitness']*ci_multipler ax.plot(u,color=colors[i],label=k+typ,linestyle=linestyle) ax.fill_between(data[k]['Generation']-1,u-ci,u+ci,color=shaded[i]) plt.subplots_adjust(left=0.2) # plt.ylim([0.6,1.]) return fig,ax
def analyseAndPlot(data,title='GA fitness summary',expBoxLoc=(50, -0.9),size=(15,9)): aveModelMeans,aveModelCI = runStats(data) sortedKeys = sorted(data.keys()) n = len(sortedKeys) seaborn.set_style('white') seaborn.set_context("paper", font_scale=2.5, rc={"lines.linewidth": 1.5}) # colors = seaborn.cubehelix_palette(7, start=1, rot=-5,dark=.1, light=.5) colors = seaborn.husl_palette(n,l=.4) shaded = np.hstack([colors,0.25*np.ones((n,1))]) fig,ax = plt.subplots(figsize=size) for i,k in enumerate(sortedKeys): u = aveModelMeans[k]['Max fitness'] ci = aveModelCI[k]['Max fitness'] * 1.96 #apply CIs to these ax.plot(u,color=colors[i],label=k) ax.fill_between(aveModelMeans[k]['Generation']-1,u-ci,u+ci,color=shaded[i]) plt.title(title) plt.ylabel('Maximum fitness') plt.xlabel('Generation') plt.legend(loc='lower right') return fig
def multiPlot2(self, indexSelect=None, varSelect=None, wrapNumber=5, compLines=None, save=None): df = self.longData if varSelect is not None: if not isinstance(varSelect, list): varSelect = [varSelect] df = df[df['variable'].isin(varSelect)] if indexSelect is not None: if not isinstance(indexSelect, list): indexSelect = [indexSelect] df = df.loc[df['index'].isin(indexSelect)] indexes = list(df['index'].unique()) colors = sns.husl_palette(len(indexes)).as_hex() if compLines is not None: compVars = list(compLines.columns) dfB = compLines.copy() dfB["Time"] = dfB.index dfB = pd.melt(dfB, id_vars=["Time"], value_vars=compVars) dfB["index"] = -1 df = pd.concat([dfB, df], ignore_index=True) colors.append("#000000") indexes.append(-1) #colors = dict(zip(indexes, colors)) grid = sns.FacetGrid(df, col="variable", col_wrap=wrapNumber, palette=colors) grid.map(sns.lineplot, "Time", "value", "index") if save is not None: grid.savefig(save)
def analyseAndPlot(data, title='GA fitness summary', expBoxLoc=(50, -0.9), size=(15, 9)): aveModelMeans, aveModelCI = runStats(data) sortedKeys = sorted(data.keys()) n = len(sortedKeys) seaborn.set_style('white') seaborn.set_context("paper", font_scale=2.5, rc={"lines.linewidth": 1.5}) # colors = seaborn.cubehelix_palette(7, start=1, rot=-5,dark=.1, light=.5) colors = seaborn.husl_palette(n, l=.4) shaded = np.hstack([colors, 0.25 * np.ones((n, 1))]) fig, ax = plt.subplots(figsize=size) for i, k in enumerate(sortedKeys): u = aveModelMeans[k]['Max fitness'] ci = aveModelCI[k]['Max fitness'] * 1.96 #apply CIs to these ax.plot(u, color=colors[i], label=k) ax.fill_between(aveModelMeans[k]['Generation'] - 1, u - ci, u + ci, color=shaded[i]) plt.title(title) plt.ylabel('Maximum fitness') plt.xlabel('Generation') plt.legend(loc='lower right') return fig
def read_tasks(path): task_frame = pd.DataFrame(columns=["task", "radius", "bd"]) repl_frame = pd.DataFrame(columns=["task", "algo", "replications"]) stat_frame = pd.DataFrame( columns=["task", "algo", "stat", "type", "replications"]) path_frame = pd.DataFrame(columns=["task", "algo", "stat", "path"]) # Go through all lines of all task files taskfiles = find_tasks(path) for taskfile in taskfiles: with open(taskfile) as f: for line in f: task_frame, repl_frame, stat_frame, path_frame = read_line( taskfile, task_frame, repl_frame, stat_frame, path_frame, line) # Create algo / color frame variants = sort_frame(stat_frame)['algo'].drop_duplicates().values algo_frame = pd.DataFrame(data={ 'algo': variants, 'colors': husl_palette(len(variants), l=0.7) }) return task_frame, algo_frame, repl_frame, stat_frame, path_frame
def gaTrace(data, error, sortedKeys=None, ci_multipler=1.95, fig=None, ax=None): if (sortedKeys is None): sortedKeys = sorted(data.keys()) n = len(sortedKeys) seaborn.set_style('white') seaborn.set_context("paper", font_scale=3, rc={"lines.linewidth": 1.5}) # colors = seaborn.cubehelix_palette(7, start=1, rot=-5,dark=.1, light=.5) colors = seaborn.husl_palette(n, l=.4) shaded = np.hstack([colors, 0.25 * np.ones((n, 1))]) if (fig is None or ax is None): fig, ax = plt.subplots(figsize=(7, 7)) for i, k in enumerate(sortedKeys): u = data[k]['Max fitness'] ci = error[k]['Max fitness'] * ci_multipler ax.plot(u, color=colors[i], label=k) ax.fill_between(data[k]['Generation'] - 1, u - ci, u + ci, color=shaded[i]) plt.subplots_adjust(left=0.15) return fig, ax
def make_cluster_heatmap(spectrum, base_dir, filename, grad=False): # Load and prep dataset df = prep_spectrum_df(spectrum, base_dir, filename, grad) del spectrum # Create a categorical palette to identify the networks algo_pal = sns.husl_palette(8, s=0.45)[-2:] algo_lut = dict(zip(["eQ", "DDPG"], algo_pal)) # Convert the palette to vectors that will be drawn on the side of the matrix algos = df.columns.get_level_values("algo") algo_colors = pd.Series(algos, index=df.columns).map(algo_lut) # Draw the full plot sns.clustermap( df.corr(), center=0, cmap="vlag", row_colors=algo_colors, col_colors=algo_colors, # col_cluster=False, linewidths=0.75, figsize=(4,4), ) plt.savefig(os.path.join(base_dir, filename + ".pdf")) plt.savefig(os.path.join(base_dir, filename + ".png")) plt.show()
def plot_boxes(self, peaks): """Draw a boxplot to show the distribution of copes at peaks.""" cope_data = nib.load(self.inputs.cope_file).get_data() peak_spheres = self._peaks_to_spheres(peaks).get_data() peak_dists = np.zeros((cope_data.shape[-1], len(peaks))) for i, peak in enumerate(peaks, 1): sphere_mean = cope_data[peak_spheres == i].mean(axis=(0)) peak_dists[:, i - 1] = sphere_mean with sns.axes_style("whitegrid"): f, ax = plt.subplots(figsize=(9, float(len(peaks)) / 3 + 0.33)) try: # seaborn >= 0.6 sns.boxplot(data=peak_dists, palette="husl", orient="h", ax=ax) labels = np.arange(len(peaks)) + 1 except TypeError: # seaborn < 0.6 pal = sns.husl_palette(peak_dists.shape[1])[::-1] sns.boxplot(peak_dists[:, ::-1], color=pal, ax=ax, vert=False) labels = np.arange(len(peaks))[::-1] + 1 sns.despine(left=True, bottom=True) ax.axvline(0, c=".3", ls="--") ax.set(yticklabels=labels, ylabel="Local Maximum", xlabel="COPE Value") out_fname = op.realpath("peak_boxplot.png") self.out_files.append(out_fname) f.savefig(out_fname, bbox_inches="tight") plt.close(f)
def set_plt_style(n): colors = sns.husl_palette(n) markers = [ 'o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd' ] * (int(n / 13) + 1) linestyles = ['-', ':', '-.', '--', ':', '-.', '--'] * (int(n / 7) + 1) return ({'color': colors, 'marker': markers, 'linestyle': linestyles})
def plot_design_matrix(self, dm, variable, split_by=None, panel=False): import matplotlib.pyplot as plt import seaborn as sns n_cols = min(dm.shape[1], 32) n_axes = dm.shape[2] n_rows = self.dataset.n_vols * 3 * self.dataset.n_runs fig, axes = plt.subplots(n_axes, 1, figsize=(20, 2 * n_axes)) if n_axes == 1: axes = [axes] colors = sns.husl_palette(n_cols) random.shuffle(colors) # To improve discrimination at boundaries for j in range(n_axes): ax = axes[j] min_y, max_y = dm[:n_rows, :, j].min(), dm[:n_rows, :, j].max() for i in range(n_cols): ax.plot(dm[:n_rows, i, j], c=colors[i], lw=2) ax.set_ylim(min_y - 0.05 * min_y * np.sign(min_y), max_y + 0.05 * max_y * np.sign(max_y)) if n_axes > 1: try: ax.set(ylabel=list(self.level_map[split_by].keys())[j]) except: pass title = variable + ('' if split_by is None else ' split by ' + split_by) axes[0].set_title(title, fontsize=16) plt.show()
def plot_kde(df, states=None, ax=None): if ax is None: ax = plt.gca() if states is None: states = df.state.unique() xmin, xmax = df.rtt.min(), df.rtt.max() X = np.arange(xmin - 10, xmax + 10, 0.1) palette = sns.husl_palette(df.state.max(), l=0.7, s=.9) for state in sorted(states): obs = df[df.state == state].rtt.dropna() if len(obs) < 2: continue w = len(obs) / len(df.rtt.dropna()) pdf = w * gaussian_kde(obs)(X) ax.plot(X, pdf, color=palette[state - 1]) ax.fill_between(X, pdf, color=palette[state - 1], alpha=0.2, label='State {}'.format(state)) ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.set_xlim(min(X), max(X)) ax.set_xlabel('RTT (ms)') ax.set_ylabel('Density estimation') ax.legend(frameon=0)
def plot_locations(self, ax=None, lu=None): if self.annotations_loaded == False: return if ax is None: fig, ax = pl.subplots(1, 1, sharex=True, sharey=False, figsize=(20, 5)) else: pl.sca(ax) if lu is None: lu = (self.meta['start'], self.meta['end']) palette = it.cycle(sns.husl_palette()) offsets = self.get_offsets() for ai in range(self.num_annotators): col = next(palette) offset = offsets[ai] for index, rr in slice_df_start_stop(self.locations[ai], lu).iterrows(): pl.plot( [rr['start'], rr['end']], [self.location_targets.index(rr['name']) + offset * 2] * 2, color=col, linewidth=5, alpha=0.5) pl.yticks(np.arange(len(self.location_targets)), self.location_targets) pl.ylim((-1, len(self.location_targets))) pl.xlim(lu)
def plot_locations(self, ax=None, lu=None): if self.annotations_loaded == False: return if ax is None: fig, ax = pl.subplots(1, 1, sharex=True, sharey=False, figsize=(20, 5)) else: pl.sca(ax) if lu is None: lu = (self.meta['start'], self.meta['end']) palette = it.cycle(sns.husl_palette()) offsets = self.get_offsets() for ai in xrange(self.num_annotators): col = next(palette) offset = offsets[ai] for index, rr in slice_df_start_stop(self.locations[ai], lu).iterrows(): pl.plot([rr['start'], rr['end']], [self.location_targets.index(rr['name']) + offset * 2] * 2, color=col, linewidth=5, alpha=0.5) pl.yticks(np.arange(len(self.location_targets)), self.location_targets) pl.ylim((-1, len(self.location_targets))) pl.xlim(lu)
def draw_overview_plot(x, df, hue=None): """Plot bar plot with rentals count and duration divided by a feature defined in arguments""" fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(12, 10)) palette = sns.husl_palette(10, h=.5) sns.barplot(x=x, y='rental_id', hue=hue, data=df, ax=ax1, ci=None, palette=palette) ax1.set_title('Total number of rentals') ax1.set_ylabel('number of rentals') sns.barplot(x=x, y='duration', hue=hue, data=df, ax=ax2, ci=None, palette=palette) ax2.set_title('Average rental duration') ax2.set_ylabel('rental duration') plt.tight_layout() plt.show()
def colormap(): global _COLORMAP if _COLORMAP is None: classifiers = models.CLASSIFIER_KEYS standard_features = models.FEATURE_SETS new_features = models.NEW_FEATURE_SETS metrics = models.METRICS p1 = sns.xkcd_palette([ "brick", "steel blue", "moss green", "dusty rose", "grape", "pale orange", "deep pink" ]) p2 = sns.xkcd_palette([ "purple", "green", "blue", "pink", "brown", "light blue", "grey", "orange", "tan" ]) p3 = sns.husl_palette(len(new_features), h=0.3) p4 = sns.xkcd_palette(["windows blue", "faded green", "dusty purple"]) # p2 = sns.color_palette("husl", len(featuresets)) MAP1 = {key: color for key, color in zip(classifiers, p1)} MAP2 = {key: color for key, color in zip(standard_features, p2)} MAP3 = {key: color for key, color in zip(new_features, p3)} MAP4 = {key: color for key, color in zip(metrics, p4)} _COLORMAP = MAP1 _COLORMAP.update(MAP2) _COLORMAP.update(MAP3) _COLORMAP.update(MAP4) return _COLORMAP
def generateMoleculeSVGsbyLabel(topicModel, label, idLabelToMatch=0, baseRad=0.5, molSize=(250,150),maxMols=100): data = topicModel.moldata.loc[topicModel.moldata['label_'+str(idLabelToMatch)] == label] if not len(data): return "Label not found" svgs=[] namesSVGs=[] numDocs, numTopics = topicModel.documentTopicProbabilities.shape colors = sns.husl_palette(numTopics, s=.6) topicIdx = np.argmax(topicModel.documentTopicProbabilities[data.index,:],axis=1) topicProb = np.amax(topicModel.documentTopicProbabilities[data.index,:],axis=1) topicdata = list(zip(data.index, topicIdx, topicProb)) topicdata_sorted = sorted(topicdata, key=operator.itemgetter(2), reverse=True) for idx,tIdx,tProb in topicdata_sorted[:maxMols]: mol = Chem.MolFromSmiles(data['smiles'][idx]) color = tuple(colors[tIdx]) svg = drawTopicWeightsMolecule(mol, idx, tIdx, topicModel, molSize=molSize, baseRad=baseRad, color=color) svgs.append(svg) namesSVGs.append(str("Topic "+str(tIdx)+" | (p="+str(round(tProb,2))+")")) return svgs, namesSVGs
def mpld3_2(): sns.set_theme() # Load the brain networks example dataset df = sns.load_dataset("brain_networks", header=[0, 1, 2], index_col=0) # Select a subset of the networks used_networks = [1, 5, 6, 7, 8, 12, 13, 17] used_columns = ( df.columns.get_level_values("network").astype(int).isin(used_networks)) df = df.loc[:, used_columns] # Create a categorical palette to identify the networks network_pal = sns.husl_palette(8, s=.45) network_lut = dict(zip(map(str, used_networks), network_pal)) # Convert the palette to vectors that will be drawn on the side of the matrix networks = df.columns.get_level_values("network") network_colors = pd.Series(networks, index=df.columns).map(network_lut) # Draw the full plot g = sns.clustermap(df.corr(), center=0, cmap="vlag", row_colors=network_colors, col_colors=network_colors, dendrogram_ratio=(.1, .2), cbar_pos=(.02, .32, .03, .2), linewidths=.75, figsize=(12, 13)) g.ax_row_dendrogram.remove() mpld3.show()
def palette(nb, hls=HLS, viridis=VIRIDIS): """ Use a smart palette from seaborn, for nb different plots on the same figure. - Ref: http://seaborn.pydata.org/generated/seaborn.hls_palette.html#seaborn.hls_palette """ if viridis: return sns.color_palette('viridis', nb) else: return sns.hls_palette(nb + 1)[:nb] if hls else sns.husl_palette(nb + 1)[:nb]
def plot_entries(t, entries, n, m): fig, axes = plt.subplots(n, m, sharex=True) axesr = np.ravel(axes, order='F') # column order color = sns.husl_palette(len(axesr)) for i, ax in enumerate(axesr): ax.plot(t, entries[:, i], color=color[i]) ax.set_xlabel('time [s]') return fig, ax
def random_color_list(n): cat_size=n color_list_ori = np.round(np.array(sns.husl_palette(cat_size, h=.3))*255).reshape(-1).astype(int) color_list_ori hex_list = np.array([hex(x)[2:] for x in color_list_ori.reshape(-1).astype(int)]).reshape(cat_size,3) color_list = [] for c in hex_list: color_list.append("#"+''.join(c).upper()) return color_list
def _generate_edges(self) -> Dict[Tuple[Position, Position], Dict[str, Any]]: edges = dict() for route, color in zip(self.routes, sns.husl_palette(len(self.routes))): for first, second in zip(route.stops[:-1], route.stops[1:]): edges[(first.position, second.position)] = { 'color': color, 'label': '', } return edges
def train_test_histogram(data, filename): num_activities = data.shape[0] with sns.axes_style("ticks"): fig, ax = plt.subplots(figsize=params.FIGSIZE) sns.barplot(x="activity", y="total size", palette=sns.husl_palette(num_activities), data=data, ax=ax) sns.barplot(x="activity", y="training size", palette=sns.husl_palette(num_activities, l=.3, s=.8), data=data, ax=ax) ax.set_xlabel("Activity") ax.set_ylabel("Count") plt.xticks(rotation=params.X_TICK_ROTATION) plt.yticks() sns.despine() plt.savefig(filename)
def plot_watershed(self, peaks): """Plot the watershed segmentation.""" palette = sns.husl_palette(len(peaks)) cmap = mpl.colors.ListedColormap(palette) m = Mosaic(stat=self.inputs.seg_file, mask=self.inputs.mask_file) m.plot_overlay(thresh=.5, cmap=cmap, vmin=1, vmax=len(peaks)) out_fname = nii_to_png(self.inputs.seg_file) self.out_files.append(out_fname) m.savefig(out_fname) m.close()
def plot_norm(samples, fields=None, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts n = t.shape[0] if fields is None: # Set default to be fields that are not scalar with data available for # at least 10% of the timerange fields = [] for name in samples.dtype.names: mask = samples.mask[name] if len(mask.shape) < 2: continue if reduce(mul, mask.shape[1:], 1) == 1: continue if np.count_nonzero(mask) < n/10: fields.append(name) if isinstance(fields, str): fields = (fields,) n = len(fields) if n > 6: color = sns.husl_palette(n) else: color = sns.color_palette('muted', n) if n > 1: fig, axes = plt.subplots(math.ceil(n/2), 2) axes = np.ravel(axes) if len(axes) > n: axes[-1].axis('off') else: fig, ax = plt.subplots() axes = [ax] for n, f in enumerate(fields): ax = axes[n] X = samples.__getattribute__(f) x = np.linalg.norm(X, axis=(1, 2)) ax.set_xlabel('{} [{}]'.format('time', unit('time'))) ax.plot(t, x, color=color[n], label=f) ax.legend() if n > 0: title = 'Norms' else: ax.legend().remove() field_parts = fields[0].split('.') title = ' '.join([f.title() for f in field_parts[:-1]] + field_parts[-1:]) title = 'Norm of ' + title axes = ax _set_suptitle(fig, title, filename) return fig, axes
def learning_curve(csv_file, out_figure): df = pd.read_csv(csv_file) df_train = df.query("type == 'train'") df_val = df.query("type == 'test'") colors = sns.husl_palette(3, l=.5, s=.5) plt.figure(figsize=(12, 6), dpi=500) ######### # TRAIN # ######### # train loss plt.subplot(221) plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) plt.plot(df_train.i_iter, df_train.loss, '-', markersize=1, color=colors[0], alpha=.5, label='train loss') plt.xlabel('iteration') plt.ylabel('train loss') # train accuracy plt.subplot(222) plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.plot(df_train.i_iter, df_train.acc, '-', markersize=1, color=colors[1], alpha=.5, label='train accuracy') plt.xlabel('iteration') plt.ylabel('train overall accuracy') ####### # VAL # ####### # val loss plt.subplot(223) plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) plt.plot(df_val.i_iter, df_val.loss, 'o-', color=colors[0], alpha=.5, label='val loss') plt.xlabel('iteration') plt.ylabel('val loss') # val accuracy plt.subplot(224) plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0)) plt.plot(df_val.i_iter, df_val.acc, 'o-', color=colors[1], alpha=.5, label='val accuracy') plt.xlabel('iteration') plt.ylabel('val overall accuracy') plt.savefig(out_figure) print("Saved as '{0}'".format(out_figure))
def plot_peaks(self, peaks): """Plot the peaks.""" palette = sns.husl_palette(len(peaks)) cmap = mpl.colors.ListedColormap(palette) disk_img = self._peaks_to_disks(peaks) m = Mosaic(stat=disk_img, mask=self.inputs.mask_file) m.plot_overlay(thresh=.5, cmap=cmap, vmin=1, vmax=len(peaks)) out_fname = nii_to_png(self.inputs.zstat_thresh_file, "_peaks") self.out_files.append(out_fname) m.savefig(out_fname) m.close()
def create_lut(self, markers): """Create a Freesurfer-style lookup tabke for the segmentation.""" n = int(markers.max()) colors = [[0, 0, 0]] + sns.husl_palette(n) colors = np.hstack([colors, np.zeros((n + 1, 1))]) lut_data = pd.DataFrame(columns=["#ID", "ROI", "R", "G", "B", "A"], index=np.arange(n + 1)) names = ["Unknown"] + ["roi_%d" % i for i in range(1, n + 1)] lut_data["ROI"] = np.array(names) lut_data["#ID"] = np.arange(n + 1) lut_data.loc[:, "R":"A"] = (colors * 255).astype(int) return lut_data
def show_object_dist(marginal_dist_n, utterance, objects, title = 'Distribution over objects in-the-moment, given utterance '): num_objects = np.shape(marginal_dist_n)[1] fig, ax = plt.subplots() # set up axes ax.set_xlim(0, num_objects + 1) ax.set_ylim(0, 1) ax.set_xlabel('objects') xticks(np.arange(num_objects)+1) #ax.set_xticklabels(np.arange(num_objects)) ax.set_xticklabels(objects) # draw plot pos = np.arange(num_objects) + .6 ax.bar(pos, np.exp(marginal_dist_n[utterance[0], :]), color=sns.husl_palette(6, s=.75), ecolor="#333333"); fig.suptitle('Distribution over objects in-the-moment, given utterance %d ' % utterance, fontsize=20)
def plot_entries(samples, field, filename=None): # get time from timestamp and sample time t = samples.bicycle.dt.mean() * samples.ts X = samples.__getattribute__(field) _, rows, cols = X.shape n = rows*cols if n > 6: color = sns.husl_palette(n) else: color = sns.color_palette('muted', n) fig, axes = plt.subplots(rows, cols, sharex=True) axes = axes.ravel() vector_type = False if cols == 1: vector_type = True fields = field.split('.') for n in range(rows*cols): ax = axes[n] if vector_type: x = X[:, n] else: i = n // cols j = n % cols x = X[:, i, j] # small entries in Kalman gain K break plots small_indices = np.abs(x) < 10*np.finfo(x.dtype).eps if small_indices.any(): # copy data (maybe we should modify in place? x = np.array(x) x[small_indices] = 0 ax.set_xlabel('{} [{}]'.format('time', unit('time'))) if vector_type: ax.set_title('{}[{}]'.format(fields[-1], n)) else: ax.set_title('{}[{}, {}]'.format(fields[-1], i, j)) ax.plot(t, x, color=color[n]) title = ' '.join([f.title() for f in fields[:-1]] + fields[-1:]) _set_suptitle(fig, title, filename) return fig, axes
def plot_confound_correlation(self, fname=None, legend=True, close=False): """Plot how correlated the condition and confound regressors are.""" import seaborn as sns corrs = self.design_matrix.corr() corrs = corrs.loc[self._confound_names, self._condition_names] n_bars = len(self._condition_names) * len(self._confound_names) ysize = min(n_bars * .2, 10) figsize = (9, ysize) f, ax = plt.subplots(1, 1, figsize=figsize) n_conf = corrs.shape[0] colors = sns.husl_palette(len(corrs)) for i, (cond, conf_corrs) in enumerate(corrs.iteritems()): barpos = np.linspace(i, i + 1, n_conf + 1)[:-1][::-1] bars = ax.barh(barpos, conf_corrs.abs(), height=1 / n_conf, color=colors, linewidth=.3, edgecolor="white") ax.set_xlim(0, 1) ax.set_xlabel("| correlation |") ax.set_yticks(np.arange(len(self._condition_names)) + 0.5) ax.set_yticklabels(self._condition_names) ax.set_ylim(0, len(self._condition_names)) ax.yaxis.grid(False) for y in range(1, len(self._condition_names)): ax.axhline(y, ls=":", c=".6", lw=1, zorder=0) if legend: ncol = len(self._confound_names) // 15 + 1 box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * (1 - .15 * ncol), box.height]) lgd = ax.legend(bars, self._confound_names, ncol=ncol, fontsize=10, loc='center left', bbox_to_anchor=(1, 0.5)) else: lgd = [] if fname is not None: f.savefig(fname, dpi=100, bbox_extra_artists=[lgd], bbox_inches="tight") if close: plt.close(f)
def show_lexicon_dist(lexicons, lexicon_weights, title = 'Distribution over lexicons', trim = 0): mask = (lexicon_weights > np.log(trim)) lexicons = lexicons[mask, :] lexicon_weights = lexicon_weights[mask] num_lexs = np.shape(lexicons)[0] fig, ax = plt.subplots() # set up axes ax.set_xlim(0,num_lexs+1) #ax.set_ylim(0,1) ax.set_xlabel('lexicons') xticks(np.arange(num_lexs)+1) # draw plot pos = np.arange(num_lexs) + .6 ax.bar(pos, np.exp(lexicon_weights), color=sns.husl_palette(6, s=.75), ecolor="#333333"); ax.set_xticklabels(lexicons) fig.suptitle(title, fontsize=20)
def plot_confound_correlation(self, fname=None, legend=True): """Plot how correlated the condition and confound regressors are.""" corrs = self.design_matrix.corr() corrs = corrs.loc[self._confound_names, self._condition_names] n_bars = len(self._condition_names) * len(self._confound_names) xsize = min(n_bars * .2, 10) figsize = (xsize, 4) f, ax = plt.subplots(1, 1, figsize=figsize) n_conf = corrs.shape[0] colors = sns.husl_palette(n_conf) for i, (cond, conf_corrs) in enumerate(corrs.iteritems()): barpos = np.linspace(i, i + 1, n_conf + 1)[:-1] bars = ax.bar(barpos, conf_corrs.abs(), width=1 / n_conf, color=colors, linewidth=0) ax.set_xticks(np.arange(len(self._condition_names)) + 0.5) ax.set_xticklabels(self._condition_names) ax.set_xlim(0, len(self._condition_names)) ax.xaxis.grid(False) ymin, ymax = ax.get_ylim() ymax = max(.25, ymax) ax.set_ylim(0, ymax) ax.set_ylabel("abs(correlation)") for x in range(1, len(self._condition_names)): ax.axvline(x, ls=":", c="#222222", lw=1) if legend: ncol = len(self._confound_names) // 15 + 1 box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * (1 - .15 * ncol), box.height]) lgd = ax.legend(bars, self._confound_names, ncol=ncol, fontsize=10, loc='center left', bbox_to_anchor=(1, 0.5)) else: lgd = [] if fname is not None: f.savefig(fname, bbox_extra_artists=[lgd], bbox_inches="tight")
def plot_Bwall(Bs, yrange=None, title=None, legs=None, palette=None): f, ax = plt.subplots(figsize=[12, 8]) sns.set_style("white") N = len(Bs) if palette is not None: cols = sns.color_palette(n_colors=N, palette=palette) else: cols = sns.husl_palette(n_colors=N, s=1.0, l=0.55) hs = [] for i in range(N): Z = Bs[i].Z.data Bw = Bs[i].data hr, = plt.plot(Z, np.real(Bw), "-", color=cols[i], linewidth=4) # hi, = plt.plot(Z,np.imag(Bw),'-.',color=cols[i],linewidth=3) if legs is not None: hr.set_label(legs[i]) # +' - Re{}') # hi.set_label(legs[i]+' - Im{}') ax.legend(fontsize=22, loc="best") hs.append(hr) # hs.append(hi) ax.set_xlim([-1.25, 1.25]) if yrange is not None: ax.set_ylim(yrange) if title is not None: ax.set_title(title, fontsize=32) ax.set_xlabel("Z", fontsize=28) ax.set_ylabel(r"HFS Re{$\delta B_{Z,pl}$} (G/kA)", fontsize=28) ax.tick_params(labelsize=24) # plt.tight_layout() return (f, ax, hs)
def plot_design_matrix(self, dm, variable, split_by=None, panel=False): print(self.level_map.keys()) import matplotlib.pyplot as plt import seaborn as sns n_cols = min(dm.shape[1], 10) n_axes = dm.shape[2] n_rows = self.dataset.n_vols * 3 * self.dataset.n_runs fig, axes = plt.subplots(n_axes, 1, figsize=(20, 2 * n_axes)) if n_axes == 1: axes = [axes] colors = sns.husl_palette(n_cols) for j in range(n_axes): ax = axes[j] min_y, max_y = dm[:n_rows, :, j].min(), dm[:n_rows, :, j].max() for i in range(n_cols): ax.plot(dm[:n_rows, i, j], c=colors[i], lw=2) ax.set_ylim(min_y - 0.05*min_y*np.sign(min_y), max_y + 0.05*max_y*np.sign(max_y)) if n_axes > 1: try: ax.set(ylabel=list(self.level_map[split_by].keys())[j]) except: pass title = variable + ('' if split_by is None else ' split by ' + split_by) axes[0].set_title(title, fontsize=16) plt.show()
import matplotlib.patches as p import matplotlib.pyplot as plt import pandas import seaborn as sns from datos import data sns.set(style="white") #colors=sns.color_palette("muted", n_colors=9) colors = sns.husl_palette(10) fig=plt.figure(1, figsize=(4,4)) ax = plt.subplot(111, aspect='equal') d=data('mtcars') ps = pandas.Series([i for i in d.gear]) counts = ps.value_counts() x=0.05 y=0.9 m=0 cols=1 for i in counts: for j in range(i): p1 = p.Circle((x, y), 0.05, fc=colors[m], edgecolor='white') x=x+0.1 ax.add_patch(p1) cols=cols+1 if (cols>10): cols=1 x=0.05 y=y-0.1 m=m+4
def learning_curve(csv_file): df = pd.read_csv(csv_file) df_train = df.query("type == 'train'") df_val = df.query("type == 'val'") colors = sns.husl_palette(3, l=.5, s=.5) plt.figure(figsize=(12, 6), dpi=500) ######### # TRAIN # ######### # train loss plt.subplot(231) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) plt.plot(df_train.i_iter, df_train.loss, '-', markersize=1, color=colors[0], alpha=.5, label='train loss') plt.xlabel('iteration') plt.ylabel('train loss') # train accuracy plt.subplot(232) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.plot(df_train.i_iter, df_train.acc, '-', markersize=1, color=colors[1], alpha=.5, label='train accuracy') plt.xlabel('iteration') plt.ylabel('train overall accuracy') # train mean iu plt.subplot(233) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.plot(df_train.i_iter, df_train.iu, '-', markersize=1, color=colors[2], alpha=.5, label='train accuracy') plt.xlabel('iteration') plt.ylabel('train mean IU') ####### # VAL # ####### # val loss plt.subplot(234) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) plt.plot(df_val.i_iter, df_val.loss, 'o-', color=colors[0], alpha=.5, label='val loss') plt.xlabel('iteration') plt.ylabel('val loss') # val accuracy plt.subplot(235) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.plot(df_val.i_iter, df_val.acc, 'o-', color=colors[1], alpha=.5, label='val accuracy') plt.xlabel('iteration') plt.ylabel('val overall accuracy') # val mean iu plt.subplot(236) plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0)) plt.plot(df_val.i_iter, df_val.iu, 'o-', color=colors[2], alpha=.5, label='val mean IU') plt.xlabel('iteration') plt.ylabel('val mean IU') fig_file = osp.splitext(csv_file)[0] + '.png' plt.savefig(fig_file) print('Saved to %s' % fig_file)
def plot_ecm_highlevel(theme, ylim, office): sns.set_style("white") #sns.set_context("paper", font_scale=3) sns.set_context("paper", font_scale=0.8) sns.mpl.rc("figure", figsize=(10,5)) colors_2 = sns.husl_palette(7, l=.8, s=.9) + \ [(192.0/255,192.0/255,192.0/255)] colors_1 = sns.husl_palette(7, l=.5, s=.9) + \ [(104.0/255,104.0/255,104.0/ 255)] colors = [[x, y] for (x, y) in zip(colors_1, colors_2)] colors = reduce(lambda x, y: x + y, colors) office_set = get_office() df = pd.read_csv(os.getcwd() + '/csv_FY/join/join_ecm_2015_highlevel.csv') if office: df = df[df['Building Number'].isin(office_set)] if theme == 'eui': df = df[df['eui_elec'] >= 12] df = df[df['eui_gas'] >= 3] #print ('filter eui', len(df)) if theme == 'eui_water': df = df[df[theme] >= 5] #print ('filter water', len(df)) if theme == 'eui_gas': df = df[df[theme] >= 3] #print ('filter gas', len(df)) if theme == 'eui_elec': df = df[df[theme] >= 12] #print ('filter elec', len(df)) program = list(df)[-8:] program.remove('Renewable Energy') program.remove('Water') print program totalnum = len(set(df['Building Number'].tolist())) print totalnum ps = [[x + '+', ''] for x in program] ps = reduce(lambda x, y: x + y, ps) dfs = [] sizes = [] p_inc = [] for col in program: df_yes = df[df[col] == 1] df_yes['program'] = col df_no = df[df[col] == 0] df_no['program'] = 'no_' + col dfs.append(df_yes) dfs.append(df_no) sizes.append(len(df_yes)) sizes.append(len(df_no)) percent_inprove = 0 if df_no[theme].median() != 0: percent_inprove = (df_no[theme].median() - df_yes[theme].median())/ df_no[theme].median() p_inc.append(percent_inprove) df_all = pd.concat(dfs, ignore_index=True) df_plot = df_all[['program', theme]] p_inc = [[str(round(x, 4)*100) + '%', ''] for x in p_inc] p_inc = reduce(lambda x, y:x + y, p_inc) p_inc_order = sorted(zip(program, p_inc), key=lambda x: x[1]) yn = ['Yes', 'No'] * len(program) order = [x[0] for x in p_inc_order] my_dpi = 300 bx = sns.boxplot(x = 'program', y = theme, data = df_plot, fliersize=0,palette = sns.color_palette(colors)) #BOOKMARK st = sns.stripplot(x = 'program', y = theme, data = df_plot, jitter=0.2, edgecolor='gray', color = 'gray', size=0.3, alpha=0.5) xticklabels = ['{0}(n={1})\n {2}\n {3}'.format(indi, size, '\n'.join(tw.wrap(p, 30,subsequent_indent = ' ')), p_i) for indi, size, p, p_i in zip(yn, sizes, ps, p_inc)] bx.set(xticklabels=xticklabels) for tick in bx.xaxis.get_major_ticks(): tick.label.set_fontsize(6) for tick in bx.yaxis.get_major_ticks(): tick.label.set_fontsize(10) plt.title('Total {0} Buildings'.format(totalnum), fontsize=15) plt.ylabel(theme.upper(), fontsize=12) bx.xaxis.set_label_coords(0.5, -0.08) plt.xlabel('', fontsize=12) plt.ylim((0, ylim)) if office: P.savefig(os.getcwd() + '/plot_FY_annual/ECM2015_office/office_highlevel_{0}_boxplot.png'.format(theme), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) P.savefig(os.getcwd() + '/plot_FY_annual/ECM2015_office/office_highlevel_{0}_boxplot_droplast.png'.format(theme), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) else: P.savefig(os.getcwd() + '/plot_FY_annual/ECM2015/highlevel_{0}_boxplot_droplast.png'.format(theme), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) plt.close()
def husl(n_colors, hue=0.01, saturation=0.9, lightness=0.65): return sns.husl_palette(n_colors, h=hue, s=saturation, l=lightness)
print 'RGB\t HLS' for p in palette: print mcl.rgb2hex(p), print ['{:03.2f}'.format(cc * rgb_scale[i]) for i, cc in enumerate(p)], print ['{:03.2f}'.format(cc * hls_scale[i]) for i, cc in enumerate(colorsys.rgb_to_hls(*p))] # print ['{:1.2f}'.format(cc * hls_scale[i]) # for i, cc in enumerate(mcl.rgb_to_hsv(p))] sns.palplot(palette) # In[3]: print '\n\n--> HUSL' palette = sns.color_palette("husl", 12) + [cblue] palette = sns.husl_palette(12, s=.5, l=.5) print 'RGB\t HLS' for p in palette: print mcl.rgb2hex(p), print ['{:03.2f}'.format(cc * rgb_scale[i]) for i, cc in enumerate(p)], print ['{:03.2f}'.format(cc * hls_scale[i]) for i, cc in enumerate(colorsys.rgb_to_hls(*p))] # print ['{:1.2f}'.format(cc * hls_scale[i]) # for i, cc in enumerate(mcl.rgb_to_hsv(p))] sns.palplot(palette) sns.husl_palette plt.show()
def plot_box_pro(theme, ylim, office): my_dpi = 300 sns.set_style("white") sns.set_context("paper", font_scale=0.8) sns.mpl.rc("figure", figsize=(10,5.5)) filename = os.getcwd() + '/csv_FY/join/join_2015.csv' df = pd.read_csv(filename) df['GSALink'] = df.apply(lambda row: 1 if row['GSALink Option(26)'] + row['GSAlink I(55)'] > 0 else 0, axis=1) df.drop(['GSAlink I(55)', 'GSALink Option(26)'], axis=1, inplace=True) office_set = get_office() if office: df = df[df['Building Number'].isin(office_set)] if theme == 'eui': df = df[df['eui_elec'] >= 12] df = df[df['eui_gas'] >= 3] if theme == 'eui_water': df = df[df['eui_water'] >= 5] if theme == 'eui_gas': df = df[df['eui_gas'] >= 3] if theme == 'eui_elec': df = df[df['eui_elec'] >= 12] # plot only programs dfs = [] df_temp = df df_temp = df_temp[df_temp['Total Programs_v2'] > 0] totalnum = len(set(df_temp['Building Number'].tolist())) program = ['LEED', 'GP', 'first fuel', 'GSALink', 'E4', 'ESPC', 'Shave Energy', 'Energy Star'] colors = sns.husl_palette(7, l=.5, s=.9) + [(104.0/255,104.0/255,104.0/255)] color_dict = dict(zip(program, colors)) sizes = [] for col in program: df_yes = df[df[col] == 1] df_yes['program'] = col dfs.append(df_yes) sizes.append((col, len(df_yes))) df_all = pd.concat(dfs, ignore_index=True) df_plot = df_all size_dict = dict(sizes) print size_dict # sort by median gr = df_plot.groupby('program') medians = [(name, group[theme].median()) for name, group in gr] s_medians = sorted(medians, key=lambda x: x[1]) order = [x for (x, _) in s_medians] order.remove('Energy Star') order.append('Energy Star') print theme print order ordered_color = [color_dict[x] for x in order] df_plot = df_all[['program', theme]] bx = sns.boxplot(x = 'program', y = theme, data = df_plot, fliersize=0, order = order, palette = sns.color_palette(ordered_color)) st = sns.stripplot(x = 'program', y = theme, data = df_plot, jitter=0.2, edgecolor='gray', color = 'gray', size=0.3, alpha=0.5, order=order) xticklabels = ['(n={0})\n{1}+'.format(size_dict[pro], pro) for pro in order] bx.set(xticklabels=xticklabels) for tick in bx.xaxis.get_major_ticks(): tick.label.set_fontsize(9) for tick in bx.yaxis.get_major_ticks(): tick.label.set_fontsize(10) plt.ylim((0, ylim)) plt.title('Total {0} Buildings'.format(totalnum), fontsize=15) plt.ylabel((ylabel_dict[theme])(), fontsize=12, position=(0, 0)) bx.xaxis.set_label_coords(0.5, -0.08) plt.xlabel('Program', fontsize=12) plt.ylim((0, ylim)) if office: P.savefig(os.getcwd() + '/plot_FY_annual/office/box_program_{0}.png'.format(theme), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) else: P.savefig(os.getcwd() + '/plot_FY_annual/box_program_{0}.png'.format(theme), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi)) plt.close()
def plot_box_vio(inputfile, program, prefix, theme, ylim, office, isOnly): ylabel_dict = {'eui':'Electricity + Gas [kBtu/sq.ft]', 'eui_elec':'Electric EUI [kBtu/sq.ft]', 'eui_gas':'Gas EUI [kBtu/sq.ft]', 'eui_oil':'Oil EUI [Gallons/sq.ft]', 'eui_water':'WEUI [Gallons/sq.ft]'} filter_dict = {'eui':'EUI Electric >= 12 and EUI Gas >= 3', 'eui_elec':'Electric >= 12', 'eui_gas':'EUI Gas >= 3', 'eui_water':'WEUI >= 5'} title_dict = {'eui':'EUI', 'eui_elec':'Electric EUI', 'eui_gas':'Gas EUI', 'eui_oil':'Oil EUI', 'eui_water':'WEUI'} sns.set_style("white") sns.set_context("talk", font_scale=1.2) # sns.mpl.rc("figure", figsize=(10,5.5)) # colors_1 = sns.husl_palette(7, l=.8, s=.9) + [(192.0/255,192.0/255,192.0/255)] colors_2 = sns.husl_palette(7, l=.5, s=.9) + [(104.0/255,104.0/255,104.0/255)] colors = [[x, y] for (x, y) in zip(colors_2, colors_2)] colors = reduce(lambda x, y: x + y, colors) office_set = get_office() filename = inputfile df = pd.read_csv(filename) #print ('input length', len(df)) if office: df = df[df['Building Number'].isin(office_set)] plotset = 'office' else: plotset = 'all' if theme == 'eui': df = df[df['eui_elec'] >= 12] df = df[df['eui_gas'] >= 3] #print ('filter eui', len(df)) if theme == 'eui_water': df = df[df[theme] >= 5] #print ('filter water', len(df)) if theme == 'eui_gas': df = df[df[theme] >= 3] #print ('filter gas', len(df)) if theme == 'eui_elec': df = df[df[theme] >= 12] #print ('filter elec', len(df)) totalnum = len(set(df['Building Number'].tolist())) print totalnum if isOnly == 'only': program = [x + '_only' for x in program] dfs = [] sizes = [] p_inc = [] emptys = [] for col in program: df_yes = df[df[col] == 1] if len(df_yes) == 0: emptys.append(col) continue df_yes['program'] = col df_no = df[df['None'] == 1] df_no['program'] = 'no_' + col # print theme # print col # print 'len of yes, {0}'.format(len(df_yes)) # print 'len of no, {0}'.format(len(df_no)) dfs.append(df_no) dfs.append(df_yes) sizes.append(len(df_no)) sizes.append(len(df_yes)) percent_inprove = 0 if df_no[theme].median() != 0: percent_inprove = (df_no[theme].median() - df_yes[theme].median())/df_no[theme].median() p_inc.append(percent_inprove) program = [x for x in program if x not in emptys] ps = [['\n'.join(tw.wrap(x, 20)), ''] for x in program] ps = reduce(lambda x, y: x + y, ps) print ps df_all = pd.concat(dfs, ignore_index=True) df_plot = df_all[['program', theme]] p_inc = [[str(round(x, 4)*100) + '%', ''] for x in p_inc] p_inc = reduce(lambda x, y:x + y, p_inc) yn = ['No', 'Yes'] * len(program) my_dpi = 300 bx = sns.boxplot(x = 'program', y = theme, data = df_plot, fliersize=0, palette = sns.color_palette(colors)) st = sns.stripplot(x = 'program', y = theme, data = df_plot, jitter=0.2, edgecolor='gray', color = 'gray', size=0.3, alpha=0.5) xticklabels = ['{0}(n={1})\n{2}\n{3}'.format(indi, size, p, p_i) for indi, size, p, p_i in zip(yn, sizes, ps, p_inc)] bx.set(xticklabels=xticklabels) for tick in bx.xaxis.get_major_ticks(): tick.label.set_fontsize(7) for tick in bx.yaxis.get_major_ticks(): tick.label.set_fontsize(10) plt.title('Site {0} KBTU/ft2/year by ECM Program vs Not (n = {1})'.format(title_dict[theme], totalnum)) plt.suptitle('FY15 EUAS {1} building set (A, B, C, D, E, I) with positive sq.ft.\n{0} (exclude lowest 10%)'.format(filter_dict[theme], plotset)) plt.ylabel(ylabel_dict[theme]) bx.xaxis.set_label_coords(0.5, -0.08) plt.xlabel('', fontsize=12) plt.ylim((0, ylim)) if office: P.savefig(os.getcwd() + \ '/plot_FY_annual/office/office_{1}_{0}.png'.format(theme, prefix), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi), bbox_inches='tight') else: P.savefig(os.getcwd() + \ '/plot_FY_annual/{1}_{0}.png'.format(theme, prefix), dpi = my_dpi, figsize = (2000/my_dpi, 500/my_dpi), bbox_inches='tight') plt.close()
def generate_viz( scene, outdir,fig, mm, numbers=False, debug=False, datadir=None, overwrite=False): if debug: import __builtin__ openfiles = set() oldfile = __builtin__.file class newfile(oldfile): def __init__(self, *args): self.x = args[0] print("### OPENING %s ###" % str(self.x) ) oldfile.__init__(self, *args) openfiles.add(self) def close(self): print("### CLOSING %s ###" % str(self.x)) oldfile.close(self) openfiles.remove(self) oldopen = __builtin__.open def newopen(*args): return newfile(*args) __builtin__.file = newfile __builtin__.open = newopen figname = os.path.split('{}_plot.png'.format(scene))[-1] if os.path.exists(os.path.join(outdir, figname)): if overwrite: print("{} exists, overwriting.".format(figname)) else: print("{} exists, skipping.".format(figname)) return ax = fig.gca() if not datadir: datadir = os.path.join(NASPATH, scene, 'sdr') elif os.path.exists(os.path.join(datadir, scene, 'sdr')): datadir = os.path.join(datadir, scene, 'sdr') else: datadir = os.path.join(datadir, scene) print("Working on data in {}.".format(datadir)) if not os.path.isdir(datadir): print("{} is not a directory, skipping.".format(datadir)) return os.chdir(datadir) testfiles = glob.glob(FILEPAT) print("Working with {} data files.".format(len(testfiles))) testfiles.sort() current_palette = sns.husl_palette(n_colors=len(testfiles), h=.2, l=.4, s=.9) totalfrac = 0.0 plotobj = [] plottexts = [] for idx, tf in enumerate(testfiles): print('{}: {}'.format(idx, tf)) try: tsto = raster.VIIRSHDF5(tf) except IOError as err: print("Error opening file {}: {}.".format(tf, err)) print("Aborting plot for scene {}.".format(datadir)) return try: edgelons, edgelats = vt.getedge(tsto) except PygaarstRasterError as err: print("PygaarstRasterError, aborting: {}".format(err)) return x, y = mm(edgelons, edgelats) contour = Polygon(zip(x, y)) try: intersect = contour.intersection(poly) fraction = intersect.area/poly.area except TopologicalError: fraction = 0 totalfrac += fraction*100 print("Intersection as fraction of AOI: {}".format(fraction)) if fraction > MINFRAC: size = 10 alpha = 1.000 color = current_palette[idx] plotobj.append(ax.plot(x, y, zorder=15, linewidth=3, color=color, alpha=alpha)) else: size = 5 alpha = .4 color = current_palette[idx] plotobj.append(ax.scatter(x, y, size, zorder=25, marker='o', color=color, alpha=alpha)) if numbers: midx = 0.5 * (min(x) + max(x)) midx = max(min(midx, mm.xmax), mm.xmin) midy = 0.5 * (min(y) + max(y)) midy = max(min(midy, mm.ymax), mm.ymin) plottexts.append( ax.text(midx, midy, str(idx), color=current_palette[idx], weight='bold', fontsize=20 )) tsto.close() if debug: printOpenFiles(openfiles) ax.set_title("{}: {:.2f}% of AOI".format(scene, totalfrac)) print("Outdir variable: {}".format(outdir)) print("Figname variable: {}".format(figname)) imgpath = os.path.join(outdir, figname) print("Saving figure to {}.".format(imgpath)) fig.savefig(imgpath) # clean up base plot for item in plotobj: try: for subobj in item: try: ax.lines.remove(subobj) except ValueError: pass except TypeError: # not a sequence try: ax.collections.remove(item) except ValueError: pass for item in plottexts: ax.texts.remove(item)
loaded_keep_prob: 1.0} softmax_prob = sess.run([loaded_preds], feed_dict=test_feed) predictions.append(np.argmax(softmax_prob)) softmax_probs.append(softmax_prob) return predictions, softmax_probs # In[22]: import matplotlib.pyplot as plt from matplotlib import gridspec import seaborn as sns get_ipython().run_line_magic('matplotlib', 'inline') mycol = sns.husl_palette(10)[0] def show_predicted(save_model_path, test_image, true_label): _, softmax_probs = predict_from_trained_model(save_model_path, test_image) sign_names = pd.read_csv("traffic-signs-data/signnames.csv") softmax_probs = pd.DataFrame(softmax_probs[0][0].reshape(43, 1), columns=['softmax_prob']) tmp_table = pd.concat([sign_names, softmax_probs], 1) tmp_table = tmp_table.sort_values(by = "softmax_prob", ascending=False) top5 = tmp_table.iloc[:5, ] plt.figure(figsize=(10, 4)) gs = gridspec.GridSpec(1, 2, width_ratios=[4, 1])