def create_canvas(self): # pragma: no cover self.fig = Figure() canvas = FigureCanvas(self.fig) self.ui.mainLayout.addWidget(canvas) canvas.setFocusPolicy(QtCore.Qt.StrongFocus) # Add subplots gridspec = GridSpec(2, 4) self.map_ax = self.fig.add_subplot( gridspec.new_subplotspec((0, 0), rowspan=2, colspan=2) ) self.spectrum_ax = self.fig.add_subplot( gridspec.new_subplotspec((0, 2), rowspan=1, colspan=2) ) self.hist_ax = self.fig.add_subplot( gridspec.new_subplotspec((1, 2), rowspan=1, colspan=1) ) self.edge_ax = self.fig.add_subplot( gridspec.new_subplotspec((1, 3), rowspan=1, colspan=1) ) # Create the colorbar on the histogram axes self.cbar = plots.draw_histogram_colorbar(ax=self.hist_ax, cmap="viridis", norm=Normalize(0, 1)) self.cbar.ax.set_xlabel("Map Value") # Adjust the margins self.fig.tight_layout(pad=0) self.fig.canvas.draw_idle()
def get_stereo_rose(self): """ Resets the figure and returns a stereonet and rose diagram axis. When the view in the main window is changed to stereonet and rose diagram, the figure is reset. The current settings are applied and two subplots for the stereonet and rose diagram are created. The axis of the stereonet and rose diagram are returned. This method is called by the MainWindow "redraw_plot"-method. """ self.fig.clf() self.fig.patch.set_facecolor(self.props["canvas_color"]) self.fig.set_dpi(self.props["pixel_density"]) gridspec = GridSpec(2, 5) sp_stereo = gridspec.new_subplotspec((0, 0), rowspan=2, colspan=2) sp_cbar = gridspec.new_subplotspec((1, 2), rowspan=1, colspan=1) sp_rose = gridspec.new_subplotspec((0, 3), rowspan=2, colspan=2) ax_stereo = self.fig.add_subplot(sp_stereo, projection=self.get_projection()) ax_rose = self.fig.add_subplot(sp_rose, projection="northpolar") ax_cbar = self.fig.add_subplot(sp_cbar) ax_cbar.axis("off") ax_cbar.set_aspect(8) return ax_stereo, ax_rose, ax_cbar
def get_stereo_two_rose(self): """ Resets the figure and returns a stereonet two rose diagrams axis. When the view in the main window is changed to this setting, this function is called and sets up a plot with a stereonet and two rose diagram axis. One axis is for azimuth, the other one for dip. """ self.fig.clf() self.fig.patch.set_facecolor(self.props["canvas_color"]) self.fig.set_dpi(self.props["pixel_density"]) gridspec = GridSpec(2, 4) sp_stereo = gridspec.new_subplotspec((0, 0), rowspan=2, colspan=2) sp_cbar = gridspec.new_subplotspec((1, 2), rowspan=1, colspan=1) sp_rose = gridspec.new_subplotspec((0, 3), rowspan=1, colspan=1) sp_drose = gridspec.new_subplotspec((1, 3), rowspan=1, colspan=1) ax_stereo = self.fig.add_subplot(sp_stereo, projection=self.get_projection()) ax_rose = self.fig.add_subplot(sp_rose, projection="northpolar") ax_drose = self.fig.add_subplot(sp_drose, projection="dippolar") ax_cbar = self.fig.add_subplot(sp_cbar) ax_cbar.axis("off") ax_cbar.set_aspect(8) return ax_stereo, ax_rose, ax_drose, ax_cbar
def create_canvas(self): # Add the canvas to the UI self.fig = Figure() canvas = FigureCanvas(self.fig) self.ui.mainLayout.addWidget(canvas) # Add subplots gridspec = GridSpec(2, 4) self.img_ax = self.fig.add_subplot( gridspec.new_subplotspec((0, 0), rowspan=2, colspan=2) ) self.spectrum_ax = self.fig.add_subplot( gridspec.new_subplotspec((0, 2), rowspan=1, colspan=2) ) self.hist_ax = self.fig.add_subplot( gridspec.new_subplotspec((1, 2), rowspan=1, colspan=1) ) self.edge_ax = self.fig.add_subplot( gridspec.new_subplotspec((1, 3), rowspan=1, colspan=1) ) # Create the colorbar on the histogram axes self.cbar = plots.draw_histogram_colorbar(ax=self.hist_ax, cmap="viridis", norm=Normalize(0, 1)) self.cbar.ax.set_xlabel("Intensity") # Adjust the margins self.fig.tight_layout(pad=0) self.fig.canvas.draw_idle()
def plot_ext_laws(self): wave = np.arange(900, 20000) f0 = np.ones(wave.shape[0]) for law in ['calz', 'ccm', 'allen', 'prevot', 'seaton', 'fitz']: getattr(self, law)(wave, f0, 1.) self.wild(wave) fig = plt.figure() gs = GridSpec(1,1) gs.update(left=0.12, right=0.95, top=0.95, bottom=0.12) ax = fig.add_subplot(gs[0]) ax.semilogx(wave, self.calz_klam, 'c', lw=1.5, label='Calzetti') # ax.semilogx(wave, self.ccm_klam, 'k', label='Cardelli') ax.semilogx(wave, self.allen_klam, 'r', lw=1.5, label='Allen') ax.semilogx(wave, self.prevot_klam, 'g', lw=1.5, label='Prevot') ax.semilogx(wave, self.seaton_klam, 'm', lw=1.5, label='Seaton') ax.semilogx(wave, self.fitz_klam, 'b', lw=1.5, label='Fitzpatrick') ax.legend(frameon=False) for axis in ['top', 'bottom', 'left', 'right']: ax.spines[axis].set_linewidth(1.5) ax.set_ylabel(r'$k(\lambda)$', fontsize=20) ax.set_xlabel(r'$\lambda [\AA]$', fontsize=20) ax.set_xlim(9e2, 2.5e4) ax.set_ylim(0, 20) plt.savefig('extlaw.pdf')
def nbo_vs_year(self): """ Plot percent of structures, with different NBO per 1000 atoms levels, from "good" pdb structures (all PDB files with a single model, no unknown atom types and good CRYST1 records) VS. year Second sub plot: the total of "good" structures deposited VS. year """ plt.close('all') # figure parameters # plt.ion() # enables interactive mode max_y = 105 fontsize = 20 fig = plt.figure(figsize=(8,10)) gs = GridSpec(2,1,height_ratios=[2,1]) # first subplot ax1 = plt.subplot(gs[0,0]) ax2 = plt.subplot(gs[1,0]) lines = [] line_type = ['.:','.-','.--'] n = len(line_type) for i,pd in enumerate(self.plot_data_list): lt = line_type[i%n] l, = ax1.plot(pd.years,pd.percent,lt) lines.append(l) ax1.set_ylabel('Percent of PDB structures',fontsize=fontsize) ax1.text(min(self.years)+0.5,max_y-4,'a.',fontsize=fontsize) ax1.tick_params(axis='both',labelsize=fontsize - 2) ax1.axes.get_xaxis().set_visible(False) ax1.set_yticks([5,10,40,70,100]) ax1.set_ylim([0,max_y]) ax1.set_xlim([self.start_year,self.end_year]) # legend labels = ['NBO per 1000 atom > {}']*len(self.nbo_per_1000_atoms) labels = [x.format(y) for x,y in zip(labels,self.nbo_per_1000_atoms)] if self.sym: legend_pos = [0.96,0.70] else: legend_pos = [0.54,0.30] ax1.legend( lines,labels, bbox_to_anchor=legend_pos, loc=1,borderaxespad=0.0) # Second subplot ax2.plot(self.years,self.n_total,'.:g') ax2.set_xlim([self.start_year,self.end_year]) ax2.set_xlabel('Year',fontsize=fontsize) ax2.set_ylabel('Number of structures',fontsize=fontsize) ax2.text(min(self.years)+0.5,max(self.n_total)-5,'b.',fontsize=fontsize) ax2.tick_params(axis='both',labelsize=fontsize - 2) ax2.set_xticks([self.start_year,1990,2000,self.end_year]) ax2.set_yscale('log') ax2.set_yticks([10,100,1000]) # gs.tight_layout(fig) gs.update(hspace=0) s = 'all'*(not self.sym) + 'sym'*self.sym fig_name = 'nbo_vs_year_{}.png'.format(s) plt.savefig(fig_name) fig.show()
def plot_fd(fd_file, fd_radius, mean_fd_dist=None, figsize=DINA4_LANDSCAPE): fd_power = _calc_fd(fd_file, fd_radius) fig = plt.Figure(figsize=figsize) FigureCanvas(fig) if mean_fd_dist: grid = GridSpec(2, 4) else: grid = GridSpec(1, 2, width_ratios=[3, 1]) grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) ax = fig.add_subplot(grid[0, :-1]) ax.plot(fd_power) ax.set_xlim((0, len(fd_power))) ax.set_ylabel("Frame Displacement [mm]") ax.set_xlabel("Frame number") ylim = ax.get_ylim() ax = fig.add_subplot(grid[0, -1]) sns.distplot(fd_power, vertical=True, ax=ax) ax.set_ylim(ylim) if mean_fd_dist: ax = fig.add_subplot(grid[1, :]) sns.distplot(mean_fd_dist, ax=ax) ax.set_xlabel("Mean Frame Displacement (over all subjects) [mm]") mean_fd = fd_power.mean() label = r'$\overline{{\text{{FD}}}}$ = {0:g}'.format(mean_fd) plot_vline(mean_fd, label, ax=ax) return fig
def main(): matplotlib.rc('font', size=12) fig = plt.figure(figsize=(16,9)) gs = GridSpec(2, 1, height_ratios=[20, 1])#, 20]) gs.update(hspace=0., wspace=0.) ax1 = plt.subplot(gs[0]) label_ax = plt.subplot(gs[1]) [ax.set_xlim(0, 599) for ax in (ax1, label_ax)] ax1.set_ylim(0, 350) # New way regiondict = dict(zip(range(1,600), ['MA']*(133-1) + ['CA']*(364-133) + ['p2']*(378-364) + ['NC']*(433-378) + ['p1']*(449-433) + ['p6']*(501-449) + ['PR']*(600-501))) N_lines = 50 muts = get_muts('gag-gag') + get_muts('gag-pr') muts = [mut for mut in muts if regiondict[mut[1]] != regiondict[mut[0]]] muts.sort(key=lambda x: x[-1], reverse=True) min_mi = muts[N_lines-1][2] counter = 0 for mut in muts[:N_lines]: r1, r2 = regiondict[mut[0]], regiondict[mut[1]] c = 'r' if r2 == 'PR' else 'b' ax1.add_patch(make_circ(*mut, ec=c)) counter += 1 print counter r = range(1) proxy1 = plt.Line2D(r, r, color='b', markerfacecolor='none', lw=3) proxy2 = plt.Line2D(r, r, color='r', markerfacecolor='none', lw=3) ax1.legend((proxy1, proxy2), ('Gag-Gag', 'Gag-PR')) # Add x-axis boxes locs = [(132, 'MA'), (363, 'CA'), (377, 'p2'), (432, 'NC'), (448, 'p1'), (500, 'p6'), (599, 'PR')] x_start = 0 colors = ('#AAAAAA', '#EEEEEE') for i, (junc, name) in enumerate(locs): color = colors[i%2] width = junc - x_start rect = patches.Rectangle((x_start, 0), width, 1, color=color) label_ax.add_patch(rect) label_ax.text(x_start + width/2., 1/2., name, ha='center', va='center') x_start = junc label_ax.set_xlim(0, 599) label_ax.set_xticks([1]+range(50, 650, 50)) label_ax.set_xticklabels([1]+range(50, 500, 50)+[1]+range(50, 150, 50)) [plt.setp(ax.get_xticklabels(), visible=False) for ax in (ax1, )] [plt.setp(ax.get_yticklabels(), visible=False) for ax in (ax1, label_ax)] [ax.tick_params(top=False, left=False, right=False, bottom=False) for ax in (ax1, label_ax)] ax1.tick_params(bottom=True) ax1.set_xticks(np.arange(0, 599, 10)) label_ax.set_xlabel('Sequence position') plt.show()
def initialize_figure(start_hour, stop_hour): f = plt.figure(figsize=(17, 21)) font_1 = font_0.copy() font_1.set_size('20') font_1.set_weight('bold') plt.suptitle(u"Schemat wyznaczania HRA dla pojedynczego 24-godzinnego nagrania odstępów RR.", fontproperties=font_1, y=0.995, fontsize=25) empty_ratio = 0.2 height_ratios = [ 0.5, #1 24-tachogram 0.3, #2 plot 24h -> 2h pass 0.9, #3 2-hour tachogram empty_ratio, #4 empty 0.45, #5 5-min window arrow plot #empty_ratio, #6 0.9, #7 2-hour windowed tachogram empty_ratio, #8 empty plot 0.55, #9 calculate descriptors arrow plot empty_ratio, #10 empty plot 0.9, #11 2-hour windowed tachogram with asymmetry signs 2.0 #12 schema for binomial test ] num_rows = len(height_ratios) num_cols = 1 row_number = 0 gs1 = GridSpec(num_rows, num_cols, height_ratios=height_ratios) #[3, 0.3, 3, 0.5, 4]) gs1.update(left=0.04, right=0.99, wspace=0.1, hspace=0.0, bottom=0.04, top=0.98) return f, gs1
def __init__(self, main_window, settings, data, add_layer_dataset, add_feature, redraw_main): """ Initializes the RotationDialog class. Requires the main_window object, the settings object (PlotSettings class) and the data rows to initialize. All the necessary widgets are loaded from the Glade file. A matplotlib figure is set up and added to the scrolledwindow. Two axes are set up that show the original and rotated data. """ self.builder = Gtk.Builder() self.builder.set_translation_domain(i18n().get_ts_domain()) script_dir = os.path.dirname(__file__) rel_path = "gui_layout.glade" abs_path = os.path.join(script_dir, rel_path) self.builder.add_objects_from_file(abs_path, ("dialog_rotation", "adjustment_rotation_dipdir", "adjustment_rotation_dip", "adjustment_rotation_angle")) self.dialog = self.builder.get_object("dialog_rotation") self.dialog.set_transient_for(main_window) self.settings = settings self.data = data self.trans = self.settings.get_transform() self.add_layer_dataset = add_layer_dataset self.add_feature = add_feature self.redraw_main = redraw_main self.adjustment_rotation_dipdir = self.builder.get_object("adjustment_rotation_dipdir") self.adjustment_rotation_dip = self.builder.get_object("adjustment_rotation_dip") self.adjustment_rotation_angle = self.builder.get_object("adjustment_rotation_angle") self.spinbutton_rotation_dipdir = self.builder.get_object("spinbutton_rotation_dipdir") self.spinbutton_rotation_dip = self.builder.get_object("spinbutton_rotation_dip") self.spinbutton_rotation_angle = self.builder.get_object("spinbutton_rotation_angle") self.scrolledwindow_rotate = self.builder.get_object("scrolledwindow_rotate") self.fig = Figure(dpi=self.settings.get_pixel_density()) self.canvas = FigureCanvas(self.fig) self.scrolledwindow_rotate.add_with_viewport(self.canvas) gridspec = GridSpec(1, 2) original_sp = gridspec.new_subplotspec((0, 0), rowspan=1, colspan=1) rotated_sp = gridspec.new_subplotspec((0, 1), rowspan=1, colspan=1) self.original_ax = self.fig.add_subplot(original_sp, projection=self.settings.get_projection()) self.rotated_ax = self.fig.add_subplot(rotated_sp, projection=self.settings.get_projection()) self.canvas.draw() self.redraw_plot() self.dialog.show_all() self.builder.connect_signals(self) if sys.platform == "win32": translate_gui(self.builder)
def gridplot (grid, loc, rowspan=1, colspan=1): ''' Returns a matplotlib.gridspec.SubplotSpec for a subplot. The resulting object can then be added to a matplotlib.figure using the add_subplot() method. ''' gridspec = GridSpec (grid[0], grid[1]) subplotspec = gridspec.new_subplotspec(loc, rowspan, colspan) return subplotspec
def plot_flux_decomposition(ss, tm, name=None, source_code=None): f = plt.figure(figsize=(18, 6)) gs = GridSpec(2, 3, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[0, 2]) ax4 = plt.subplot(gs[1, 0]) ax5 = plt.subplot(gs[1, 1]) ax6 = plt.subplot(gs[1, 2]) first_surface = int(len(ss) / 2) ax1.scatter(range(first_surface), ss[0:first_surface], s=80, c="r") ax1.set_title("First part of steady state eigenvector") ax1.set_ylabel("Population") ax2.scatter(range(first_surface), [tm[i][i + first_surface] for i in range(first_surface)], s=80, c="r") ax2.set_title("Transition probabilities (unbound to bound)") ax2.set_ylabel("Probability") ax3.scatter(range(first_surface), [ss[i] * tm[i][i + first_surface] for i in range(first_surface)], s=80, c="r") ax3.set_title("Steady state eigenvector * transition probabilities (unbound to bound)") ax3.set_ylabel("Population * Probability") ax4.scatter(range(first_surface, 2 * first_surface), ss[first_surface : 2 * first_surface], s=80, c="b") ax4.set_title("Second part of steady state eigenvector") ax4.set_ylabel("Population") ax5.scatter(range(first_surface), [tm[i + first_surface][i] for i in range(first_surface)], s=80, c="b") ax5.set_title("Transition probabilities (bound to unbound)") ax5.set_ylabel("Probability") ax6.scatter( range(first_surface, 2 * first_surface), [ss[i] * tm[i + first_surface][i] for i in range(first_surface)], s=80, c="b", ) ax6.set_title("Steady state eigenvector * transition probabilities (bound to unbound)") ax6.set_ylabel("Population * Probability") st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) plt.savefig("Figures/flux-decomposition-{}-{}".format(sts, name), dpi=150) # plt.show() plt.close()
def make_figure(self, type): self.fig.clf() if type == 'gd': pass elif type == 'mc': gs = GridSpec(1, 1) gs.update(hspace=0.7, wspace=0.8) self.splts = [self.fig.add_subplot(gs[int(i/3), int(i%3)]) for i in range(1*1)] # grid nxn else: pass
def plot_steady_state(unbound, bound, pdf_unbound, pdf_bound, ss, name=None, source_code=None): f = plt.figure(figsize=(12, 12)) gs = GridSpec(2, 2, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[1, 0]) ax4 = plt.subplot(gs[1, 1]) ax1.scatter(range(len(unbound)), unbound, s=80, c="r") ax1.plot(range(len(unbound)), unbound, lw=2, c="r") ax2.scatter(range(len(bound)), bound, s=80, c="b") ax2.plot(range(len(bound)), bound, lw=2, c="b") ax1.set_title("Unbound energy surface") ax2.set_title("Bound energy surface") bins = int(len(ss) / 2) ax3.plot(range(len(unbound)), ss[0:bins], lw=2, c="r") ax3.scatter(range(len(unbound)), ss[0:bins], s=80, c="r") ax3.plot(range(len(pdf_unbound)), pdf_unbound, lw=4, ls="--", c="k") ax3.set_title("Boltzmann and forward s.s.") ax4.plot(range(len(bound)), ss[bins : 2 * bins], lw=2, c="b") ax4.scatter(range(len(bound)), ss[bins : 2 * bins], s=80, c="b") ax4.plot(range(len(pdf_bound)), pdf_bound, lw=4, ls="--", c="k") ax4.set_title("Boltzmann and forward s.s.") ax4.set_xlabel("Reaction coordinate ($\phi$)") ax3.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Energy (a.u.)") ax2.set_ylabel("Energy (a.u.)") ax3.set_ylabel("Population") ax4.set_ylabel("Population") ax1.set_ylim([-15, 20]) ax2.set_ylim([-15, 20]) ax3.set_ylim([0, 0.6]) ax4.set_ylim([0, 0.6]) st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) # plt.show() plt.savefig("Figures/steady-state-{}-{}".format(sts, name), dpi=150) plt.close()
def plot_standard(corr="acorr"): os.chdir(tables_dir) ref = np.loadtxt("stars_lick_val_{0}.txt".format(corr)).T obs = np.loadtxt("stars_lick_obs_{0}.txt".format(corr)).T bands = np.loadtxt("bands_matching_standards.txt", usecols=(0), dtype=str).tolist() bands2, units, error = np.loadtxt("bands.txt", usecols=(0,9,10), dtype=str).T idx = [list(bands2).index(x) for x in bands] idx2 = np.array([list(bands).index(x) for x in bands2]) error = error[idx] units = units[idx] units = [x.replace("Ang", "\AA") for x in units] fig = plt.figure(1, figsize=(20,12)) gs = GridSpec(5,5) gs.update(left=0.03, right=0.988, top=0.98, bottom=0.06, wspace=0.2, hspace=0.4) offsets, errs = [], [] for i in range(25): ax = plt.subplot(gs[i]) plt.locator_params(axis="y", nbins=6) plt.locator_params(axis="x", nbins=6) ax.minorticks_on() # ax.plot(obs[i], ref[i] - obs[i], "ok") ax.axhline(y=0, ls="--", c="k") diff = ref[i] - obs[i] diff, c1, c2 = sigmaclip(diff[np.isfinite(diff)], 2.5, 2.5) ax.hist(diff, bins=8, color="0.7", histtype='stepfilled') ylim = plt.ylim() xlim = plt.xlim() xlim = np.max(np.abs(xlim)) ax.set_ylim(0, ylim[1] + 2) ax.set_xlim(-xlim, xlim) mean = np.nanmean(diff) N = len(diff) err = np.nanstd(diff) / np.sqrt(N) lab = "${0:.2f}\pm{1:.2f}$".format(mean, err) ax.axvline(x=mean, ls="-", c="r", label=lab) ax.axvline(x=0, ls="--", c="k") # ax.axhline(y=float(error[i])) # ax.axhline(y=-float(error[i])) # ax.set_xlabel("{0} ({1})".format(bands[i].replace("_", " "), units[i])) ax.legend(loc=1,prop={'size':12}) ax.set_xlabel("$\Delta$ {0} ({1})".format(bands[i].replace("_", " "), units[i])) ax.set_ylabel("Frequency") offsets.append(mean) errs.append(err) offsets = np.array(offsets)[idx2] errs = np.array(errs)[idx2] output = os.path.join(home, "plots/lick_stars_{0}.png".format(corr)) plt.savefig(output) with open(os.path.join(tables_dir, "lick_offsets.txt"), "w") as f: f.write("# Index Additive Correction\n") np.savetxt(f, np.column_stack((np.array(bands)[idx2],offsets, errs)), fmt="%s")
def kplot(curcode,startdatetime,show_log,title="none",ymarks=[]): df='%Y-%m-%d %H:%M:%S' dt=datetime.strptime(startdatetime,df) # fig,ax = plt.subplots(nrows=3,ncols=1, figsize=(12,5),facecolor='#D3D3D3') fig = plt.figure(figsize=(12,5),facecolor='#D3D3D3') fig.suptitle(title) gs1 = GridSpec(8, 8) gs1.update(left=0.05, right=0.95, wspace=0.05) ax1 = plt.subplot(gs1[0:5, :]) ax2 = plt.subplot(gs1[6:8, 0:3]) ax3 = plt.subplot(gs1[6:8, 4:8]) #day ax2.set_title("days") days_range=30 table=curcode+"_1day" kdatas=kdb.query(table,(dt-timedelta(days=days_range)).strftime(df),(dt+timedelta(days=days_range)).strftime(df)) while len(kdatas) < 60: days_range +=5 kdatas=kdb.query(table,(dt-timedelta(days=days_range)).strftime(df),(dt+timedelta(days=days_range)).strftime(df)) kaxplot(ax2,dt.replace(hour=0,minute=0,second=0),'%m-%d',kdatas,ymarks) #hour ax1.set_title("hours") hours_range=60 table=curcode+"_1hour" kdatas=kdb.query(table,(dt-timedelta(hours=hours_range)).strftime(df),(dt+timedelta(hours=hours_range)).strftime(df)) while len(kdatas) < 120: hours_range +=10 kdatas=kdb.query(table,(dt-timedelta(hours=hours_range)).strftime(df),(dt+timedelta(hours=hours_range)).strftime(df)) kaxplot(ax1,dt.replace(minute=0,second=0),'%Y-%m-%d %H',kdatas,ymarks) #min ax3.set_title("minutes") mins_range=50 table=curcode+"_1min" kdatas=kdb.query(table,(dt-timedelta(minutes=mins_range)).strftime(df),(dt+timedelta(minutes=mins_range)).strftime(df)) while len(kdatas) < 100: mins_range +=10 kdatas=kdb.query(table,(dt-timedelta(minutes=mins_range)).strftime(df),(dt+timedelta(minutes=mins_range)).strftime(df)) kaxplot(ax3,dt.replace(second=0),'%H:%M',kdatas,ymarks) #show or log if show_log == "show" : plt.show() else: if not os.path.exists(show_log): os.makedirs(show_log) plt.savefig(show_log+startdatetime.replace(":","%")+".png") plt.close('all')
def plot_series(data, order='co', fig=None, subplot_spec=None): ''' Parameters ---------- data : ndarray shape (26=ntask, nbin) order : string, optional one of 'co' or 'oc', for center-out or out-center data order determines mapping of data to polygons defaults to center-out fig : matplotlib Figure instance an existing figure to use, optional subplotspec : matplotlib SubplotSpec instance, optional an existing subplotspec to use, i.e. subset of a gridspec ''' if not ((np.rank(data) == 2) & (data.shape[0] == 26)): raise ValueError('data has wrong shape; should be (26, nbin),' 'actually is %s' % (str(data.shape))) if order == 'co': mapping = get_targ_co_dir_mapping() elif order == 'oc': mapping = get_targ_oc_dir_mapping() else: raise ValueError('`order` not recognized') if (fig != None) & (not isinstance(fig, Figure)): raise ValueError('`fig` must be an instance of Figure') if (fig == None): fig = plt.figure() if (subplot_spec != None): if not isinstance(subplot_spec, SubplotSpec): raise ValueError('subplot_spec must be instance of ' 'SubplotSpec') ntask, nbin = data.shape clim = (np.nanmin(data), np.nanmax(data)) if subplot_spec == None: gs = GridSpec(nbin + 1,1, height_ratios=[1,] * nbin + [0.5,]) gs.update(left=0.02, right=0.98, top=0.98, bottom=0.05) else: gs = GridSpecFromSubplotSpec(\ nbin + 1,1, subplot_spec=subplot_spec, \ height_ratios=[1,] * nbin + [0.5,]) for i in xrange(nbin): ax = fig.add_subplot(gs[i], projection='split_lambert') plot_gem(data[:,i], order=order, ax=ax, clim=clim) cbax = fig.add_subplot(gs[-1], aspect=.25) cb = plt.colorbar(ax.collections[0], cbax, orientation='horizontal') clim = cb.get_clim() cb.set_ticks(clim) cb.set_ticklabels(['%0.1f' % x for x in clim]) return fig
def __init__(self, window): """Initialize spectrogram canvas graphs.""" # Initialize variables to default values. self.window = window self.samples = 100 # Number of samples to store self.fftSize = 256 # Initial FFT size just to render something in the charts self.sampleRate = 0 self.binFreq = 0 self.binCount = self.fftSize/2 self.graphUpdateHz = 10 # Update rate of the animation self.coloredBin = None self.magnitudes = np.zeros((self.samples, self.binCount)) # Tell numpy to ignore errors like taking the log of 0 np.seterr(all='ignore') # Set up figure to hold plots self.figure = Figure(figsize=(1024,768), dpi=72, facecolor=(1,1,1), edgecolor=(0,0,0)) # Set up 2x1 grid to hold initial plots gs = GridSpec(2, 1, height_ratios=[1,2], width_ratios=[1]) gs.update(left=0.075, right=0.925, bottom=0.05, top=0.95, wspace=0.05) # Set up frequency histogram bar plot self.histAx = self.figure.add_subplot(gs[0]) self.histAx.set_title('Frequency Histogram') self.histAx.set_ylabel('Intensity (decibels)') self.histAx.set_xlabel('Frequency Bin (hz)') self.histAx.set_xticks([]) self.histPlot = self.histAx.bar(np.arange(self.binCount), np.zeros(self.binCount), width=1.0, linewidth=0.0, facecolor='blue') # Set up spectrogram waterfall plot self.spectAx = self.figure.add_subplot(gs[1]) self.spectAx.set_title('Spectrogram') self.spectAx.set_ylabel('Sample Age (seconds)') self.spectAx.set_xlabel('Frequency Bin (hz)') self.spectAx.set_xticks([]) self.spectPlot = self.spectAx.imshow(self.magnitudes, aspect='auto', cmap=get_cmap('jet')) # Add formatter to translate position to age in seconds self.spectAx.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: '%d' % (x*(1.0/self.graphUpdateHz)))) # Set up spectrogram color bar #cbAx = self.figure.add_subplot(gs[3]) #self.figure.colorbar(self.spectPlot, cax=cbAx, use_gridspec=True, format=FuncFormatter(lambda x, pos: '%d' % (x*100.0))) #cbAx.set_ylabel('Intensity (decibels)') # Initialize canvas super(SpectrogramCanvas, self).__init__(self.figure) # Hook up mouse and animation events self.mpl_connect('motion_notify_event', self._mouseMove) self.ani = FuncAnimation(self.figure, self._update, interval=1000.0/self.graphUpdateHz, blit=False)
def plot(self, *args, **kwargs): g_idx = self.myc['g_idx'] neuron_idx = self.myc['neuron_idx'] l, b, r, t = self.myc['bbox_rect'] fig = self._get_final_fig(self.myc['fig_size']) gs = GridSpec(2, 2) gs.update(left=l, right=r, bottom=b, top=t, hspace=0) # E-->I outgoing ax = fig.add_subplot(gs[0, 0]) self.plotOutgoing(g_idx, "E", neuron_idx, ax=ax, xlabel='', ylabel='', use_title=False) ax.set_xticks([]) # I-->E input ax = fig.add_subplot(gs[0, 1]) self.plotIncoming(g_idx, "E", neuron_idx, ax=ax, ylabel='', xlabel='', use_title=False) ax.set_xticks([]) ax.set_yticks([]) # I-->E outgoing ax = fig.add_subplot(gs[1, 0]) self.plotOutgoing(g_idx, "I", neuron_idx, ax=ax, use_title=False, xlabel='', ylabel='') # E-->I input ax = fig.add_subplot(gs[1, 1]) self.plotIncoming(g_idx, "I", neuron_idx, ax=ax, xlabel='', ylabel='', use_title=False) ax.set_yticks([]) fname = self.get_fname("/connections_pcolor_grid.pdf") plt.savefig(fname, dpi=300, transparent=True) plt.close() # Add an extra colorbar fig = self._get_final_fig(self.myc['cbar_fig_size']) ax_cbar = fig.add_axes([0.05, 0.80, 0.8, 0.15]) cbar = mpl.colorbar.ColorbarBase(ax_cbar, cmap=mpl.cm.jet, norm=mpl.colors.Normalize(vmin=0, vmax=1), ticks=[0, 1], orientation='horizontal') ax_cbar.xaxis.set_ticklabels(['0', '$g_{E/I}$']) fname_cbar = self.get_fname("/connections_pcolor_grid_colorbar.pdf") plt.savefig(fname_cbar, dpi=300, transparent=True) plt.close()
def test_lector(): os.chdir(os.path.join(home, "MILES")) bands = os.path.join(tables_dir, "bands.txt") filename = "lector_tmputH9bu.list_LINE" stars = np.loadtxt(filename, usecols=(0,), dtype=str) ref = np.loadtxt(filename, usecols=(2,3,4,5,6,7,8,9,14,15,16,17,18,24,25,26, 27,28,29,30,31,32,33,34,35)) obs = [] from lick import Lick for i, star in enumerate(stars): print star + ".fits" spec = pf.getdata(star + ".fits") h = pf.getheader(star + ".fits") w = h["CRVAL1"] + h["CDELT1"] * \ (np.arange(h["NAXIS1"]) + 1 - h["CRPIX1"]) lick, tmp = lector.lector(w, spec, np.ones_like(w), bands, interp_kind="linear") ll = Lick(w, spec, np.loadtxt(bands, usecols=(2,3,4,5,6,7,))) obs.append(ll.classic) obs = np.array(obs) fig = plt.figure(1, figsize=(20,12)) gs = GridSpec(5,5) gs.update(left=0.08, right=0.98, top=0.98, bottom=0.06, wspace=0.25, hspace=0.4) obs = obs.T ref = ref.T names = np.loadtxt(bands, usecols=(0,), dtype=str) units = np.loadtxt(bands, usecols=(9,), dtype=str).tolist() # units = [x.replace("Ang", "\AA") for x in units] for i in range(25): ax = plt.subplot(gs[i]) plt.locator_params(axis="x", nbins=6) ax.minorticks_on() ax.plot(obs[i], (obs[i] - ref[i]) / ref[i], "o", color="0.5") ax.axhline(y=0, ls="--", c="k") lab = "median $= {0:.3f}$".format( np.nanmedian(obs[i] - ref[i])).replace("-0.00", "0.00") ax.axhline(y=np.nanmedian(obs[i] - ref[i]), ls="--", c="r", label=lab) ax.set_xlabel("{0} ({1})".format(names[i].replace("_", " "), units[i])) ax.legend(loc=1,prop={'size':15}) ax.set_ylim(-0.01, 0.01) fig.text(0.02, 0.5, 'I$_{{\\rm pylector}}$ - I$_{{\\rm lector}}$', va='center', rotation='vertical', size=40) output = os.path.join(home, "plots/test_lector.png") plt.show() plt.savefig(output)
def BuildPanel(self): """ builds basic GUI panel and popup menu""" self.fig = Figure(self.figsize, dpi=self.dpi) # 1 axes for now self.gridspec = GridSpec(1,1) self.axes = self.fig.add_subplot(self.gridspec[0], axisbg=self.axisbg) self.canvas = FigureCanvas(self, -1, self.fig) # modification of the canvas.draw function that is all over the libs self._updateCanvasDraw() if sys.platform.lower().startswith('darw'): def swallow_mouse(*args): pass self.canvas.CaptureMouse = swallow_mouse self.printer.canvas = self.canvas self.set_bg() self.conf.canvas = self.canvas self.canvas.SetCursor(wx.StockCursor(wx.CURSOR_CROSS)) # overwrite ScalarFormatter from ticker.py here: self.axes.xaxis.set_major_formatter(FuncFormatter(self.xformatter)) self.axes.yaxis.set_major_formatter(FuncFormatter(self.yformatter)) # This way of adding to sizer allows resizing sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.canvas, 2, wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND, 0) self.SetAutoLayout(True) self.SetSizer(sizer) self.Fit() self.addCanvasEvents()
def BuildPanel(self): """ builds basic GUI panel and popup menu""" self.fig = Figure(self.figsize, dpi=self.dpi) # 1 axes for now self.gridspec = GridSpec(1,1) self.axes = self.fig.add_subplot(self.gridspec[0], axisbg=self.axisbg) self.canvas = FigureCanvas(self, -1, self.fig) self.printer.canvas = self.canvas self.set_bg() self.conf.canvas = self.canvas self.canvas.SetCursor(wxCursor(wx.CURSOR_CROSS)) self.canvas.mpl_connect("pick_event", self.__onPickEvent) # overwrite ScalarFormatter from ticker.py here: self.axes.xaxis.set_major_formatter(FuncFormatter(self.xformatter)) self.axes.yaxis.set_major_formatter(FuncFormatter(self.yformatter)) # This way of adding to sizer allows resizing sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.canvas, 2, wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND, 0) self.SetAutoLayout(True) self.autoset_margins() self.SetSizer(sizer) self.Fit() canvas_draw = self.canvas.draw def draw(*args, **kws): self.autoset_margins() canvas_draw(*args, **kws) self.canvas.draw = draw self.addCanvasEvents()
def get_rose_diagram(self): """ Resets the figure and returns the rose diagram axis. When the view in the main window is changed to rose-diagram-only the figure is reset. The current settings are applied and one subplot for the rose diagram is created. The axis of the rose-diagram is returned. This method is called by the MainWindow "redraw_plot"-method. """ self.fig.clf() self.fig.patch.set_facecolor(self.props["canvas_color"]) self.fig.set_dpi(self.props["pixel_density"]) gridspec = GridSpec(1, 1) sp_rose = gridspec.new_subplotspec((0, 0)) ax_rose = self.fig.add_subplot(sp_rose, projection="northpolar") return ax_rose
def make_split(ratio, gap=0.12): import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec from matplotlib.ticker import MaxNLocator cax = plt.gca() box = cax.get_position() xmin, ymin = box.xmin, box.ymin xmax, ymax = box.xmax, box.ymax gs = GridSpec(2, 1, height_ratios=[ratio, 1 - ratio], left=xmin, right=xmax, bottom=ymin, top=ymax) gs.update(hspace=gap) ax = plt.subplot(gs[0]) plt.setp(ax.get_xticklabels(), visible=False) bx = plt.subplot(gs[1], sharex=ax) return ax, bx
def test_blank_subplots(): fig = plt.figure() gs = GridSpec(4, 6) ax1 = fig.add_subplot(gs[0,1]) ax1.plot(D['x1'], D['y1']) fig.add_subplot(gs[1,1]) fig.add_subplot(gs[2:,1]) fig.add_subplot(gs[0,2:]) fig.add_subplot(gs[1:3, 2:4]) fig.add_subplot(gs[3, 2:5]) fig.add_subplot(gs[1:3,4:]) fig.add_subplot(gs[3,5]) gs.update(hspace=.6, wspace=.6) renderer = run_fig(fig) equivalent, msg = compare_dict(renderer.layout, BLANK_SUBPLOTS['layout']) assert equivalent, msg
def get_stereonet(self): """ Resets the figure and returns the stereonet axis. When the view in the main window is changed to only stereoent. The figure is reset. Then the current settings are applied and one subplot for the stereonet is created. This method is called when the MainWindow "__init__"-method and the "redraw_plot"-method. """ self.fig.clf() self.fig.patch.set_facecolor(self.canvas_color) self.fig.set_dpi(self.pixel_density) gridspec = GridSpec(1, 1) sp_stereo = gridspec.new_subplotspec((0, 0)) ax_stereo = self.fig.add_subplot(sp_stereo, projection=self.get_projection()) return ax_stereo
def plot_confound(tseries, figsize, name, units=None, series_tr=None, normalize=False): """ A helper function to plot :abbr:`fMRI (functional MRI)` confounds. """ import matplotlib matplotlib.use(config.get('execution', 'matplotlib_backend')) import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec from matplotlib.backends.backend_pdf import FigureCanvasPdf as FigureCanvas import seaborn as sns fig = plt.Figure(figsize=figsize) FigureCanvas(fig) grid = GridSpec(1, 2, width_ratios=[3, 1], wspace=0.025) grid.update(hspace=1.0, right=0.95, left=0.1, bottom=0.2) ax = fig.add_subplot(grid[0, :-1]) if normalize and series_tr is not None: tseries /= series_tr ax.plot(tseries) ax.set_xlim((0, len(tseries))) ylabel = name if units is not None: ylabel += (' speed [{}/s]' if normalize else ' [{}]').format(units) ax.set_ylabel(ylabel) xlabel = 'Frame #' if series_tr is not None: xlabel = 'Frame # ({} sec TR)'.format(series_tr) ax.set_xlabel(xlabel) ylim = ax.get_ylim() ax = fig.add_subplot(grid[0, -1]) sns.distplot(tseries, vertical=True, ax=ax) ax.set_xlabel('Frames') ax.set_ylim(ylim) ax.set_yticklabels([]) return fig
def plot_flux( flux_unbound, unbound_scale, flux_bound, bound_scale, flux_between, between_scale, name=None, source_code=None ): f = plt.figure(figsize=(12, 12)) gs = GridSpec(2, 2, wspace=0.5, hspace=0.5) ax1 = plt.subplot(gs[0, 0]) ax2 = plt.subplot(gs[0, 1]) ax3 = plt.subplot(gs[1, 0]) # ax1.scatter(range(len(flux_unbound)), flux_unbound, s=80, c='r') ax1.plot(range(len(flux_unbound)), flux_unbound * 10 ** unbound_scale, lw=2, c="r") # ax2.scatter(range(len(flux_bound)), flux_bound, s=80, c='b') ax2.plot(range(len(flux_bound)), flux_bound * 10 ** bound_scale, lw=2, c="b") ax1.set_title("Unbound energy surface") ax2.set_title("Bound energy surface") # ax3.scatter(range(len(flux_between)), flux_between, s=80, c='k') ax3.autoscale(enable=False, axis="y") ax3.plot(range(len(flux_between)), flux_between * 10 ** between_scale, lw=4, ls="-", c="k") ax3.set_title("Flux between surfaces") ax3.set_xlabel("Reaction coordinate ($\phi$)") ax1.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(unbound_scale), size=20) ax2.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(bound_scale), size=20) ax3.set_ylabel("Flux ($\\times 10^{{{}}}$)".format(between_scale), size=20) st = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") sts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") if name: if source_code is not None: text = st + " " + name + " in " + source_code else: text = st + " " + name f.text(0.0, 0.0, text, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) else: f.text(0.0, 0.0, st, fontsize=12, color="black", ha="left", va="bottom", alpha=0.5) gs.tight_layout(f, rect=[0, 0, 1, 1]) plt.savefig("Figures/flux-{}-{}.png".format(sts, name), dpi=150) # plt.show() plt.close()
def myShow(X, Y, Z, xs = None, xs2 = None, xlabel = None, xlabel2=None, xdot=None, ydot=None, title = None): gs = GridSpec(1, 1) gs.update(top = 0.95, bottom = 0.02,left=0.02, right=0.98, hspace=0.2,wspace=0.05) ax = plt.subplot(gs[0, 0]) m = 30 V = np.array(range(10))**2 * np.sqrt(Z.max() - Z.min()) / np.float(m - 1) + Z.min() ax.contourf(X, Y, Z, V, alpha=.75, cmap=cm.RdBu) C = ax.contour(X, Y, Z, V, colors='black', linewidth=.3, alpha=0.5) # put the axes in the centre ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0)) # put white boxes around the labels for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(10) label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.35 )) if xdot != None and ydot != None : ax.plot([xdot,xdot],[ydot,ydot], marker='o', color='black') if xs != None: x = [] y = [] for xx,yy in xs: x.append(xx) y.append(yy) ax.plot(x, y, label=xlabel) if xs2 != None: x = [] y = [] for xx,yy in xs2: x.append(xx) y.append(yy) ax.plot(x, y, label=xlabel2) if xlabel != None: ax.legend(loc='upper left') if title !=None: ax.set_title(title, fontsize=18, position=(0.5, 1.01)) fig = plt.gcf() fig.set_size_inches(10, 5)
def __init__(self,scale,imshape,interval=1): self.fig = plt.figure(dpi=100) self.axes = {} self.artists = [] gridspec = GridSpec(2,2) self.axes['raw'] = gridspec.new_subplotspec((0, 0),rowspan=2) self.axes['draw'] = gridspec.new_subplotspec((0, 1)) self.axes['match'] = gridspec.new_subplotspec((1, 1)) for k,sp in self.axes.items(): self.axes[k] = self.fig.add_subplot(sp) self.axes['raw'].set_title('raw') self.axes['raw'].set_xticklabels([]) self.axes['raw'].set_yticklabels([]) self.axes['match'].grid(which='both') self.lines = {} self.lines['template'] = self.axes['match'].plot((),(),marker='x',color='g')[0] self.lines['query'] = self.axes['match'].plot((),(),marker='o',color='b')[0] self.lines['draw'] = self.axes['draw'].plot((),(),color='b',linewidth=5)[0] self.axes['match'].set_ylim(-scale//2-10,scale//2+10) self.axes['match'].set_xlim(-scale//2-10,scale//2+10) self.axes['draw'].set_ylim(imshape[0],0) self.axes['draw'].set_xlim(imshape[1],0) self.axes['draw'].xaxis.tick_top() self.axes['draw'].yaxis.tick_right() self.imdisp = self.axes['raw'].imshow(np.zeros(imshape,dtype=np.uint8)) self.fig.tight_layout() self.bg_cache = {ax:self.fig.canvas.copy_from_bbox(ax.bbox) for ax in self.fig.axes} self.draw_state = 0 self.drawcount = 0 self.fig.canvas.mpl_connect('button_press_event', self.onclick) self.fig.canvas.mpl_connect('key_press_event', self.onkey) self.timer = self.fig.canvas.new_timer(interval=interval) self.timer.add_callback(self._update) self.queuelen = 0
plt.xlabel("YEAR") plt.ylabel("SearchCount") plt.title("Python - Searches") java_plt = fig.add_subplot(232) java_plt.plot(keyword_searches_java, linewidth=0.5, color='orange') plt.xlabel("YEAR") plt.title("Java - Searches") r_plt = fig.add_subplot(233) r_plt.plot(keyword_searches_r, linewidth=0.5, color='green') plt.xlabel("YEAR") plt.title("R - Searches") from matplotlib.gridspec import GridSpec gs = GridSpec(2, 2, figure=fig) all_plt = fig.add_subplot(gs[1, :]) all_plt.plot(keyword_searches_df, linewidth=0.5) all_plt.legend(keyword_searches_df) plt.xlabel("YEAR") plt.ylabel("SearchCount") plt.title("Python/Java/R - Searches") plt.show() #"PYTHON Trendd vs Searches - Last 5 Years" keyword_searches_python_roll_avg = keyword_searches_python.rolling(52).mean() # print(keyword_searches_python_roll_avg) keyword_searches_python_trend = pd.concat( [keyword_searches_python, keyword_searches_python_roll_avg], axis=1) keyword_searches_python_trend.columns = ['PYTHON', 'PythonTrend']
def plot_abide_stripplots(inputs, figsize=(15, 2), out_file=None, rating_label='rater_1', dpi=100): import seaborn as sn from ..classifier.helper import FEATURE_NORM sn.set(style="whitegrid") mdata = [] pp_cols = [] for X, Y, sitename in inputs: sitedata, cols = read_dataset(X, Y, rate_label=rating_label, binarize=False, site_name=sitename) sitedata['database'] = [sitename] * len(sitedata) if sitename == 'DS030': sitedata['site'] = [sitename] * len(sitedata) mdata.append(sitedata) pp_cols.append(cols) mdata = pd.concat(mdata) pp_cols = pp_cols[0] for col in mdata.columns.ravel().tolist(): if col.startswith('rater_') and col != rating_label: del mdata[col] mdata = mdata.loc[mdata[rating_label].notnull()] for col in ['size_x', 'size_y', 'size_z', 'spacing_x', 'spacing_y', 'spacing_z']: del mdata[col] try: pp_cols.remove(col) except ValueError: pass zscored = BatchRobustScaler( by='site', columns=FEATURE_NORM).fit_transform(mdata) sites = list(set(mdata.site.values.ravel())) nsites = len(sites) # palette = ['dodgerblue', 'darkorange'] palette = ['limegreen', 'tomato'] if len(set(mdata[[rating_label]].values.ravel().tolist())) == 3: palette = ['tomato', 'gold', 'limegreen'] # pp_cols = pp_cols[:5] nrows = len(pp_cols) fig = plt.figure(figsize=(figsize[0], figsize[1] * nrows)) # ncols = 2 * (nsites - 1) + 2 gs = GridSpec(nrows, 4, wspace=0.02) gs.set_width_ratios([nsites, len(inputs), len(inputs), nsites]) for i, colname in enumerate(pp_cols): ax_nzs = plt.subplot(gs[i, 0]) axg_nzs = plt.subplot(gs[i, 1]) axg_zsc = plt.subplot(gs[i, 2]) ax_zsc = plt.subplot(gs[i, 3]) # plots sn.stripplot(x='site', y=colname, data=mdata, hue=rating_label, jitter=0.18, alpha=.6, split=True, palette=palette, ax=ax_nzs) sn.stripplot(x='site', y=colname, data=zscored, hue=rating_label, jitter=0.18, alpha=.6, split=True, palette=palette, ax=ax_zsc) sn.stripplot(x='database', y=colname, data=mdata, hue=rating_label, jitter=0.18, alpha=.6, split=True, palette=palette, ax=axg_nzs) sn.stripplot(x='database', y=colname, data=zscored, hue=rating_label, jitter=0.18, alpha=.6, split=True, palette=palette, ax=axg_zsc) ax_nzs.legend_.remove() ax_zsc.legend_.remove() axg_nzs.legend_.remove() axg_zsc.legend_.remove() if i == nrows - 1: ax_nzs.set_xticklabels(ax_nzs.xaxis.get_majorticklabels(), rotation=80) ax_zsc.set_xticklabels(ax_zsc.xaxis.get_majorticklabels(), rotation=80) axg_nzs.set_xticklabels(axg_nzs.xaxis.get_majorticklabels(), rotation=80) axg_zsc.set_xticklabels(axg_zsc.xaxis.get_majorticklabels(), rotation=80) else: ax_nzs.set_xticklabels([]) ax_zsc.set_xticklabels([]) axg_nzs.set_xticklabels([]) axg_zsc.set_xticklabels([]) ax_nzs.set_xlabel('', visible=False) ax_zsc.set_xlabel('', visible=False) ax_zsc.set_ylabel('', visible=False) ax_zsc.yaxis.tick_right() axg_nzs.set_yticklabels([]) axg_nzs.set_xlabel('', visible=False) axg_nzs.set_ylabel('', visible=False) axg_zsc.set_yticklabels([]) axg_zsc.set_xlabel('', visible=False) axg_zsc.set_ylabel('', visible=False) for yt in ax_nzs.yaxis.get_major_ticks()[1:-1]: yt.label1.set_visible(False) for yt in axg_nzs.yaxis.get_major_ticks()[1:-1]: yt.label1.set_visible(False) for yt in zip(ax_zsc.yaxis.get_majorticklabels(), axg_zsc.yaxis.get_majorticklabels()): yt[0].set_visible(False) yt[1].set_visible(False) if out_file is None: out_file = 'stripplot.svg' fname, ext = op.splitext(out_file) if ext[1:] not in ['pdf', 'svg', 'png']: ext = '.svg' out_file = fname + '.svg' fig.savefig(op.abspath(out_file), format=ext[1:], bbox_inches='tight', pad_inches=0, dpi=dpi) return fig
def __init__(self, parent, width=5, height=5): super(GroupOverviewFigure, self).__init__(parent, width, height) self.currentGroup = None grid = GridSpec(3, 3, left=.05, right=.97, top=.98, bottom=.05, hspace=.15, wspace=.2) self.meanDensAx = self.fig.add_subplot(grid[0]) self.meanDensLogAx = self.fig.add_subplot(grid[1]) self.meanAx = self.fig.add_subplot(grid[2]) self.isiAx = self.fig.add_subplot(grid[3]) self.cumSpikeAx = self.fig.add_subplot(grid[4]) self.maxDistrAx = self.fig.add_subplot(grid[5]) self.overTimeAx = self.fig.add_subplot(grid[6:9]) titles = ((self.meanDensAx, 'Density'), (self.meanDensLogAx, 'Log density'), (self.meanAx, 'Mean spikes'), (self.isiAx, 'Inter-spike intervals'), (self.maxDistrAx, 'Distr. of amplitudes'), (self.overTimeAx, 'Amplitude over time')) for t in titles: t[0].text(1, 1, t[1], transform=t[0].transAxes, backgroundcolor='w', horizontalalignment='right', verticalalignment='top') self.cumSpikeAx.text(0, 1, 'Cumulative spike count', transform=self.cumSpikeAx.transAxes, backgroundcolor='w', horizontalalignment='left', verticalalignment='top'), for p in (self.meanDensAx, self.meanDensLogAx): p.set_yticks([]) p.set_xticks([]) args = ((self.meanAx, 0, 1, u'µV', 'left', 'top'), (self.maxDistrAx, 1, 0, u'µV', 'right', 'bottom'), (self.overTimeAx, 1, 0, u'min', 'right', 'bottom'), (self.overTimeAx, 0, 1, u'µV', 'left', 'top'), (self.isiAx, 1, 0, u'ms', 'right', 'bottom'), (self.cumSpikeAx, 1, 0, 'min', 'right', 'bottom')) for a in args: a[0].text(a[1], a[2], a[3], transform=a[0].transAxes, backgroundcolor='w', horizontalalignment=a[4], verticalalignment=a[5]) self.cumSpikeAx.set_ylim(auto=True)
w = cell.somav[inds] k = inds[:-1][np.diff(w) == np.diff(w).max()][0] AP_train[k] = 1 #sample spike waveform for l in LFP: spw[n, ] = l[np.arange(k - samplelength, k + samplelength)] n += 1 #fill in sampled spike waveforms and times of spikes in comm_dict COMM_DICT.update({ os.path.split(NRN)[-1] + '_' + os.path.split(morphologyfile)[-1].strip('.asc'): dict(spw=spw, ) }) #plot gs = GridSpec(2, 3) fig = plt.figure(figsize=(10, 8)) fig.suptitle(NRN + '\n' + os.path.split(morphologyfile)[-1].strip('.asc')) #morphology zips = [] for x, z in cell.get_idx_polygons(projection=('x', 'z')): zips.append(zip(x, z)) polycol = PolyCollection(zips, edgecolors='none', facecolors='k', rasterized=True) ax = fig.add_subplot(gs[:, 0]) ax.add_collection(polycol) plt.plot(electrode.x, electrode.z, 'ro')
import numpy as np from matplotlib.gridspec import GridSpec from py_utilities import fp_library2 as fpl import plot_utilities as plt_util import scipy ROUND_TO_NEAREST = 10 colors = ['k', 'r', 'c', 'g', 'm'] summary_data_location = '/Users/tim/data/2p_summary_data/' summary_data_files_to_load = [ 'march27_unc-5_animal4.pck', 'march27_unc-5_animal3.pck', 'march27_unc-5_animal7.pck', 'unc-5_animal5.pck', 'unc-5_animal4.pck' ] fig = plt.figure(figsize=(.5, 1.5)) gs = GridSpec(3, 6, figure=fig) axraw = {} axnorm = {} axnorm = fig.add_subplot(gs[0:2, 0:3]) axsum = fig.add_subplot(gs[0:2, 4]) depth_for_analysis = {} depth_for_analysis['on'] = 0 depth_for_analysis['off'] = -20 def main(): mn_by_animal = {} for file_ind, crfile in enumerate(summary_data_files_to_load): dt = fh.open_pickle(summary_data_location + crfile)
def __init__(self, fig, *args, **kwargs): """ *fig* is a :class:`matplotlib.figure.Figure` instance. *args* is the tuple (*numRows*, *numCols*, *plotNum*), where the array of subplots in the figure has dimensions *numRows*, *numCols*, and where *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args) == 1: if isinstance(args[0], SubplotSpec): self._subplotspec = args[0] else: try: s = str(int(args[0])) rows, cols, num = map(int, s) except ValueError: raise ValueError('Single argument to subplot must be ' 'a 3-digit integer') self._subplotspec = GridSpec(rows, cols, figure=self.figure)[num - 1] # num - 1 for converting from MATLAB to python indexing elif len(args) == 3: rows, cols, num = args rows = int(rows) cols = int(cols) if rows <= 0: raise ValueError(f'Number of rows must be > 0, not {rows}') if cols <= 0: raise ValueError(f'Number of columns must be > 0, not {cols}') if isinstance(num, tuple) and len(num) == 2: num = [int(n) for n in num] self._subplotspec = GridSpec( rows, cols, figure=self.figure)[(num[0] - 1):num[1]] else: if num < 1 or num > rows * cols: raise ValueError( f"num must be 1 <= num <= {rows*cols}, not {num}") self._subplotspec = GridSpec(rows, cols, figure=self.figure)[int(num) - 1] # num - 1 for converting from MATLAB to python indexing else: raise ValueError(f'Illegal argument(s) to subplot: {args}') self.update_params() # _axes_class is set in the subplot_class_factory self._axes_class.__init__(self, fig, self.figbox, **kwargs) # add a layout box to this, for both the full axis, and the poss # of the axis. We need both because the axes may become smaller # due to parasitic axes and hence no longer fill the subplotspec. if self._subplotspec._layoutbox is None: self._layoutbox = None self._poslayoutbox = None else: name = self._subplotspec._layoutbox.name + '.ax' name = name + layoutbox.seq_id() self._layoutbox = layoutbox.LayoutBox( parent=self._subplotspec._layoutbox, name=name, artist=self) self._poslayoutbox = layoutbox.LayoutBox( parent=self._layoutbox, name=self._layoutbox.name + '.pos', pos=True, subplot=True, artist=self)
class SubplotBase(object): """ Base class for subplots, which are :class:`Axes` instances with additional methods to facilitate generating and manipulating a set of :class:`Axes` within a figure. """ def __init__(self, fig, *args, **kwargs): """ *fig* is a :class:`matplotlib.figure.Figure` instance. *args* is the tuple (*numRows*, *numCols*, *plotNum*), where the array of subplots in the figure has dimensions *numRows*, *numCols*, and where *plotNum* is the number of the subplot being created. *plotNum* starts at 1 in the upper left corner and increases to the right. If *numRows* <= *numCols* <= *plotNum* < 10, *args* can be the decimal integer *numRows* * 100 + *numCols* * 10 + *plotNum*. """ self.figure = fig if len(args) == 1: if isinstance(args[0], SubplotSpec): self._subplotspec = args[0] else: try: s = str(int(args[0])) rows, cols, num = map(int, s) except ValueError: raise ValueError('Single argument to subplot must be ' 'a 3-digit integer') self._subplotspec = GridSpec(rows, cols, figure=self.figure)[num - 1] # num - 1 for converting from MATLAB to python indexing elif len(args) == 3: rows, cols, num = args rows = int(rows) cols = int(cols) if rows <= 0: raise ValueError(f'Number of rows must be > 0, not {rows}') if cols <= 0: raise ValueError(f'Number of columns must be > 0, not {cols}') if isinstance(num, tuple) and len(num) == 2: num = [int(n) for n in num] self._subplotspec = GridSpec( rows, cols, figure=self.figure)[(num[0] - 1):num[1]] else: if num < 1 or num > rows * cols: raise ValueError( f"num must be 1 <= num <= {rows*cols}, not {num}") self._subplotspec = GridSpec(rows, cols, figure=self.figure)[int(num) - 1] # num - 1 for converting from MATLAB to python indexing else: raise ValueError(f'Illegal argument(s) to subplot: {args}') self.update_params() # _axes_class is set in the subplot_class_factory self._axes_class.__init__(self, fig, self.figbox, **kwargs) # add a layout box to this, for both the full axis, and the poss # of the axis. We need both because the axes may become smaller # due to parasitic axes and hence no longer fill the subplotspec. if self._subplotspec._layoutbox is None: self._layoutbox = None self._poslayoutbox = None else: name = self._subplotspec._layoutbox.name + '.ax' name = name + layoutbox.seq_id() self._layoutbox = layoutbox.LayoutBox( parent=self._subplotspec._layoutbox, name=name, artist=self) self._poslayoutbox = layoutbox.LayoutBox( parent=self._layoutbox, name=self._layoutbox.name + '.pos', pos=True, subplot=True, artist=self) def __reduce__(self): # get the first axes class which does not inherit from a subplotbase axes_class = next( c for c in type(self).__mro__ if issubclass(c, Axes) and not issubclass(c, SubplotBase)) return (_picklable_subplot_class_constructor, (axes_class, ), self.__getstate__()) def get_geometry(self): """get the subplot geometry, e.g., 2,2,3""" rows, cols, num1, num2 = self.get_subplotspec().get_geometry() return rows, cols, num1 + 1 # for compatibility # COVERAGE NOTE: Never used internally or from examples def change_geometry(self, numrows, numcols, num): """change subplot geometry, e.g., from 1,1,1 to 2,2,3""" self._subplotspec = GridSpec(numrows, numcols, figure=self.figure)[num - 1] self.update_params() self.set_position(self.figbox) def get_subplotspec(self): """get the SubplotSpec instance associated with the subplot""" return self._subplotspec def set_subplotspec(self, subplotspec): """set the SubplotSpec instance associated with the subplot""" self._subplotspec = subplotspec def get_gridspec(self): """get the GridSpec instance associated with the subplot""" return self._subplotspec.get_gridspec() def update_params(self): """update the subplot position from fig.subplotpars""" self.figbox, self.rowNum, self.colNum, self.numRows, self.numCols = \ self.get_subplotspec().get_position(self.figure, return_all=True) def is_first_col(self): return self.colNum == 0 def is_first_row(self): return self.rowNum == 0 def is_last_row(self): return self.rowNum == self.numRows - 1 def is_last_col(self): return self.colNum == self.numCols - 1 # COVERAGE NOTE: Never used internally. def label_outer(self): """Only show "outer" labels and tick labels. x-labels are only kept for subplots on the last row; y-labels only for subplots on the first column. """ lastrow = self.is_last_row() firstcol = self.is_first_col() if not lastrow: for label in self.get_xticklabels(which="both"): label.set_visible(False) self.get_xaxis().get_offset_text().set_visible(False) self.set_xlabel("") if not firstcol: for label in self.get_yticklabels(which="both"): label.set_visible(False) self.get_yaxis().get_offset_text().set_visible(False) self.set_ylabel("") def _make_twin_axes(self, *args, **kwargs): """ Make a twinx axes of self. This is used for twinx and twiny. """ if 'sharex' in kwargs and 'sharey' in kwargs: # The following line is added in v2.2 to avoid breaking Seaborn, # which currently uses this internal API. if kwargs["sharex"] is not self and kwargs["sharey"] is not self: raise ValueError("Twinned Axes may share only one axis") # The dance here with label is to force add_subplot() to create a new # Axes (by passing in a label never seen before). Note that this does # not affect plot reactivation by subplot() as twin axes can never be # reactivated by subplot(). sentinel = str(uuid.uuid4()) real_label = kwargs.pop("label", sentinel) twin = self.figure.add_subplot(self.get_subplotspec(), *args, label=sentinel, **kwargs) if real_label is not sentinel: twin.set_label(real_label) self.set_adjustable('datalim') twin.set_adjustable('datalim') if self._layoutbox is not None and twin._layoutbox is not None: # make the layout boxes be explicitly the same twin._layoutbox.constrain_same(self._layoutbox) twin._poslayoutbox.constrain_same(self._poslayoutbox) self._twinned_axes.join(self, twin) return twin
fpga = corr.katcp_wrapper.FpgaClient(HOST) time.sleep(0.1) if fpga.is_connected(): print 'DONE' else: print 'ERROR: Failed to connect to server %s!' % (HOST) sys.exit(0) fig = plt.figure(figsize=(12, 6)) '''ax1 = plt.subplot2grid((3,2), (0,0)) ax2 = plt.subplot2grid((3,2), (0,1)) ax3 = plt.subplot2grid((3,2), (1,0)) ax4 = plt.subplot2grid((3,2), (1,1)) ax5 = plt.subplot2grid((3,2), (2,0)) ax6 = plt.subplot2grid((3,2), (2,1))''' plt.suptitle("ADC Snapshot") gs1 = GridSpec(3, 2) #gs1.update(left=0.04, right=0.05, hspace=0.25) gs1.update(hspace=0.25) #ax2 = plt.subplot(gs1[0, 1]) #ax3 = plt.subplot(gs1[1, 0]) #ax4 = plt.subplot(gs1[1, 1]) #ax5 = plt.subplot(gs1[-1, 1]) #ax6 = plt.subplot(gs1[-1, -1]) fig.canvas.manager.window.after(200, myplot) plt.show()
''' ##### To make 1D plots of the distances vs. each parameter: #''' N_steps_sample = min(1000, len(active_params_steps_all)) i_steps_sample = np.random.choice( np.arange(len(active_params_steps_all)), N_steps_sample, replace=False ) #array of indices of a sample of optimization steps to be plotted for i, param in enumerate(active_params_names): fig = plt.figure(figsize=(16, 8)) plot = GridSpec(len(distances_names) + 1, 1, left=0.075, bottom=0.115, right=0.875, top=0.925, wspace=0.25, hspace=0) ax = plt.subplot(plot[0, 0]) i_sortx = np.argsort( active_params_best_all[:, i] ) #array of indices that would sort the array based on the current active parameter plt.plot(active_params_best_all[i_sortx, i], distance_tot_best_all[i_sortx], 'o-', color='r', label='Total') #to plot the best values for each run plt.scatter( active_params_steps_all[i_steps_sample, i], distance_tot_steps_all[i_steps_sample],
def display_results(target, image, boxes, masks, class_ids, scores=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(3, 3) ax = plt.subplot(gs[:, 1:]) target_ax = plt.subplot(gs[1, 0]) auto_show = True # Generate random colors colors = colors or visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height + 10, -10) ax.set_xlim(-10, width + 10) ax.axis('off') ax.set_title(title) target_height, target_width = target.shape[:2] target_ax.set_ylim(target_height + 10, -10) target_ax.set_xlim(-10, target_width + 10) target_ax.axis('off') # target_ax.set_title('target') masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] ax.text(x1, y1 + 8, caption, color='w', size=11, backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask(masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_ax.imshow(target.astype(np.uint8)) if auto_show: plt.show() return
""" import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec def annotate_axes(fig): for i, ax in enumerate(fig.axes): ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center") ax.tick_params(labelbottom=False, labelleft=False) fig = plt.figure() fig.suptitle("Controlling subplot sizes with width_ratios and height_ratios") gs = GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[4, 1]) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1]) ax3 = fig.add_subplot(gs[2]) ax4 = fig.add_subplot(gs[3]) annotate_axes(fig) fig = plt.figure() fig.suptitle("Controlling spacing around and between subplots") gs1 = GridSpec(3, 3, left=0.05, right=0.48, wspace=0.05) ax1 = fig.add_subplot(gs1[:-1, :]) ax2 = fig.add_subplot(gs1[-1, :-1]) ax3 = fig.add_subplot(gs1[-1, -1])
def plot_image_map_line_profile_using_interface_plane( image_data, heatmap_data_list, line_profile_data_list, interface_plane, atom_plane_list=None, data_scale=1, data_scale_z=1, atom_list=None, extra_marker_list=None, clim=None, plot_title='', vector_to_plot=None, rotate_atom_plane_list_90_degrees=False, prune_outer_values=False, figname="map_data.jpg"): """ Parameters ---------- atom_list : list of Atom_Position instances extra_marker_list : two arrays of x and y [[x_values], [y_values]] """ number_of_line_profiles = len(line_profile_data_list) figsize = (10, 18 + 2 * number_of_line_profiles) fig = Figure(figsize=figsize) FigureCanvas(fig) gs = GridSpec(95 + 10 * number_of_line_profiles, 95) image_ax = fig.add_subplot(gs[0:45, :]) distance_ax = fig.add_subplot(gs[45:90, :]) colorbar_ax = fig.add_subplot(gs[90:95, :]) line_profile_ax_list = [] for i in range(number_of_line_profiles): gs_y_start = 95 + 10 * i line_profile_ax = fig.add_subplot( gs[gs_y_start:gs_y_start + 10, :]) line_profile_ax_list.append(line_profile_ax) image_y_lim = (0, image_data.shape[0] * data_scale) image_x_lim = (0, image_data.shape[1] * data_scale) image_clim = _get_clim_from_data(image_data, sigma=2) image_cax = image_ax.imshow( image_data, origin='lower', extent=[ image_x_lim[0], image_x_lim[1], image_y_lim[0], image_y_lim[1]]) image_cax.set_clim(image_clim[0], image_clim[1]) image_ax.set_xlim(image_x_lim[0], image_x_lim[1]) image_ax.set_ylim(image_y_lim[0], image_y_lim[1]) image_ax.set_title(plot_title) if atom_plane_list is not None: for atom_plane in atom_plane_list: if rotate_atom_plane_list_90_degrees: atom_plane_x = np.array(atom_plane.get_x_position_list()) atom_plane_y = np.array(atom_plane.get_y_position_list()) start_x = atom_plane_x[0] start_y = atom_plane_y[0] delta_y = (atom_plane_x[-1] - atom_plane_x[0]) delta_x = -(atom_plane_y[-1] - atom_plane_y[0]) atom_plane_x = np.array([start_x, start_x + delta_x]) atom_plane_y = np.array([start_y, start_y + delta_y]) else: atom_plane_x = np.array(atom_plane.get_x_position_list()) atom_plane_y = np.array(atom_plane.get_y_position_list()) image_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='red', lw=2) atom_plane_x = np.array(interface_plane.get_x_position_list()) atom_plane_y = np.array(interface_plane.get_y_position_list()) image_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='blue', lw=2) _make_subplot_map_from_regular_grid( distance_ax, heatmap_data_list, distance_data_scale=data_scale_z, clim=clim, atom_list=atom_list, atom_plane_marker=interface_plane, extra_marker_list=extra_marker_list, vector_to_plot=vector_to_plot) distance_cax = distance_ax.images[0] distance_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='red', lw=2) for line_profile_ax, line_profile_data in zip( line_profile_ax_list, line_profile_data_list): _make_subplot_line_profile( line_profile_ax, line_profile_data[:, 0], line_profile_data[:, 1], prune_outer_values=prune_outer_values, scale_x=data_scale, scale_z=data_scale_z) fig.tight_layout() fig.colorbar( distance_cax, cax=colorbar_ax, orientation='horizontal') fig.savefig(figname)
def plot_complex_image_map_line_profile_using_interface_plane( image_data, amplitude_data, phase_data, line_profile_amplitude_data, line_profile_phase_data, interface_plane, atom_plane_list=None, data_scale=1, atom_list=None, extra_marker_list=None, amplitude_image_lim=None, phase_image_lim=None, plot_title='', add_color_wheel=False, color_bar_markers=None, vector_to_plot=None, rotate_atom_plane_list_90_degrees=False, prune_outer_values=False, figname="map_data.jpg"): """ Parameters ---------- atom_list : list of Atom_Position instances extra_marker_list : two arrays of x and y [[x_values], [y_values]] """ number_of_line_profiles = 2 figsize = (10, 18 + 2 * number_of_line_profiles) fig = Figure(figsize=figsize) FigureCanvas(fig) gs = GridSpec(100 + 10 * number_of_line_profiles, 95) image_ax = fig.add_subplot(gs[0:45, :]) distance_ax = fig.add_subplot(gs[45:90, :]) colorbar_ax = fig.add_subplot(gs[90:100, :]) line_profile_ax_list = [] for i in range(number_of_line_profiles): gs_y_start = 100 + 10 * i line_profile_ax = fig.add_subplot( gs[gs_y_start:gs_y_start + 10, :]) line_profile_ax_list.append(line_profile_ax) image_y_lim = (0, image_data.shape[0] * data_scale) image_x_lim = (0, image_data.shape[1] * data_scale) image_clim = _get_clim_from_data(image_data, sigma=2) image_cax = image_ax.imshow( image_data, origin='lower', extent=[ image_x_lim[0], image_x_lim[1], image_y_lim[0], image_y_lim[1]]) image_cax.set_clim(image_clim[0], image_clim[1]) image_ax.set_xlim(image_x_lim[0], image_x_lim[1]) image_ax.set_ylim(image_y_lim[0], image_y_lim[1]) image_ax.set_title(plot_title) if atom_plane_list is not None: for atom_plane in atom_plane_list: if rotate_atom_plane_list_90_degrees: atom_plane_x = np.array(atom_plane.get_x_position_list()) atom_plane_y = np.array(atom_plane.get_y_position_list()) start_x = atom_plane_x[0] start_y = atom_plane_y[0] delta_y = (atom_plane_x[-1] - atom_plane_x[0]) delta_x = -(atom_plane_y[-1] - atom_plane_y[0]) atom_plane_x = np.array([start_x, start_x + delta_x]) atom_plane_y = np.array([start_y, start_y + delta_y]) else: atom_plane_x = np.array(atom_plane.get_x_position_list()) atom_plane_y = np.array(atom_plane.get_y_position_list()) image_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='red', lw=2) atom_plane_x = np.array(interface_plane.get_x_position_list()) atom_plane_y = np.array(interface_plane.get_y_position_list()) image_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='blue', lw=2) _make_subplot_map_from_complex_regular_grid( distance_ax, amplitude_data, phase_data, atom_list=atom_list, amplitude_image_lim=amplitude_image_lim, phase_image_lim=phase_image_lim, atom_plane_marker=interface_plane, extra_marker_list=extra_marker_list, vector_to_plot=vector_to_plot) distance_ax.plot( atom_plane_x * data_scale, atom_plane_y * data_scale, color='red', lw=2) line_profile_data_list = [ line_profile_amplitude_data, line_profile_phase_data] for line_profile_ax, line_profile_data in zip( line_profile_ax_list, line_profile_data_list): _make_subplot_line_profile( line_profile_ax, line_profile_data[:, 0], line_profile_data[:, 1], prune_outer_values=prune_outer_values, scale_x=data_scale) amplitude_delta = 0.01 * (amplitude_image_lim[1] - amplitude_image_lim[0]) phase_delta = 0.01 * (phase_image_lim[1] - phase_image_lim[0]) colorbar_mgrid = np.mgrid[ amplitude_image_lim[0]:amplitude_image_lim[1]:amplitude_delta, phase_image_lim[0]:phase_image_lim[1]:phase_delta ] colorbar_rgb = get_rgb_array(colorbar_mgrid[1], colorbar_mgrid[0]) colorbar_ax.imshow( colorbar_rgb, origin='lower', extent=[ phase_image_lim[0], phase_image_lim[1], amplitude_image_lim[0], amplitude_image_lim[1]]) colorbar_ax.set_xlabel("Phase", size=6) if color_bar_markers is not None: for color_bar_marker in color_bar_markers: colorbar_ax.axvline( color_bar_marker[0], color='white') colorbar_ax.text( color_bar_marker[0], amplitude_image_lim[0] * 0.97, color_bar_marker[1], transform=colorbar_ax.transData, va='top', ha='center', fontsize=8) if add_color_wheel: ax_magnetic_color_wheel_gs = GridSpecFromSubplotSpec( 40, 40, subplot_spec=gs[45:90, :])[30:39, 3:12] ax_magnetic_color_wheel = fig.add_subplot(ax_magnetic_color_wheel_gs) ax_magnetic_color_wheel.set_axis_off() make_color_wheel(ax_magnetic_color_wheel) fig.tight_layout() fig.savefig(figname) plt.close(fig)
# TOP RIGHT quad.set_array(pressure.isel(time=i).stack(z=('y', 'x'))) # BOTTOM x, y, mag = cop.isel(time=i).to_array() scrub_line.set_data([dt, dt], [0, 1]) cop_dot[0].set_data(x, y) cop_dot[0].set_markersize(10 * mag / cop.magnitude.max(dim='time')) # VERTICAL scrub_line_v.set_data([0, 1], [dt, dt]) """ SET UP GRID LAYOUT """ fig = plt.figure(figsize=(15, 7)) gridspec = GridSpec(2, 3, width_ratios=[3, 3, 1], height_ratios=[2, 1]) ax1 = fig.add_subplot(gridspec.new_subplotspec((0, 0), rowspan=1, colspan=1)) ax2 = fig.add_subplot(gridspec.new_subplotspec((0, 1), rowspan=1, colspan=1)) ax3 = fig.add_subplot(gridspec.new_subplotspec((1, 0), rowspan=1, colspan=2)) ax4 = fig.add_subplot(gridspec.new_subplotspec((0, 2), rowspan=2, colspan=1)) """ INIT PLOTS """ # TOP LEFT ax1.set_axis_off() try: img = ax1.imshow(kr.imread(samples[0])) except FileNotFoundError: img = None # TOP RIGHT ax2.set_axis_off()
def main(): start_year = 1979 end_year = 1981 HL_LABEL = "CRCM5_HL" NEMO_LABEL = "CRCM5_NEMO" dx = 0.1 dy = 0.1 file_prefix = "pm" PR_level = -1 PR_level_type = level_kinds.ARBITRARY tprecip_vname = "PR" sprecip_vname = "SN" TT_level = 1 TT_level_type = level_kinds.HYBRID sim_label_to_path = OrderedDict([ (HL_LABEL, "/RESCUE/skynet3_rech1/huziy/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl_oneway/Samples" ), (NEMO_LABEL, "/HOME/huziy/skynet3_rech1/CNRCWP/C5/2016/2-year-runs/coupled-GL+stfl/Samples" ) ]) # get a coord file ... (use pm* files, since they contain NEM1 variable) # Should be NEMO_LABEL, since the hostetler case does not calculate NEM? vars coord_file = "" found_coord_file = False for mdir in os.listdir(sim_label_to_path[NEMO_LABEL]): mdir_path = os.path.join(sim_label_to_path[NEMO_LABEL], mdir) if not os.path.isdir(mdir_path): continue for fn in os.listdir(mdir_path): if fn[:2] not in [ "pm", ]: continue if fn[-9:-1] == "0" * 8: continue coord_file = os.path.join(mdir_path, fn) found_coord_file = True if found_coord_file: break bmp, lons, lats = nemo_hl_util.get_basemap_obj_and_coords_from_rpn_file( path=coord_file) xx, yy = bmp(lons, lats) r = RPN(coord_file) lats_rot = r.get_first_record_for_name("^^") lons_rot = r.get_first_record_for_name(">>") lake_mask = np.greater( commons.get_nemo_lake_mask_from_rpn(coord_file, vname="NEM1"), 0) # Get the 100km region around the lakes lake_effect_regions = get_mask_of_points_near_lakes(lake_mask, npoints_radius=10) local_amplification_limit = 4 * 1e-2 / (24.0 * 3600.0) # the radius is 500 km, i.e. 50 gridpoints ij_to_non_local_mask = get_map_ij_to_nonlocal_mask(lake_effect_regions, lake_mask, npoints_radius=50) # Snowfall amount criteria (>= 10 cm) lower_snow_fall_limit = 10 * 1e-2 / (24.0 * 3600.0) # convert to M/s # wind blows from lake: time limit wind_blows_from_lake_time_limit_hours = 6.0 months_of_interest = [10, 11, 12, 1, 2, 3, 4, 5] sim_label_to_duration_mean = {} sim_label_to_lake_effect_sprecip_mean = {} sim_label_to_year_to_lake_effect_snow_fall_duration = OrderedDict([ (sim_label, OrderedDict()) for sim_label in sim_label_to_path ]) for sim_label, samples_dir_path in sim_label_to_path.items(): # calculate the composites for the (Oct - March) period lake_effect_snowfall_mean_duration = None # the duration is in time steps lake_effect_mean_snowrate_m_per_s = None snowfall_current_event = None duration_current_event = None # the duration is in time steps n_events = None sn_previous = None time_wind_blows_from_lake = None samples_dir = Path(samples_dir_path) snowfall_file = samples_dir.parent / "{}_snow_fall_{}-{}.nc".format( sim_label, start_year, end_year) wind_components_file = samples_dir.parent / "rotated_wind_{}.nc".format( sim_label) ds_wind = Dataset(str(wind_components_file)) print("Working on {} ...".format(sim_label)) lkice_manager = RPNLakeIceManager(samples_dir=samples_dir) with Dataset(str(snowfall_file)) as ds: time_var = ds.variables["time"] nt = time_var.shape[0] snowfall_var_m_per_s = ds.variables["SN"] u_var = ds_wind.variables["UU"] v_var = ds_wind.variables["VV"] time_var_wind = ds_wind.variables["time"] assert time_var_wind.shape == time_var.shape assert time_var_wind[0] == time_var[0] assert time_var_wind[-1] == time_var_wind[-1] assert (u_var.shape == snowfall_var_m_per_s.shape) and ( v_var.shape == snowfall_var_m_per_s.shape) times = num2date(time_var[:], time_var.units) dt_seconds = (times[1] - times[0]).total_seconds() year_to_lake_effect_snow_fall_duration = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label] for ti, t in enumerate(times): if t.month not in months_of_interest: continue if t.year > end_year or t.year < start_year: continue sn_current = snowfall_var_m_per_s[ti, :, :] if t.year not in year_to_lake_effect_snow_fall_duration: year_to_lake_effect_snow_fall_duration[ t.year] = np.zeros_like(sn_current) # initialize aggragtion fields if lake_effect_snowfall_mean_duration is None: lake_effect_snowfall_mean_duration = np.zeros_like( sn_current) lake_effect_mean_snowrate_m_per_s = np.zeros_like( sn_current) n_events = np.zeros_like(sn_current) snowfall_current_event = np.zeros_like(sn_current) duration_current_event = np.zeros_like(sn_current) sn_previous = np.zeros_like(sn_current) time_wind_blows_from_lake = np.zeros_like(sn_current) where_lake_effect_snow = ( sn_current > lower_snow_fall_limit) & lake_effect_regions & (~lake_mask) # add a condition on the local amplification i_arr, j_arr = np.where(where_lake_effect_snow) for i, j in zip(i_arr, j_arr): the_mask = ij_to_non_local_mask[(i, j)] where_lake_effect_snow[i, j] = sn_current[the_mask].mean( ) < sn_current[i, j] - local_amplification_limit # add a condition on the wind fetch from lakes and ice fraction. wind_blows_from_lake = get_wind_blows_from_lake_mask( lake_mask, lake_effect_regions, u_var[ti, :, :], v_var[ti, :, :], dx=dx, dy=dy, lake_ice_frac=lkice_manager.get_lake_fraction_for_date( the_date=t), lats_rot=lats_rot) time_wind_blows_from_lake[ wind_blows_from_lake] += dt_seconds / 3600.0 where_lake_effect_snow = where_lake_effect_snow & ( time_wind_blows_from_lake >= wind_blows_from_lake_time_limit_hours) time_wind_blows_from_lake[~wind_blows_from_lake] = 0 # update accumulators for current lake effect snowfall events snowfall_current_event[where_lake_effect_snow] += sn_current[ where_lake_effect_snow] duration_current_event[where_lake_effect_snow] += 1.0 where_lake_effect_snow_finished = (~where_lake_effect_snow) & ( sn_previous > lower_snow_fall_limit) # recalculate mean lake effect snowfall duration and rate lake_effect_snowfall_mean_duration[ where_lake_effect_snow_finished] = ( lake_effect_snowfall_mean_duration[ where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + duration_current_event[where_lake_effect_snow_finished] ) / (n_events[where_lake_effect_snow_finished] + 1) lake_effect_mean_snowrate_m_per_s[ where_lake_effect_snow_finished] = ( lake_effect_mean_snowrate_m_per_s[ where_lake_effect_snow_finished] * n_events[where_lake_effect_snow_finished] + snowfall_current_event[where_lake_effect_snow_finished] ) / (n_events[where_lake_effect_snow_finished] + 1) year_to_lake_effect_snow_fall_duration[t.year][ where_lake_effect_snow_finished] += duration_current_event[ where_lake_effect_snow_finished] * dt_seconds # reset the current accumulators snowfall_current_event[where_lake_effect_snow_finished] = 0 duration_current_event[where_lake_effect_snow_finished] = 0 n_events[where_lake_effect_snow_finished] += 1 sn_previous = sn_current if ti % 1000 == 0: print("Done {} of {}".format(ti + 1, nt)) # normalization lake_effect_snowfall_mean_duration *= dt_seconds / ( 24 * 60 * 60.0) # convert to days lake_effect_mean_snowrate_m_per_s = np.ma.masked_where( ~lake_effect_regions, lake_effect_mean_snowrate_m_per_s) lake_effect_snowfall_mean_duration = np.ma.masked_where( ~lake_effect_regions, lake_effect_snowfall_mean_duration) for y, yearly_durations in sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label].items(): sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label][ y] = np.ma.masked_where(~lake_effect_regions, yearly_durations) / (24 * 3600.0) sim_label_to_duration_mean[ sim_label] = lake_effect_snowfall_mean_duration sim_label_to_lake_effect_sprecip_mean[ sim_label] = lake_effect_mean_snowrate_m_per_s * 100 * 24 * 3600.0 # close the file with rotated wind components ds_wind.close() plot_utils.apply_plot_params(font_size=6, width_cm=18, height_cm=10) fig = plt.figure() gs = GridSpec(3, 3) duration_clevs = 20 # np.arange(0, 1.1, 0.1) snowrate_clevs = 20 # np.arange(0, 36, 4) duration_clevs_diff = 20 # np.arange(-1, 1.1, 0.1) snowrate_clevs_diff = 20 # np.arange(-10, 12, 2) vmax_duration = None vmax_snowrate = None vmax_days_per_year = None for row, sim_label in enumerate(sim_label_to_path): if vmax_duration is None: vmax_duration = sim_label_to_duration_mean[sim_label].max() vmax_snowrate = sim_label_to_lake_effect_sprecip_mean[ sim_label].max() vmax_days_per_year = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label][1980].max() else: vmax_duration = max(vmax_duration, sim_label_to_duration_mean[sim_label].max()) vmax_snowrate = max( vmax_snowrate, sim_label_to_lake_effect_sprecip_mean[sim_label].max()) vmax_days_per_year = max( vmax_days_per_year, sim_label_to_year_to_lake_effect_snow_fall_duration[sim_label] [1980].max()) for col, sim_label in enumerate(sim_label_to_path): # plot the duration of lake-effect snow events ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[sim_label], ax=ax, vmin=0, vmax=vmax_duration, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{}".format(sim_label)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[sim_label], ax=ax, vmax=vmax_snowrate, vmin=lower_snow_fall_limit, cmap="rainbow_r") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{}".format(sim_label)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = sim_label_to_year_to_lake_effect_snow_fall_duration[ sim_label][1980] clevs = [ 0, 0.1, ] + list(np.arange(0.4, 3.2, 0.4)) bn = BoundaryNorm(clevs, len(clevs)) cmap = cm.get_cmap("spectral_r", len(clevs)) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, norm=bn, cmap=cmap) bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax, extend="max") ax.set_title("# Days per year") ax.set_xlabel("{}".format(sim_label)) # plot the difference # plot the duration of lake-effect snow events col = 2 cmap = cm.get_cmap("seismic", 40) vmin = -np.max(sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL]) ax = fig.add_subplot(gs[0, col]) cs = bmp.pcolormesh(xx, yy, sim_label_to_duration_mean[NEMO_LABEL] - sim_label_to_duration_mean[HL_LABEL], vmin=vmin, ax=ax, cmap=cmap) plt.colorbar(cs, ax=ax) bmp.drawcoastlines(linewidth=0.3, ax=ax) ax.set_title("Duration (days)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean intensity of the lake-effect snow events ax = fig.add_subplot(gs[1, col]) vmin = -np.max(sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL]) cs = bmp.pcolormesh(xx, yy, sim_label_to_lake_effect_sprecip_mean[NEMO_LABEL] - sim_label_to_lake_effect_sprecip_mean[HL_LABEL], ax=ax, vmin=vmin, cmap=cmap) # convert to cm/day bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("Snowfall rate, (cm/day)") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) # plot the mean duration of the lake effect snowfall events per year ax = fig.add_subplot(gs[2, col]) to_plot = ( sim_label_to_year_to_lake_effect_snow_fall_duration[NEMO_LABEL][1980] - sim_label_to_year_to_lake_effect_snow_fall_duration[HL_LABEL][1980]) cs = bmp.pcolormesh(xx, yy, to_plot, ax=ax, vmin=-to_plot.max(), cmap="seismic") bmp.drawcoastlines(linewidth=0.3, ax=ax) plt.colorbar(cs, ax=ax) ax.set_title("# Days per year") ax.set_xlabel("{} - {}".format(NEMO_LABEL, HL_LABEL)) fig.tight_layout() fig.savefig(os.path.join( img_folder, "lake_effect_snow_10cm_limit_and_loc_ampl_{}-{}.png".format( start_year, end_year)), dpi=commons.dpi, transparent=True, bbox_inches="tight")
ts = one.load(eid[0], 'ephysData.raw.timestamps') # %% Calculate power spectrum and coherence between two random channels ps_freqs, ps = bb.lfp.power_spectrum(signal, fs=raw.fs, segment_length=1, segment_overlap=0.5) random_ch = np.random.choice(raw.nc, 2) coh_freqs, coh, phase_lag = bb.lfp.coherence(signal[random_ch[0], :], signal[random_ch[1], :], fs=raw.fs) # %% Create power spectrum and coherence plot fig = plt.figure(figsize=(18, 12)) gs = GridSpec(3, 2, figure=fig) cmap = sns.color_palette('cubehelix', 50) ax1 = fig.add_subplot(gs[:, 0]) sns.heatmap(data=np.log10(ps[:, ps_freqs < 140]), cbar=True, ax=ax1, yticklabels=50, cmap=cmap, cbar_kws={'label': 'log10 power ($V^2$)'}) ax1.set(xticks=np.arange(0, np.sum(ps_freqs < 140), 50), xticklabels=np.array(ps_freqs[np.arange(0, np.sum(ps_freqs < 140), 50)], dtype=int), ylabel='Channels', xlabel='Frequency (Hz)')
pre_batch_x = torch.cat( [batch_x, High_origin[:, 0].unsqueeze(1).cuda()], dim=1) output = pre_model(batch_y) noise_level = activation['noise_level'] # batch_y = torch.cat([batch_y, noise_level[:,0].unsqueeze(1).cuda()], dim=1) # output = model(batch_y[:,:1], noise_level[:,:1]) output = model(batch_y[:, :1], noise_level[:, :1]) loss = criterion(output, batch_x) fig = plt.figure() gs = GridSpec(nrows=1, ncols=3) plot1 = fig.add_subplot(gs[0, 0]) plot2 = fig.add_subplot(gs[0, 1]) plot3 = fig.add_subplot(gs[0, 2]) # plot4 = fig.add_subplot(gs[1, 1]) # plot5 = fig.add_subplot(gs[1, 2]) plot1.axis('off') plot2.axis('off') plot3.axis('off') # plot4.axis('off') # plot5.axis('off') if n_count % 200 == 0: # plt.title("dd") plot1.imshow(batch_x[0].cpu().squeeze(0), cmap='rainbow') plot2.imshow(batch_y[0, :1].cpu().squeeze(0), cmap='rainbow')
##construct the Gf U = 1.0 l_block = GfImFreq_x_ImFreqTv3(mesh=mprod, shape=[1, 1, 1]) #instantiation of the function Lambda = BlockGf(name_list=["ch", "sp"], block_list=[l_block, l_block]) ##fill with expression g_om = GfImFreq(mesh=m1, shape=[1, 1]) w = lambda n: 2 * n * pi / beta * 1j for n in range(-n_max + 1, n_max): g_om << U**2 / 4 * inverse(iOmega_n + w(n)) * inverse(iOmega_n) Lambda["ch"].data[:, n + n_max - 1, 0, 0, 0] = g_om.data[:, 0, 0] ###plot gs = GridSpec(1, 2) plt.subplot(gs[0], aspect="equal") oplot(Lambda["ch"][0, 0, 0], type="contourf") plt.xlim(-2, 2) plt.ylim(-2, 2) plt.colorbar() plt.subplot(gs[1]) for n in [0, 5, 10]: oplot(Lambda["ch"][0, 0, 0].slice_at_const_w2(n), mode="R", x_window=(-2, 2), label=r"$\Lambda^\mathrm{ch}(i\omega, i\Omega_{%s})$" % n) plt.ylabel("") plt.legend(loc="best")
def change_geometry(self, numrows, numcols, num): """change subplot geometry, e.g., from 1,1,1 to 2,2,3""" self._subplotspec = GridSpec(numrows, numcols, figure=self.figure)[num - 1] self.update_params() self.set_position(self.figbox)
def plot_seasonal_mean_biases(season_to_error_field=None, varname="", basemap_info=None): assert isinstance(basemap_info, BasemapInfo) cmap = cm.get_cmap("RdBu_r", 20) # Set to False if you want the limits to be recalculated from data manual_limits = True minval = min([ np.percentile(field[~field.mask], 95) for s, field in season_to_error_field.items() ]) maxval = max([ np.percentile(field[~field.mask], 95) for s, field in season_to_error_field.items() ]) if manual_limits: if varname == "PR": minval, maxval = -3, 3 if varname == "TT": minval, maxval = -7, 7 d = max(abs(minval), abs(maxval)) clevs = MaxNLocator(nbins=cmap.N, symmetric=True).tick_values(-d, d) fig_path = img_folder.joinpath("{}.png".format(varname)) fig = plt.figure() nrows = 2 ncols = 2 gs = GridSpec(nrows, ncols=ncols + 1, width_ratios=[1, 1, 0.05]) xx, yy = basemap_info.get_proj_xy() cs = None for i, season in enumerate(season_to_error_field): row = i // ncols col = i % ncols ax = fig.add_subplot(gs[row, col]) cs = basemap_info.basemap.contourf(xx, yy, season_to_error_field[season], ax=ax, cmap=cmap, levels=clevs, extend="both") basemap_info.basemap.drawcoastlines() ax.set_title(season) # basemap_info.basemap.colorbar(cs) cax = fig.add_subplot(gs[:, -1]) cax.set_title(infovar.get_units(var_name=varname)) plt.colorbar(cs, cax=cax) with fig_path.open("wb") as figfile: fig.savefig(figfile, format="png", bbox_inches="tight") plt.close(fig)
def plot_matrix(data, cbar_title="", mode='data', chr_stops_dict=None, labels=None, cluster=False, textfontsize=24, tickfontsize=22, bps=None, figsize=(24, 8), dpi=100, vmax=None, vmid=2, bbox_to_anchor=(1.065, 0.0, 1, 1), labels_title='Subclones', output_path=None): if mode == 'data': cmap = datacmap elif mode == 'cnv': if vmax is None or vmax < 4: vmax = 4 vmid = min(vmid, vmax - 1) vmax = int(vmax) vmid = int(vmid) cmap = get_cnv_cmap(vmax=vmax, vmid=vmid) else: raise AttributeError( 'mode argument must be one of \'data\' or \'cnv\'') cmap.set_bad(color='black') # for NaN data_ = np.array(data, copy=True) fig = plt.figure(figsize=figsize, dpi=dpi) if labels is not None: labels = np.array(labels).ravel() labels_ = np.array(labels, copy=True) if mode == 'cnv': data_, labels_ = cluster_clones(data_, labels_, within_clone=False) else: data_, labels_ = cluster_clones(data_, labels_, within_clone=cluster) ticks = dict() unique_labels = np.unique(labels_) n_unique_labels = len(unique_labels) for label in unique_labels: # sorted # Get last pos t = np.where(labels_ == label)[0] if len(t) > 1: t = t[-1] ticks[label] = t + 1 gs = GridSpec(1, 2, wspace=0.05, width_ratios=[1, 40]) ax = fig.add_subplot(gs[0]) bounds = [0] + list(ticks.values()) subnorm = matplotlib.colors.BoundaryNorm(bounds, n_unique_labels) clone_colors = list(LABEL_COLORS_DICT.values())[:n_unique_labels] if '-' in unique_labels: clone_colors = ['black'] + clone_colors clonecmap = matplotlib.colors.ListedColormap(clone_colors) cb = matplotlib.colorbar.ColorbarBase( ax, cmap=clonecmap, norm=subnorm, boundaries=bounds, spacing="proportional", orientation="vertical", ) cb.outline.set_visible(False) cb.ax.set_ylabel(labels_title, fontsize=textfontsize) ax.yaxis.set_label_position("left") for j, lab in enumerate(ticks.keys()): cb.ax.text( 0.5, ((bounds[j + 1] + bounds[j]) / 2) / bounds[-1], lab, ha="center", va="center", rotation=90, color="w", fontsize=tickfontsize, ) cb.set_ticks([]) ax = fig.add_subplot(gs[1]) else: ax = plt.gca() if labels is None and cluster is True: Z = ward(pdist(data_)) hclust_index = leaves_list(Z) data_ = data_[hclust_index] im = plt.pcolormesh(data_, cmap=cmap, rasterized=True) ax = plt.gca() plt.ylabel('Cells', fontsize=textfontsize) plt.xlabel('Bins', fontsize=textfontsize) if bps is not None: ax.vlines(bps, *ax.get_ylim(), colors="k", linestyles="dashed", linewidth=2.) if chr_stops_dict is not None: chr_stops_chrs = list(chr_stops_dict.keys()) chr_stops_bins = list(chr_stops_dict.values()) chr_means = [] chr_means.append(chr_stops_bins[0] / 2) for c in range(1, len(chr_stops_bins)): aux = (chr_stops_bins[c] + chr_stops_bins[c - 1]) / 2 chr_means.append(aux) chrs_ = deepcopy(chr_stops_chrs) chrs_[12] = f'{chr_stops_chrs[12]} ' chrs_[12] chrs_[20] = "" chrs_[21] = f' {chr_stops_chrs[21]}' plt.xticks(chr_means, np.array(chrs_), rotation=0, fontsize=tickfontsize) ax.vlines(list(chr_stops_dict.values())[:-1], *ax.get_ylim(), ls='--', linewidth=2.5) plt.xlabel('Chromosomes', fontsize=textfontsize) for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(tickfontsize) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(tickfontsize) if labels is not None: plt.yticks([]) axins = inset_axes( ax, width="2%", # width = 5% of parent_bbox width height="85%", loc="lower left", bbox_to_anchor=bbox_to_anchor, bbox_transform=ax.transAxes, borderpad=0, ) cb = plt.colorbar(im, cax=axins) if vmax is not None: im.set_clim(vmin=0, vmax=vmax) if mode == 'cnv': im.set_clim(vmin=0, vmax=vmax) tick_locs = (np.arange(vmax + 1) + 0.5) * (vmax) / (vmax + 1) cb.set_ticks(tick_locs) # cb.set_ticks([0.4, 1.2, 2, 2.8, 3.6]) ticklabels = np.arange(0, vmax + 1).astype(int).astype(str) ticklabels[-1] = f"{ticklabels[-1]}+" cb.set_ticklabels(ticklabels) elif mode == 'data': if vmax == 2: cb.set_ticks([0, 1, 2]) cb.set_ticklabels(["0", "1", "2+"]) cb.ax.tick_params(labelsize=tickfontsize) cb.outline.set_visible(False) cb.ax.set_title(cbar_title, y=1.01, fontsize=textfontsize) if output_path is not None: print("Creating {}...".format(output_path)) plt.savefig(output_path, bbox_inches="tight", transparent=False) plt.close() print("Done.") else: plt.show()
def main(): plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=25, height_cm=25) season_to_months = DEFAULT_SEASON_TO_MONTHS r_config = RunConfig( data_path= "/RESCUE/skynet3_rech1/huziy/hdf_store/quebec_0.1_crcm5-hcd-rl.hdf5", start_year=1980, end_year=2009, label="CRCM5-L") # Number of points for aggregation nx_agg = 5 ny_agg = 5 bmp_info = analysis.get_basemap_info_from_hdf(file_path=r_config.data_path) bmp_info_agg = bmp_info.get_aggregated(nagg_x=nx_agg, nagg_y=ny_agg) # Validate temperature and precip model_vars = ["TT", "PR"] obs_vars = ["air", "precip"] obs_paths = [ "/RECH/data/Validation/Willmott_Matsuura/NetCDF/air.mon.mean.v301.nc", "/RECH/data/Validation/Willmott_Matsuura/NetCDF/precip.mon.total.v301.nc" ] plot_all_vars_in_one_fig = True fig = None gs = None row_axes = None ncols = None if plot_all_vars_in_one_fig: plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=25, height_cm=12) fig = plt.figure() ncols = len(season_to_months) + 1 gs = GridSpec(len(model_vars), ncols, width_ratios=(ncols - 1) * [ 1., ] + [ 0.05, ]) else: plot_utils.apply_plot_params(font_size=12, width_pt=None, width_cm=25, height_cm=25) row = 0 for mname, oname, opath in zip(model_vars, obs_vars, obs_paths): if plot_all_vars_in_one_fig: row_axes = [fig.add_subplot(gs[row, col]) for col in range(ncols)] compare_vars(vname_model=mname, vname_obs=oname, r_config=r_config, season_to_months=season_to_months, nx_agg=nx_agg, ny_agg=ny_agg, bmp_info_agg=bmp_info_agg, obs_path=opath, axes_list=row_axes) row += 1 # Save the figure if necessary if plot_all_vars_in_one_fig: fig_path = img_folder.joinpath("{}.png".format("_".join(model_vars))) with fig_path.open("wb") as figfile: fig.savefig(figfile, format="png", bbox_inches="tight") plt.close(fig)
def showMatrix(matrix, x_array=None, y_array=None, **kwargs): """Show a matrix using :meth:`~matplotlib.axes.Axes.imshow`. Curves on x- and y-axis can be added. :arg matrix: matrix to be displayed :type matrix: :class:`~numpy.ndarray` :arg x_array: data to be plotted above the matrix :type x_array: :class:`~numpy.ndarray` :arg y_array: data to be plotted on the left side of the matrix :type y_array: :class:`~numpy.ndarray` :arg percentile: a percentile threshold to remove outliers, i.e. only showing data within *p*-th to *100-p*-th percentile :type percentile: float """ from matplotlib import ticker from matplotlib.gridspec import GridSpec from matplotlib.collections import LineCollection from matplotlib.pyplot import gca, sca, sci, colorbar, subplot p = kwargs.pop('percentile', None) vmin = vmax = None if p is not None: vmin = np.percentile(matrix, p) vmax = np.percentile(matrix, 100 - p) vmin = kwargs.pop('vmin', vmin) vmax = kwargs.pop('vmax', vmax) lw = kwargs.pop('linewidth', 1) W = H = kwargs.pop('ratio', 6) ticklabels = kwargs.pop('ticklabels', None) xticklabels = kwargs.pop('xticklabels', ticklabels) yticklabels = kwargs.pop('yticklabels', ticklabels) allticks = kwargs.pop( 'allticks', False ) # this argument is temporary and will be replaced by better implementation origin = kwargs.pop('origin', 'lower') tree_mode = False if np.isscalar(y_array): try: from Bio import Phylo except ImportError: raise ImportError('Phylo module could not be imported. ' 'Reinstall ProDy or install Biopython ' 'to solve the problem.') tree_mode = isinstance(y_array, Phylo.BaseTree.Tree) if x_array is not None and y_array is not None: nrow = 2 ncol = 2 i = 1 j = 1 width_ratios = [1, W] height_ratios = [1, H] aspect = 'auto' elif x_array is not None and y_array is None: nrow = 2 ncol = 1 i = 1 j = 0 width_ratios = [W] height_ratios = [1, H] aspect = 'auto' elif x_array is None and y_array is not None: nrow = 1 ncol = 2 i = 0 j = 1 width_ratios = [1, W] height_ratios = [H] aspect = 'auto' else: nrow = 1 ncol = 1 i = 0 j = 0 width_ratios = [W] height_ratios = [H] aspect = None if tree_mode: nrow = 2 ncol = 2 i = 1 j = 1 width_ratios = [W, W] height_ratios = [H, H] aspect = 'auto' main_index = (i, j) upper_index = (i - 1, j) left_index = (i, j - 1) complex_layout = nrow > 1 or ncol > 1 show_colorbar = kwargs.pop('colorbar', True) ax1 = ax2 = ax3 = None if complex_layout: gs = GridSpec(nrow, ncol, width_ratios=width_ratios, height_ratios=height_ratios, hspace=0., wspace=0.) lines = [] if nrow > 1: ax1 = subplot(gs[upper_index]) if not tree_mode: ax1.set_xticklabels([]) y = x_array xp, yp = interpY(y) points = np.array([xp, yp]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) lcy = LineCollection(segments, array=yp, linewidths=lw, cmap='jet') lines.append(lcy) ax1.add_collection(lcy) ax1.set_xlim(xp.min(), xp.max()) ax1.set_ylim(yp.min(), yp.max()) ax1.axis('off') if ncol > 1: ax2 = subplot(gs[left_index]) if tree_mode: Phylo.draw(y_array, do_show=False, axes=ax2, **kwargs) else: ax2.set_xticklabels([]) y = y_array xp, yp = interpY(y) points = np.array([yp, xp]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1) lcx = LineCollection(segments, array=yp, linewidths=lw, cmap='jet') lines.append(lcx) ax2.add_collection(lcx) ax2.set_xlim(yp.min(), yp.max()) ax2.set_ylim(xp.min(), xp.max()) ax2.invert_xaxis() ax2.axis('off') if complex_layout: ax3 = subplot(gs[main_index]) else: ax3 = gca() kwargs['origin'] = origin if not 'cmap' in kwargs: kwargs['cmap'] = 'jet' im = ax3.imshow(matrix, aspect=aspect, vmin=vmin, vmax=vmax, **kwargs) #ax3.set_xlim([-0.5, matrix.shape[0]+0.5]) #ax3.set_ylim([-0.5, matrix.shape[1]+0.5]) if xticklabels is not None: ax3.xaxis.set_major_formatter(ticker.IndexFormatter(xticklabels)) if yticklabels is not None and ncol == 1: ax3.yaxis.set_major_formatter(ticker.IndexFormatter(yticklabels)) if allticks: ax3.xaxis.set_major_locator(ticker.IndexLocator(offset=0.5, base=1.)) ax3.yaxis.set_major_locator(ticker.IndexLocator(offset=0.5, base=1.)) else: ax3.xaxis.set_major_locator(ticker.AutoLocator()) ax3.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax3.yaxis.set_major_locator(ticker.AutoLocator()) ax3.yaxis.set_minor_locator(ticker.AutoMinorLocator()) if ncol > 1: ax3.yaxis.set_major_formatter(ticker.NullFormatter()) cb = None if show_colorbar: if nrow > 1: axes = [ax1, ax2, ax3] while None in axes: axes.remove(None) s = H / (H + 1.) cb = colorbar(mappable=im, ax=axes, anchor=(0, 0), shrink=s) else: cb = colorbar(mappable=im) sca(ax3) sci(im) return im, lines, cb
"EXPLAIN ANALYSE SELECT * FROM allkeys_testing_hash WHERE " +\ "\"INSTRUME\" = '" + str(instrume[0]) + "';" # Connect to the database conn = psycopg2.connect(database="ltarchive", user="******", password="******", host="150.204.240.113", port="6543") # Run the queries and save the results in arrays n_results_hash_instrume[j][i], time_hash_instrume[j][i] =\ pq.run_query(conn, instrume_query) conn.close() fig1 = plt.figure(1, figsize=(10, 6)) fig1.clf() gridspec = GridSpec(1, 3) gridspec.update(left=0.12, right=0.95, top=0.98, bottom=0.1, wspace=0) ax1 = fig1.add_subplot(gridspec[0, 0]) ax2 = fig1.add_subplot(gridspec[0, 1]) ax3 = fig1.add_subplot(gridspec[0, 2]) ax1.scatter(np.log10(np.median(n_results_hash_obsid, axis=1)), np.log10(np.median(time_hash_obsid, axis=1)), s=2, label='OBSID') ax1.scatter(np.log10(np.median(n_results_hash_groupid, axis=1)), np.log10(np.median(time_hash_groupid, axis=1)), s=2, label='GROUPID') ax1.scatter(np.log10(np.median(n_results_hash_tagid, axis=1)),
def plot_histograms(X, Y, rating_label='rater_1', out_file=None): import seaborn as sn sn.set(style="whitegrid") mdata, pp_cols = read_dataset(X, Y, rate_label=rating_label) mdata['rater'] = mdata[[rating_label]].values.ravel() for col in mdata.columns.ravel().tolist(): if col.startswith('rater_'): del mdata[col] mdata = mdata.loc[mdata.rater.notnull()] zscored = mdata.copy() # TODO: zscore_dataset was removed # zscored = zscore_dataset( # mdata, excl_columns=['rater', 'size_x', 'size_y', 'size_z', # 'spacing_x', 'spacing_y', 'spacing_z']) colnames = [col for col in sorted(pp_cols) if not (col.startswith('spacing') or col.startswith('summary') or col.startswith('size'))] nrows = len(colnames) # palette = ['dodgerblue', 'darkorange'] fig = plt.figure(figsize=(18, 2 * nrows)) gs = GridSpec(nrows, 2, hspace=0.2) for i, col in enumerate(sorted(colnames)): ax_nzs = plt.subplot(gs[i, 0]) ax_zsd = plt.subplot(gs[i, 1]) sn.distplot(mdata.loc[(mdata.rater == 0), col], norm_hist=False, label='Accept', ax=ax_nzs, color='dodgerblue') sn.distplot(mdata.loc[(mdata.rater == 1), col], norm_hist=False, label='Reject', ax=ax_nzs, color='darkorange') ax_nzs.legend() sn.distplot(zscored.loc[(zscored.rater == 0), col], norm_hist=False, label='Accept', ax=ax_zsd, color='dodgerblue') sn.distplot(zscored.loc[(zscored.rater == 1), col], norm_hist=False, label='Reject', ax=ax_zsd, color='darkorange') alldata = mdata[[col]].values.ravel().tolist() minv = np.percentile(alldata, 0.2) maxv = np.percentile(alldata, 99.8) ax_nzs.set_xlim([minv, maxv]) alldata = zscored[[col]].values.ravel().tolist() minv = np.percentile(alldata, 0.2) maxv = np.percentile(alldata, 99.8) ax_zsd.set_xlim([minv, maxv]) if out_file is None: out_file = 'histograms.svg' fname, ext = op.splitext(out_file) if ext[1:] not in ['pdf', 'svg', 'png']: ext = '.svg' out_file = fname + '.svg' fig.savefig(out_file, format=ext[1:], bbox_inches='tight', pad_inches=0, dpi=100) return fig
def main(): data_dir = 'data' specta_filename = 'spectra_set_combined.csv' power_filename = '201028_power_through_fiber.csv' power_norm_filename = '201028_power_at laser.csv' spectra_df = pd.read_csv(os.path.join(data_dir, specta_filename), index_col=0) power_df = pd.read_csv(os.path.join(data_dir, power_filename), index_col=0, header=None, names=['wavelength', 'power']) * 1e3 power_norm_df = pd.read_csv(os.path.join(data_dir, power_norm_filename), index_col=0, header=None, names=['wavelength', 'power']) * 1e3 correction_df = (power_norm_df / power_df).dropna() correction_x = correction_df.index.values correction_y = correction_df.values.flatten() power_x = power_df.index.values correction_y_full = np.interp(power_x, correction_x, correction_y) power = power_df.values.flatten() * correction_y_full power_dict = dict(zip(power_x, power)) spectra_x = spectra_df.index.values spectra_y_names = spectra_df.columns.values.astype(int) spectra_y = spectra_df.values batches = [] for i in range(spectra_y_names.size): batches.append( dict(x=spectra_x, y=spectra_y[:, i], wavelength=spectra_y_names[i], power=power_dict[spectra_y_names[i]])) fig = plt.figure(num=1, figsize=(7, 4)) results = [gen_info_plot(**b) for b in batches] peak_deltas, _peak_widths = tuple(np.array(x) for x in zip(*results)) gs = GridSpec(3, 1, figure=fig, bottom=0.17, top=0.965, right=0.95, left=0.1, hspace=0.26) ax1 = fig.add_subplot(gs[0]) ax1.plot(spectra_y_names, peak_deltas, color='black', lw=1.5, marker='.', clip_on=False) ax1.fill_between(spectra_y_names, peak_deltas, 0, where=peak_deltas >= 0, fc='C0', interpolate=True, alpha=0.7) ax1.fill_between(spectra_y_names, peak_deltas, 0, where=peak_deltas <= 0, fc='C3', interpolate=True, alpha=0.7) ax1.set_yticks([-10, 0]) ax1.set_ylim([-12, 2]) ax1.set_ylabel(r'$\Delta\lambda_{peak}$, nm') ax2 = fig.add_subplot(gs[1]) ax2.plot(spectra_y_names, _peak_widths, color='black', lw=1.5, marker='.', clip_on=False) ax2.fill_between(spectra_y_names, _peak_widths, 0, where=_peak_widths >= 0, fc='black', interpolate=True, alpha=0.2) ax2.set_yticks([0, 6, 12]) ax2.set_ylim([0, 12]) ax2.set_ylabel('FWHM, nm') ax3 = fig.add_subplot(gs[2]) ax3.plot(power_x, power / 1000, color='black', lw=1.5, marker='.') ax3.plot(power_x[-1], power[-1] / 1000, color='black', lw=1.5, marker='.', clip_on=False) ax3.fill_between(power_x, power / 1000, 0, where=power >= 0, fc='black', interpolate=True, alpha=0.2) ax3.set_yticks([0, 0.8, 1.6]) ax3.set_ylim([8e-2, 2]) ax3.set_yscale('log') ax3.set_xlabel('Expected $\lambda_{peak}$, nm') ax3.set_ylabel('Max Power, W') ax_lw = 1 axs = [ax1, ax2, ax3] for i, ax in enumerate(axs): sns.despine(ax=ax, top=True, right=True, left=False, bottom=False, offset=ax_lw * 0) ax.spines['bottom'].set_linewidth(ax_lw) ax.set_xticks(np.arange(690, 1045, 50)) ax.set_xticks(np.arange(690, 1045, 10), minor=True) ax.minorticks_on() ax.grid(True, 'major', 'both', lw=ax_lw, zorder=0) ax.grid(True, 'minor', 'both', lw=ax_lw / 2, ls=':', zorder=0) ax.tick_params(axis='x', bottom=True, direction='out', width=ax_lw, length=5, labelbottom=i == 2) ax.set_xlim([690, 1040.1]) fig.align_ylabels(axs) csutils.save_figures( '201028_mai_tia_laser_measurements_cswain', add_filename_timestamp=False, stamp_kwargs=dict(stamp_str='MaiTai Measurements from 20.10.28 | Fig. ' '#%n | Generated on %d by Corban S.', fontfamily='Input', fontstyle='italic')) plt.show()
def raters_variability_plot(mdata, figsize=(22, 22), width=101, out_file=None, raters=['rater_1', 'rater_2', 'rater_3'], only_overlap=True, rater_names=['Rater 1', 'Rater 2a', 'Rater 2b']): if only_overlap: mdata = mdata[np.all(~np.isnan(mdata[raters]), axis=1)] # Swap raters 2 and 3 # i, j = cols.index('rater_2'), cols.index('rater_3') # cols[j], cols[i] = cols[i], cols[j] # mdata.columns = cols sites_list = sorted(set(mdata.site.values.ravel().tolist())) sites_len = [] for site in sites_list: sites_len.append(len(mdata.loc[mdata.site == site])) sites_len, sites_list = zip(*sorted(zip(sites_len, sites_list))) blocks = [(slen - 1) // width + 1 for slen in sites_len] fig = plt.figure(figsize=figsize) gs = GridSpec(len(sites_list), 1, width_ratios=[1], height_ratios=blocks, hspace=0.05) for s, gsel in zip(sites_list, gs): ax = plt.subplot(gsel) plot_raters(mdata.loc[mdata.site == s, raters], ax=ax, width=width, size=.40 if len(raters) == 3 else .80) ax.set_yticklabels([s]) # ax.add_line(Line2D([0.0, width], [8.0, 8.0], color='k')) # ax.annotate( # '%d images' % width, xy=(0.5 * width, 8), xycoords='data', # xytext=(0.5 * width, 9), fontsize=20, ha='center', va='top', # arrowprops=dict(arrowstyle='-[,widthB=1.0,lengthB=0.2', lw=1.0) # ) # ax.annotate('QC Prevalences', xy=(0.1, -0.15), xytext=(0.5, -0.1), xycoords='axes fraction', # fontsize=20, ha='center', va='top', # arrowprops=dict(arrowstyle='-[, widthB=3.0, lengthB=0.2', lw=1.0)) newax = plt.axes([0.6, 0.65, .25, .16]) newax.grid(False) newax.set_xticklabels([]) newax.set_xticks([]) newax.set_yticklabels([]) newax.set_yticks([]) nsamples = len(mdata) for i, rater in enumerate(raters): nsamples = len(mdata) - sum(np.isnan(mdata[rater].values)) good = 100 * sum(mdata[rater] == 1.0) / nsamples bad = 100 * sum(mdata[rater] == -1.0) / nsamples text_x = .92 text_y = .5 - 0.17 * i newax.text(text_x - .36, text_y, '%2.1f%%' % good, color='limegreen', weight=1000, size=25, horizontalalignment='right', verticalalignment='center', transform=newax.transAxes) newax.text(text_x - .18, text_y, '%2.1f%%' % max((0.0, 100 - good - bad)), color='dimgray', weight=1000, size=25, horizontalalignment='right', verticalalignment='center', transform=newax.transAxes) newax.text(text_x, text_y, '%2.1f%%' % bad, color='tomato', weight=1000, size=25, horizontalalignment='right', verticalalignment='center', transform=newax.transAxes) newax.text(1 - text_x, text_y, rater_names[i], color='k', size=25, horizontalalignment='left', verticalalignment='center', transform=newax.transAxes) newax.text(0.5, 0.95, 'Imbalance of ratings', color='k', size=25, horizontalalignment='center', verticalalignment='top', transform=newax.transAxes) newax.text(0.5, 0.85, '(ABIDE, aggregated)', color='k', size=25, horizontalalignment='center', verticalalignment='top', transform=newax.transAxes) if out_file is None: out_file = 'raters.svg' fname, ext = op.splitext(out_file) if ext[1:] not in ['pdf', 'svg', 'png']: ext = '.svg' out_file = fname + '.svg' fig.savefig(op.abspath(out_file), format=ext[1:], bbox_inches='tight', pad_inches=0, dpi=300) return fig
df.dropna(inplace=True) df['Applaus / Rede'] = df['Applaus / Rede'].apply(lambda x: x-1) df.loc[df['Kinder int'] == 0, 'Kinder'] = 'keine' df.loc[(df['Kinder int'] > 0) & (df['Kinder int'] < 3), 'Kinder'] = '0 bis 2' df.loc[df['Kinder int'] > 2, 'Kinder'] = 'mehr als 2' df['Applaus / Absatz'] = df.apply(lambda row: (row['Applaus / Rede'] / row['Absätze']), axis=1) print(df) frauen = df.where(df['Geschlecht'] == 'weiblich').dropna() männer = df.where(df['Geschlecht'] == 'männlich').dropna() print() gs = GridSpec(4, 2) ax =[plt.subplot(gs[0, 0]), plt.subplot(gs[0, 1]), plt.subplot(gs[1, 0]), plt.subplot(gs[1, 1]), plt.subplot(gs[2, 0]), plt.subplot(gs[2, 1]), plt.subplot(gs[3, :])] plt.figure() gs2 = GridSpec(4, 2) ax2 =[plt.subplot(gs2[0, 0]), plt.subplot(gs2[0, 1]), plt.subplot(gs2[1, 0]), plt.subplot(gs2[1, 1]),
def main(): start_time = pd.datetime.now() solver_times = [] # Instantiate dynamic model model = MobileInvertedPendulum(t=0.0, step_size=0.035) # Choose length of simulation (timesteps) n_steps = 401 # Time horizon for predictive control horizon_steps = 10 # Initialise solver based on model parameters m = GEKKO_MPC(model, horizon_steps) # Convenience function def new_setpoint(var, value, weight=None, tol=0.01): var.SPHI = value * (1 + tol) var.SPLO = value * (1 - tol) if weight is not None: var.WSPHI = weight var.WSPLO = weight # Initialize data recorder to save state data to # file with an extra column for the set points params = { 'xr_sp': 0.0, 'θr_sp': 0.0, 'tau_p1': model.mvs['tau'], 'tau_p1_nn': model.mvs['tau'] } data_recorder = DataRecorder(model, n=n_steps + 1, params=params) ''' Run simulation, looping over all t[]. unpack the result using transpose operation (.T). ''' # Define setpoint changes for the demo def xr_sp_f(t): return (0 if t <= 0.5 else -0.5 if t < 4 else 0.5 if t < 6 else -0.5 if t < 8 else 0.5) def random_setpoint_generator(mu=0.1, sigma=0.25, n=30, init=None): if init is not None: current_value = init else: current_value = np.random.normal(mu, sigma) while True: for i in range(np.random.poisson(n)): yield current_value current_value = np.random.normal(mu, sigma) # Initialize random_setpoint_generator # Set init=0.0 to make it start at 0 xr_sp_random = random_setpoint_generator(mu=0.0, sigma=0.40, n=100) fig = plt.figure(figsize=(14, 9)) gs = GridSpec(3, 3) ax_a = plt.subplot(gs[0, 0]) ax_u = plt.subplot(gs[1, 0]) ax_y = plt.subplot(gs[2, 0]) ax_anim = plt.subplot(gs[:, 1:]) plt.ion() plt.show() for i in range(0, n_steps + 1): # TODO: Remove this when the problem of TR_INIT is fixed if i == 1: m.theta.TR_INIT = 1 m.xr.TR_INIT = 1 # Desired setpoints for robot angle and xr theta_sp, xr_sp = 0.0, next(xr_sp_random) new_setpoint(m.theta, theta_sp, weight=2) new_setpoint(m.xr, xr_sp, weight=1) # Store current setpoint values data_recorder.params['θr_sp'] = theta_sp data_recorder.params['xr_sp'] = xr_sp # Other setpoint options # TODO: Jim does not set these... #new_setpoint(m.theta_d, 0, weight=1) #new_setpoint(m.theta_dd, 0, weight=10) # setpoints for robot position to 0 #new_setpoint(m.xr, 0) # Do we need a weight? #new_setpoint(m.xr_d, 0, weight=1) #new_setpoint(m.xr_dd, 0, weight=10) # Run MPC solver to predict next control actions timer1 = timeit.default_timer() m.solve(remote=True) tau_p1 = m.tau.NEWVAL # Or use trained neural network instead tau_p1_nn = nn_predict.next_tau(tau=model.mvs['tau'], θw=model.variables['θw'], θw_dot=model.variables['θw_dot'], θr=model.variables['θr'], θr_dot=model.variables['θr_dot'], xr_sp=xr_sp) timer2 = timeit.default_timer() solver_times.append(timer2 - timer1) # Read next value for manipulated variable data_recorder.params['tau_p1'] = tau_p1 data_recorder.params['tau_p1_nn'] = tau_p1_nn # Save current model state to memory data_recorder.record_state() # Update dynamic model and simulate next time step model.mvs['tau'] = tau_p1 # or tau_p1_nn model.next_state() # Make sure segway has not tipped over! #assert -0.5*np.pi < θr[i+1] < 0.5*np.pi # TODO: Do we need this? # Read in measurements from the system (odeint) m.theta.MEAS = model.variables['θr'] m.theta_d.MEAS = model.variables['θr_dot'] m.phi.MEAS = model.variables['θw'] m.phi_d.MEAS = model.variables['θw_dot'] # Plot data from data recorder θr = data_recorder.data['θr'] θw = data_recorder.data['θw'] tau = data_recorder.data['tau'] t = data_recorder.data['t'] xr = data_recorder.data['xr'] xw = data_recorder.data['xw'] yr = data_recorder.data['yr'] xr_sp = data_recorder.data['xr_sp'] ax_a.cla() ax_a.grid() ax_a.plot(t[0:i], θr[0:i], 'b--', linewidth=2, label='rad Robot') ax_a.plot(t[0:i], θw[0:i], 'k--', linewidth=2, label='rad Wheel') ax_a.legend(loc='best') ax_a.set_ylabel('Angles (rad)') ax_u.cla() ax_u.grid() ax_u.plot(t[0:i], tau[0:i], '-', color='Black', label='u') ax_u.set_ylabel('Torque (N.m^2)') ax_u.legend(loc='best') ax_y.cla() ax_y.grid() ax_y.plot(t[0:i], xw[0:i], '-', color='black', label='xw') ax_y.plot(t[0:i], xr_sp[0:i], '--', color='red', lw=1, label='SP') ax_y.plot(t[0:i], xr[0:i], '-', color='blue', label='xr') ax_y.legend(loc='best') ax_y.set_xlabel('Time (s)') ax_y.set_ylabel('x position (m)') ax_anim.cla() ax_anim.plot([xw[i], xr[i]], [0, yr[i]], 'o-', color='blue', linewidth=3, markersize=6, markeredgecolor='blue', markerfacecolor='blue') ax_anim.plot([-4, 4], [0, 0], 'k--', linewidth=1) # display force on mass as a grey line # TODO: Try an arrow: #xs = [xw[i], xw[i] - tau[i]*3/1000.0] #xs if tau[i] < 0 else sorted(xs) #pyplot.arrow(x, y, dx, dy, hold=None, **kwargs) ax_anim.plot([xw[i], xw[i] - tau[i] * 3 / 1000.0], [0, 0], '-', color='gray', lw=3) # display body of pendulum L = model.constants['L'] ax_anim.plot([xr_sp[i], xr_sp[i]], [0, L], '--', color='red', lw=1) ax_anim.axis('equal') ax_anim.text(0.5, 0.05, 'time = %.2fs' % t[i]) #plt.draw() plt.pause(0.02) time_elapsed = (pd.datetime.now() - start_time).seconds time_stamp = start_time.strftime("%y%m%d%H%M") filename = 'MIP_MPC_data ' + time_stamp + '.csv' data_recorder.save_to_csv(filename) print("\nSimulation finished after %d hours, %d minutes." % (time_elapsed // 3600, time_elapsed // 60)) filename = 'MIP_MPC_plot ' + time_stamp + '.pdf' plt.savefig(filename) print("Average duration of each iteration: " "{:6.0f}ms".format(time_elapsed * 1000 / n_steps)) tmin, tmax, tmean = (min(solver_times), max(solver_times), sum(solver_times) / len(solver_times)) print("APMonitor solver speed (milliseconds):\n" "Fastest: {:6.0f}ms\n" "Slowest: {:6.0f}ms\n" " Mean: {:6.0f}ms\n".format(tmin * 1000, tmax * 1000, tmean * 1000)) print("Close plot window to continue.") plt.ioff() plt.show()
def display_grid(target_list, image_list, boxes_list, masks_list, class_ids_list, scores_list=None, category_names_list=None, title="", figsize=(16, 16), ax=None, show_mask=True, show_bbox=True, colors=None, captions=None, show_scores=True, target_shift=10, fontsize=14, linewidth=2, save=False): """ boxes: [num_instance, (y1, x1, y2, x2, class_id)] in image coordinates. masks: [height, width, num_instances] class_ids: [num_instances] class_names: list of class names of the dataset scores: (optional) confidence scores for each box title: (optional) Figure title show_mask, show_bbox: To show masks and bounding boxes or not figsize: (optional) the size of the image colors: (optional) An array or colors to use with each object captions: (optional) A list of strings to use as captions for each object """ if type(target_list) == list: M = int(np.sqrt(len(target_list))) if len(target_list) - M**2 > 1e-3: M = M + 1 else: M = 1 target_list = [target_list] image_list = [image_list] boxes_list = [boxes_list] masks_list = [masks_list] class_ids_list = [class_ids_list] if scores_list is not None: scores_list = [scores_list] # If no axis is passed, create one and automatically call show() auto_show = False if not ax: from matplotlib.gridspec import GridSpec # Use GridSpec to show target smaller than image fig = plt.figure(figsize=figsize) gs = GridSpec(M, M, hspace=0.1, wspace=0.02, left=0, right=1, bottom=0, top=1) # auto_show = True REMOVE index = 0 for m1 in range(M): for m2 in range(M): ax = plt.subplot(gs[m1, m2]) if index >= len(target_list): continue target = target_list[index] image = image_list[index] boxes = boxes_list[index] masks = masks_list[index] class_ids = class_ids_list[index] scores = scores_list[index] # Number of instances N = boxes.shape[0] if not N: print("\n*** No instances to display *** \n") else: assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0] # Generate random colors colors = visualize.random_colors(N) # Show area outside image boundaries. height, width = image.shape[:2] ax.set_ylim(height, 0) ax.set_xlim(0, width) ax.axis('off') ax.set_title(title) masked_image = image.astype(np.uint32).copy() for i in range(N): color = colors[i] # Bounding box if not np.any(boxes[i]): # Skip this instance. Has no bbox. Likely lost in image cropping. continue y1, x1, y2, x2 = boxes[i] if show_bbox: p = visualize.patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=linewidth, alpha=0.7, linestyle="dashed", edgecolor=color, facecolor='none') ax.add_patch(p) # Label if not captions: class_id = class_ids[i] score = scores[i] if scores is not None else None x = random.randint(x1, (x1 + x2) // 2) caption = "{:.3f}".format(score) if score else 'no score' else: caption = captions[i] if show_scores: ax.text(x1, y1 + 8, caption, color='w', size=int(10 / 14 * fontsize), backgroundcolor="none") # Mask mask = masks[:, :, i] if show_mask: masked_image = visualize.apply_mask( masked_image, mask, color) # Mask Polygon # Pad to ensure proper polygons for masks that touch image edges. padded_mask = np.zeros((mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8) padded_mask[1:-1, 1:-1] = mask contours = visualize.find_contours(padded_mask, 0.5) for verts in contours: # Subtract the padding and flip (y, x) to (x, y) verts = np.fliplr(verts) - 1 p = visualize.Polygon(verts, facecolor="none", edgecolor=color) ax.add_patch(p) ax.imshow(masked_image.astype(np.uint8)) target_height, target_width = target.shape[:2] target_height = target_height // 2 target_width = target_width // 2 target_area = target_height * target_width target_scaling = np.sqrt((192 // 2 * 96 // 2) / target_area) target_height = int(target_height * target_scaling) target_width = int(target_width * target_scaling) ax.imshow(target, extent=[ target_shift, target_shift + target_width * 2, height - target_shift, height - target_shift - target_height * 2 ], zorder=9) rect = visualize.patches.Rectangle( (target_shift, height - target_shift), target_width * 2, -target_height * 2, linewidth=5, edgecolor='white', facecolor='none', zorder=10) ax.add_patch(rect) if category_names_list is not None: plt.title(category_names_list[index], fontsize=fontsize) index = index + 1 if auto_show: plt.show() if save: fig.savefig('grid.pdf', bbox_inches='tight') return