def draw_bar(days, START=None): data = get_freq(days, START) k = sorted(list(data.keys())) res = [0] * (k[1]) for i in range(1, len(k) - 1): res += [data[k[i]]] * (k[i + 1] - k[i]) res += [0] * (1440 - k[-1]) x = np.array(list(range(len(res)))) y = np.array(res) fig, ax = plt.subplots() colors = cm.YlGnBu(y / float(max(y))) ax.bar(x, y, width=1.0, color=colors) ticks = {} for hour in range(0, 24, 3): ticks[hour * 60] = '{}:00'.format(hour) ticks[24 * 60] = '0:00' ax.set_xticks(list(ticks.keys())) ax.set_xticks(list(range(0, 24 * 60, 60)), minor=True) ax.set_xticklabels(list(ticks.values())) ax.grid(b=True, which='minor', axis='x', linestyle='dotted') ax.set_xlim(6 * 60, 24 * 60) plt.show()
def _create_colors(classes): n_cts = len(classes) color_norm = colors.Normalize(vmin=-n_cts / 3, vmax=n_cts) ct_arr = np.arange(n_cts) ct_colors = cm.YlGnBu(color_norm(ct_arr)) return ct_colors
def _create_colors(lr): n_cts = lr.classes_.shape[0] color_norm = colors.Normalize(vmin=-n_cts / 3, vmax=n_cts) ct_arr = np.arange(n_cts) ct_colors = cm.YlGnBu(color_norm(ct_arr)) return ct_colors
def DrawLocalCurrents(self, bDraw=None): if bDraw==None: bDraw = self.cbOptions1.lines[2][0].get_visible() self.axCM.set_visible(bDraw) if bDraw and self.Gate is not None: #I = self.Mol.CurrentMatrix(self.Bias, self.Gate) I = self.Mol.LocalCurrents(self.Bias, self.Gate) #DisplayMatrix(I) I = np.real(I) maxCurrent = abs(I).max() print "Maximum current:", maxCurrent for i in range(self.Mol.N): for j in range(i+1,self.Mol.N): Inet = (I[i,j]-I[j,i])/2 if abs(Inet)>maxCurrent*0.1: color = cm.YlGnBu(1-math.log10(abs(Inet))/-10) if Inet>0: self.LCPlot[i,j].set_linewidth(Inet/maxCurrent*10) self.LCPlot[i,j].set_color(color) self.LCPlot[i,j].set_visible(True) self.LCPlot[j,i].set_visible(False) print "Current from", i, "to", j, "is", I[i,j] else: self.LCPlot[j,i].set_linewidth(-Inet/maxCurrent*10) self.LCPlot[j,i].set_color(color) self.LCPlot[j,i].set_visible(True) self.LCPlot[i,j].set_visible(False) print "Current from", j, "to", i, "is", -I[i,j] else: self.LCPlot[i,j].set_visible(False) self.LCPlot[j,i].set_visible(False) else: for i in range(self.Mol.N): for j in range(self.Mol.N): if i!=j: self.LCPlot[i,j].set_visible(False) self.fig.canvas.draw()
# output colormap to txt file # # dependencies: matplotlib from matplotlib import cm import csv with open('ylgnbu.txt', 'wb') as f: writer = csv.writer(f, delimiter=' ') for c in range(0,255): # in this case I picked YlGnBu but you can pick any # color map you want. writer.writerow(cm.YlGnBu(c)) f.close()
def main(args, tick_font_size, label_font_size): lines = open(args.input_file).readlines() data_lines = lines[1:] experiment_names = lines[0].split() experiment_index = args.experiment_columns experiments = [experiment_names[ind] for ind in experiment_index] n_subfigs = len(experiments) corrected_p_threshold = args.alpha / sum(range(len(experiments))) #Bonferroni correction def fit_func(B, x): """y = m*x + b""" return B[0] * x + B[1] linear = Model(fit_func) fig = plt.figure() color_generator = iter(cm.magma(arange(0, 1, 1.0 / n_subfigs))) z = 0 for i in range(n_subfigs): experiment1 = experiments[i] for j in range(n_subfigs): experiment2 = experiments[j] z += 1 vals_1 = {} for line in data_lines: atoms = line.split() if atoms[experiment_index[i]] != 'NA': #exclude NA values generated by ParTIES score = float(atoms[experiment_index[i]]) ctrl_mean = mean([ float(atoms[n]) for n in range(len(atoms)) if n in args.control_columns ]) vals_1[atoms[0]] = max(score - ctrl_mean, 0) vals_2 = {} for line in data_lines: atoms = line.split() if atoms[experiment_index[j]] != 'NA': #exclude NA values generated by ParTIES score = float(atoms[experiment_index[j]]) ctrl_mean = mean([ float(atoms[n]) for n in range(len(atoms)) if n in args.control_columns ]) vals_2[atoms[0]] = max(score - ctrl_mean, 0) common_keys = set(vals_1.keys()) & set(vals_2.keys()) x, y = [], [] for akey in common_keys: x.append(vals_2[akey]) y.append(vals_1[akey]) slope, intercept, r_value, p_value, std_err = stats.linregress( x, y) rho, rho_p = stats.spearmanr(x, y) if i > j: if len(x) > 0 and len(y) > 0: plt.subplot(n_subfigs, n_subfigs, z) plt.hexbin(x, y, vmin=args.hexbin_vmin, vmax=args.hexbin_vmax, mincnt=1, bins='log', cmap=cm.viridis) max_max = max(max(x), max(y)) xi = arange(0, max_max, 0.01) line = slope * xi + intercept mydata = RealData(array(x), array(y), sx=std(x), sy=std(y)) if not args.deactivate_ODR: myodr = ODR(mydata, linear, beta0=[slope, intercept]) ODR_out = myodr.run() if args.verbose: ODR_out.pprint() ODR_fitted_line = fit_func(ODR_out.beta, array(x)) lowess_arr = lowess(y, x, delta=0.01, return_sorted=True) ys = lowess_arr[:, 1] xs = lowess_arr[:, 0] plt.plot(xs, ys, 'orange', lw=args.regression_line_width) plt.plot(xi, line, ls='-', color='red', lw=args.regression_line_width) if not args.deactivate_ODR: plt.plot(x, ODR_fitted_line, "--", color='gray', lw=args.regression_line_width) plt.axis(xmax=1.01, ymax=1.01, ymin=-0.01, xmin=-0.01) ax = plt.gca() current_xticklabels = ax.get_xticks() current_yticklabels = ax.get_yticks() ax.set_xticklabels([]) ax.set_yticklabels([]) plt.tick_params(axis='both', which='both', top='off', right='off') if j == 0: ax.set_yticklabels(current_yticklabels, fontsize=tick_font_size) if args.show_axis_labels: ax.set_ylabel(experiment1, fontsize=label_font_size, rotation=90, style=args.font_style) if i == n_subfigs - 1: ax.set_xticklabels(current_xticklabels, fontsize=tick_font_size, rotation=45) if args.show_axis_labels: ax.set_xlabel(experiment2, fontsize=label_font_size, style=args.font_style) elif i == j: plt.subplot(n_subfigs, n_subfigs, z) plt.tick_params(axis='both', which='both', top='off', right='off') if len(x) > 0: plt.hist(x, bins=arange(0, 1, 0.025), linewidth=0.3, ec="white", fc=next(color_generator)) plt.axis(ymin=0, ymax=args.histogram_max) ax = plt.gca() ax.set_yticks(ax.get_yticks()[::2]) ax.set_xticks(arange(0, 1.2, 0.2)) current_xticklabels = ax.get_xticks() current_yticklabels = ax.get_yticks() ax.set_xticklabels([], fontsize=tick_font_size) ax.set_yticklabels(current_yticklabels, fontsize=tick_font_size) if i == n_subfigs - 1: ax.set_xticklabels(current_xticklabels, fontsize=tick_font_size, rotation=45) if i > 0: plt.tick_params(axis='both', labelleft='off') else: plt.subplot(n_subfigs, n_subfigs, z) ax = plt.gca() current_xticklabels = ax.get_xticks() ax.set_xticklabels(current_xticklabels, fontsize=tick_font_size) if not args.use_pearson: ax.set_facecolor(cm.YlGnBu(rho)) if rho > 0.5: if rho_p < corrected_p_threshold: plt.text(0.2, 0.4, "%.2f" % round(rho, 3), fontsize=1.5 * label_font_size, color='white') else: plt.text(0.2, 0.4, "%.2f^" % round(rho, 3), fontsize=1.5 * label_font_size, color='white') else: if rho_p < corrected_p_threshold: plt.text(0.2, 0.4, "%.2f" % round(rho, 3), fontsize=1.5 * label_font_size, color='black') else: plt.text(0.2, 0.4, "%.2f^" % round(rho, 3), fontsize=1.5 * label_font_size, color='black') else: ax.set_facecolor(cm.YlGnBu(r_value)) if r_value > 0.5: plt.text(0.2, 0.4, "%.2f" % round(r_value, 3), fontsize=1.5 * label_font_size, color='white') else: plt.text(0.2, 0.4, "%.2f" % round(r_value, 3), fontsize=1.5 * label_font_size, color='black') plt.tick_params(axis='both', which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labelleft='off', labelright='off') ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) if args.show_colorbar: fig.subplots_adjust(right=0.8) cbar_ax = fig.add_axes([0.85, 0.4, 0.015, 0.4]) cb_label = 'correlation plot hexbin scale (log10)' cb = plt.colorbar(cax=cbar_ax, drawedges=False, ticks=range(args.hexbin_vmin, args.hexbin_vmax + 1), label=cb_label) current_yticklabels = cbar_ax.get_yticks() cbar_ax.set_yticklabels(range(args.hexbin_vmin, args.hexbin_vmax + 1), fontsize=tick_font_size) cbar_ax.set_ylabel(cb_label, fontsize=label_font_size) cb.outline.set_linewidth(1) #cb.ax.get_children()[4].set_linewidths(1) plt.savefig(args.output_matrix_image, dpi=((args.image_resolution)))
# d01 box for i, (xlim, ylim, s,c) in enumerate( zip([xlim_d01, xlim_d02, xlim_d03], [ylim_d01, ylim_d02, ylim_d03], [15, 12, 9], ['b','g','r'])): ax.add_patch(mpl.patches.Rectangle((xlim[0], ylim[0]), xlim[1]-xlim[0], ylim[1]-ylim[0], fill=None, lw=1.5, edgecolor='r', zorder=10)) ax.add_feature(OCEAN, lw=0.5, ec='k', zorder=0, facecolor='w') ax.add_feature(LAND, lw=0.5, ec='k', zorder=1, facecolor='cornsilk') ax.add_feature(LAKES, lw=0.5, ec='k', zorder=2, facecolor='w') #ax.coastlines('50m', linewidth=1.5, ) white = np.ones((10,4)) high = cm.YlGnBu(np.linspace(0, 1, 128)) colors = np.vstack((white, high)) my_color = LinearSegmentedColormap.from_list('my_colormap',colors, N=24) # CS = ax.contour(to_np(lons), to_np(lats), roms.h, levels=[10,50,100,200,500], linewidths=1.25, colors='k', transform=proj) CS.levels = [nf(val) for val in CS.levels] ax.clabel(CS, CS.levels, fmt='%r', inline=True, fontsize=9, ) cn = ax.contourf(to_np(lons), to_np(lats), roms.h, np.arange(0,7000,500), cmap=my_color, transform=proj, ) cbar = fig.colorbar(cn, ax=ax, shrink=0.6, ) cbar.set_label("ROMS Bathymetry (m)", rotation=-90, va='bottom', fontsize=14) cbar.add_lines(CS) lonmin, lonmax, latmin, latmax = [118, 128, 27, 41]