def grabHistory(self, item, itemName): """Grab item's 7 days historical prices for all cities, and plots them. - Grabbed from Data Project API. - Plots timeseries to 'plot.png'. """ # Outliers makes the plot useless, so we find and remove them # This function is not very effective though def reject_outliers(data): d = [abs(i - statistics.median(data)) for i in data] mdev = statistics.median(d) s = [i / mdev if mdev else 0 for i in d] m = 10 indices = [i for (i, val) in enumerate(s) if val < m] newData = [data[i] for i in indices] return newData, indices # Find API URL for past 7 days # historyURL requires dates in %m-%d-%Y format today = DT.datetime.utcnow() numDays = 7 date = (today - DT.timedelta(days=numDays)).strftime("%m-%d-%Y") fullURL = (self.historyURL + item + "?date=" + date + self.historyLocationURL + "&time-scale=1") # List will have 10 different indices for 10 different cities # The indices corresponds to this ordering of cities (Alphabetical): # Arthurs, BlackMarket, Bridgewatch, Caerleon, Fort Sterling, Lymhurst, Martlock, Merlyns, Morganas, Thetford prices_minAll = [[], [], [], [], [], [], [], [], [], []] timestampsAll = [[], [], [], [], [], [], [], [], [], []] itemCountsAll = [[], [], [], [], [], [], [], [], [], []] # Get price try: with urllib.request.urlopen(fullURL) as url: prices = json.loads(url.read().decode()) except Exception as e: print(e) return else: for price in prices: if price["quality"] == 1: if price["location"] == "Arthurs Rest": prices_minAll[0].extend(price["data"]["prices_avg"]) timestampsAll[0].extend(price["data"]["timestamps"]) itemCountsAll[0].extend(price["data"]["item_count"]) elif price["location"] == "Black Market": prices_minAll[1].extend(price["data"]["prices_avg"]) timestampsAll[1].extend(price["data"]["timestamps"]) itemCountsAll[1].extend(price["data"]["item_count"]) elif price["location"] == "Bridgewatch": prices_minAll[2].extend(price["data"]["prices_avg"]) timestampsAll[2].extend(price["data"]["timestamps"]) itemCountsAll[2].extend(price["data"]["item_count"]) elif price["location"] == "Caerleon": prices_minAll[3].extend(price["data"]["prices_avg"]) timestampsAll[3].extend(price["data"]["timestamps"]) itemCountsAll[3].extend(price["data"]["item_count"]) elif price["location"] == "Fort Sterling": prices_minAll[4].extend(price["data"]["prices_avg"]) timestampsAll[4].extend(price["data"]["timestamps"]) itemCountsAll[4].extend(price["data"]["item_count"]) elif price["location"] == "Lymhurst": prices_minAll[5].extend(price["data"]["prices_avg"]) timestampsAll[5].extend(price["data"]["timestamps"]) itemCountsAll[5].extend(price["data"]["item_count"]) elif price["location"] == "Martlock": prices_minAll[6].extend(price["data"]["prices_avg"]) timestampsAll[6].extend(price["data"]["timestamps"]) itemCountsAll[6].extend(price["data"]["item_count"]) elif price["location"] == "Merlyns Rest": prices_minAll[7].extend(price["data"]["prices_avg"]) timestampsAll[7].extend(price["data"]["timestamps"]) itemCountsAll[7].extend(price["data"]["item_count"]) elif price["location"] == "Morganas Rest": prices_minAll[8].extend(price["data"]["prices_avg"]) timestampsAll[8].extend(price["data"]["timestamps"]) itemCountsAll[8].extend(price["data"]["item_count"]) elif price["location"] == "Thetford": prices_minAll[9].extend(price["data"]["prices_avg"]) timestampsAll[9].extend(price["data"]["timestamps"]) itemCountsAll[9].extend(price["data"]["item_count"]) # Parse datetime for (i, timestamps) in enumerate(timestampsAll): timestampsAll[i] = [ DT.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S") for timestamp in timestamps ] # Reject outliers from prices data as well as their corresponding timestamps for (i, prices) in enumerate(prices_minAll): try: prices_minAll[i], indices = reject_outliers(prices) timestampsAll[i] = [timestampsAll[i][j] for j in indices] itemCountsAll[i] = [itemCountsAll[i][j] for j in indices] # Pass if prices_minAll = [] except: pass # Plot labels and plot colors names = [ "Arthur's Rest", "Black Market", "Bridgewatch", "Caerleon", "Fort Sterling", "Lymhurst", "Martlock", "Merlyn's Rest", "Morgana's Rest", "Thetford", ] colors = [ "red", "rosybrown", "orange", "black", "slategrey", "forestgreen", "blue", "darkturquoise", "purple", "brown", ] plotOrders = [3, 2, 4, 5, 6, 9] # Plot the data plt.style.use("seaborn") fig, ax = plt.subplots(nrows=3, ncols=2, figsize=(15, 8.75), sharex=True, sharey=True) ax = ax.flatten() fig.suptitle(f"7 Days Sell Order Prices for {itemName} ({item})") fig.tight_layout(rect=[0, 0.03, 1, 0.95]) plt.subplots_adjust(wspace=0.025, hspace=0.15) for j in range(6): # Create gridspec in each subplot gs = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=ax[j], height_ratios=[4, 1], hspace=0.1) # First grid is for prices ax0 = plt.subplot(gs[0]) # Second grid for item counts if j > 0: ax1 = plt.subplot(gs[1], sharex=ax0, sharey=axPrev) else: ax1 = plt.subplot(gs[1], sharex=ax0) # Iterate over all cities and plot each one for (i, timestamp) in enumerate(timestampsAll): try: # Only plot those that are in plotOrders if i in plotOrders: ax0.plot( timestampsAll[i], prices_minAll[i], color="gray", alpha=0.3, ) # Pass if prices_minAll = [] except: pass # Plot the main city ax0.plot( timestampsAll[plotOrders[j]], prices_minAll[plotOrders[j]], color=colors[plotOrders[j]], ) # Plot item counts ax1.bar( timestampsAll[plotOrders[j]], itemCountsAll[plotOrders[j]], width=0.04, ) # Remember item counts axis for sharey if j == 0: axPrev = ax1 # Only show axis for left and bottom plt.setp(ax0.get_xticklabels(), visible=False) if j % 2: plt.setp(ax0.get_yticklabels(), visible=False) plt.setp(ax1.get_yticklabels(), visible=False) else: ax0.set_ylabel("Silvers") ax1.set_ylabel("Volume") if j not in (4, 5): plt.setp(ax1.get_xticklabels(), visible=False) # Title and date axis ax0.set_title(f"{names[plotOrders[j]]}") ax1.xaxis.set_major_formatter(mdates.DateFormatter("%m/%d")) fig.savefig("plot.png", bbox_inches="tight") return
def plot_hexbin(self): from matplotlib import cm cmap = cm.coolwarm cmap.set_bad('black') for plot in range(self.numplots): col = plot % self.plots_per_row row = int(plot / float(self.plots_per_row)) # split the ax to make room for the colorbar and for each of the # groups sub_grid = gridspec.GridSpecFromSubplotSpec(self.numlines, 2, subplot_spec=self.grids[row, col], width_ratios=[0.92, 0.08], wspace=0.05, hspace=0.1) ax = self.fig.add_subplot(sub_grid[0, 0]) ax.tick_params( axis='y', which='both', left='off', right='off', labelleft='on') if self.per_group: title = self.hm.matrix.group_labels[plot] else: title = self.hm.matrix.sample_labels[plot] vmin = np.inf vmax = -np.inf for data_idx in range(self.numlines): # get the max and min if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot sub_matrix = self.hm.matrix.get_matrix(_row, _col) ma = sub_matrix['matrix'] x_values = np.tile(np.arange(ma.shape[1]), (ma.shape[0], 1)) img = ax.hexbin(x_values.flatten(), ma.flatten(), cmap=cmap, mincnt=1) _vmin, _vmax = img.get_clim() if _vmin < vmin: vmin = _vmin if _vmax > vmax: vmax = _vmax self.fig.delaxes(ax) # iterate again after having computed the vmin and vmax ax_list = [] for data_idx in range(self.numlines)[::-1]: ax = self.fig.add_subplot(sub_grid[data_idx, 0]) if data_idx == 0: ax.set_title(title) if data_idx != self.numlines - 1: plt.setp(ax.get_xticklabels(), visible=False) if self.per_group: _row, _col = plot, data_idx else: _row, _col = data_idx, plot sub_matrix = self.hm.matrix.get_matrix(_row, _col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] ma = sub_matrix['matrix'] ax.set_axis_bgcolor('black') x_values = np.tile(np.arange(ma.shape[1]), (ma.shape[0], 1)) img = ax.hexbin(x_values.flatten(), ma.flatten(), cmap=cmap, mincnt=1, vmin=vmin, vmax=vmax) # remove the numbers of the y axis for all plots ax.axes.set_ylabel(label) ax_list.append(ax) lims = ax.get_ylim() if self.y_min is not None: lims = (self.y_min, lims[1]) if self.y_max is not None: lims = (lims[0], self.y_max) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax.set_ylim(lims) if np.ceil(max(self.xticks)) != float(ma.shape[1]): tickscale = float(sub_matrix['matrix'].shape[1]) / max(self.xticks) xticks_use = [x * tickscale for x in self.xticks] ax_list[0].axes.set_xticks(xticks_use) else: ax_list[0].axes.set_xticks(self.xticks) ax_list[0].axes.set_xticklabels(self.xtickslabel) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_list[-1].xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') cax = self.fig.add_subplot(sub_grid[:, 1]) self.fig.colorbar(img, cax=cax) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=self.dpi, format=self.image_format) plt.close()
def spikesplot(ts_z, outer_gs=None, tr=None, zscored=True, spike_thresh=6., title='Spike plot', ax=None, cmap='viridis', hide_x=True, nskip=0): """ A spikes plot. Thanks to Bob Dogherty (this docstring needs be improved with proper ack) """ if ax is None: ax = plt.gca() if outer_gs is not None: gs = mgs.GridSpecFromSubplotSpec(1, 2, subplot_spec=outer_gs, width_ratios=[1, 100], wspace=0.0) ax = plt.subplot(gs[1]) # Define TR and number of frames if tr is None: tr = 1. # Load timeseries, zscored slice-wise nslices = ts_z.shape[0] ntsteps = ts_z.shape[1] # Load a colormap my_cmap = get_cmap(cmap) norm = Normalize(vmin=0, vmax=float(nslices - 1)) colors = [my_cmap(norm(sl)) for sl in range(nslices)] stem = len(np.unique(ts_z).tolist()) == 2 # Plot one line per axial slice timeseries for sl in range(nslices): if not stem: ax.plot(ts_z[sl, :], color=colors[sl], lw=0.5) else: markerline, stemlines, baseline = ax.stem(ts_z[sl, :]) plt.setp(markerline, 'markerfacecolor', colors[sl]) plt.setp(baseline, 'color', colors[sl], 'linewidth', 1) plt.setp(stemlines, 'color', colors[sl], 'linewidth', 1) # Handle X, Y axes ax.grid(False) # Handle X axis last = ntsteps - 1 ax.set_xlim(0, last) xticks = list(range(0, last)[::20]) + [last] if not hide_x else [] ax.set_xticks(xticks) if not hide_x: if tr is None: ax.set_xlabel('time (frame #)') else: ax.set_xlabel('time (s)') ax.set_xticklabels( ['%.02f' % t for t in (tr * np.array(xticks)).tolist()]) # Handle Y axis if zscored: ax.set_ylabel('z-score') zs_max = np.abs(ts_z).max() ax.set_ylim((-(np.abs(ts_z[:, nskip:]).max()) * 1.05, (np.abs(ts_z[:, nskip:]).max()) * 1.05)) ytick_vals = np.arange(0.0, zs_max, float(np.floor(zs_max / 2.))) yticks = list(reversed( (-1.0 * ytick_vals[ytick_vals > 0]).tolist())) + ytick_vals.tolist() # TODO plot min/max or mark spikes # yticks.insert(0, ts_z.min()) # yticks += [ts_z.max()] for val in ytick_vals: ax.plot((0, ntsteps - 1), (-val, -val), 'k:', alpha=.2) ax.plot((0, ntsteps - 1), (val, val), 'k:', alpha=.2) # Plot spike threshold if zs_max < spike_thresh: ax.plot((0, ntsteps - 1), (-spike_thresh, -spike_thresh), 'k:') ax.plot((0, ntsteps - 1), (spike_thresh, spike_thresh), 'k:') else: ax.set_ylabel('air sgn. intensity') yticks = [ ts_z[:, nskip:].min(), np.median(ts_z[:, nskip:]), ts_z[:, nskip:].max() ] ax.set_ylim(ts_z[:, nskip:].min() * 0.95, ts_z[:, nskip:].max() * 1.05) if yticks: ax.set_yticks(yticks) ax.set_yticklabels(['%.02f' % y for y in yticks]) # Plot maximum and minimum horizontal lines ax.plot((0, ntsteps - 1), (yticks[0], yticks[0]), 'k:') ax.plot((0, ntsteps - 1), (yticks[-1], yticks[-1]), 'k:') for side in ["top", "right"]: ax.spines[side].set_color('none') ax.spines[side].set_visible(False) if not hide_x: ax.spines["bottom"].set_position(('outward', 20)) ax.xaxis.set_ticks_position('bottom') else: ax.spines["bottom"].set_color('none') ax.spines["bottom"].set_visible(False) ax.spines["left"].set_position(('outward', 30)) ax.yaxis.set_ticks_position('left') # labels = [label for label in ax.yaxis.get_ticklabels()] # labels[0].set_weight('bold') # labels[-1].set_weight('bold') if title: ax.set_title(title) return ax
def compare_video(): fnames = ["video_z", "video_y", "video_j", "video_h", "video_ks"] _fnames = np.array_split(fnames, 2) fig = plt.figure(figsize=(20, 10), dpi=75) fig.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.08, wspace=0.4, hspace=0.23) ogs = gridspec.GridSpec(2, len(_fnames[0])) for i, fname in enumerate(fnames): print fname igs = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=ogs[i], wspace=0, hspace=0) ax1 = fig.add_subplot(igs[0, 0]) ax2 = fig.add_subplot(igs[0, 1]) ax3 = fig.add_subplot(igs[1, 0], sharex=ax1, sharey=ax1) ax4 = fig.add_subplot(igs[1, 1], sharex=ax2, sharey=ax2) bins_M = np.arange(0, 40, 0.1) bins_dM = np.arange(-5, 5, 0.01) bins_SN = 10**np.arange(-2, 7, 0.02) bins_dSN = 10**np.arange(-2, 2, 0.01) cat_DR = fitsio.getdata("catalog_DR_%s.video_matched.fits" % fname) cat_video = fitsio.getdata("catalog_%s.video_matched.fits" % fname) cat_video_errfix = fitsio.getdata( "catalog_%s_errfix.video_matched.fits" % fname) instr, filt = fname.split('_') mag_DR = cat_DR["MAG_AUTO"] mag_video = cat_video["%s_MAG_AUTO" % filt[0].upper()] mag_video_errfix = cat_video_errfix["%s_MAG_AUTO" % filt[0].upper()] cond = (cat_video["%s_MAGERR_AUTO" % filt[0].upper()] < 10) & ( cat_video_errfix["%s_MAGERR_AUTO" % filt[0].upper()] < 10) sn_DR = (cat_DR["FLUX_AUTO"] / cat_DR["FLUXERR_AUTO"])[cond] sn_video = 1. / (10**( cat_video["%s_MAGERR_AUTO" % filt[0].upper()][cond] / 2.5) - 1) sn_video_errfix = 1. / (10**( cat_video_errfix["%s_MAGERR_AUTO" % filt[0].upper()][cond] / 2.5) - 1) plot_hist(ax1, mag_DR, mag_DR - mag_video, binsx=bins_M, binsy=bins_dM) plot_hist(ax2, sn_DR, sn_video / sn_DR, binsx=bins_SN, binsy=bins_dSN) plot_hist(ax3, mag_DR, mag_DR - mag_video_errfix, binsx=bins_M, binsy=bins_dM) plot_hist(ax4, sn_DR, sn_video_errfix / sn_DR, binsx=bins_SN, binsy=bins_dSN) ax1.text(0.96, 0.96, fname, fontsize=14, fontweight=600, va='top', ha='left', transform=ax1.transAxes) for ax in [ax1, ax3]: ax.axhline(0, c='k', lw=1.2, ls='--') ax.set_xlim(11, 27) ax.set_ylim(-1.4, 1.4) for ax in [ax2, ax4]: ax.axhline(1, c='k', lw=1.2, ls='--') ax.set_yscale("log") ax.set_xscale("log") ax.set_xlim(1e0, 1e5) ax.set_ylim(10**-2, 10**2) ax.yaxis.tick_right() ax.yaxis.set_label_position('right') ax3.set_ylabel("$\Delta$M") ax4.set_ylabel("SN ratio") ax4.set_xlabel("SN") ax3.set_xlabel('MAG_AUTO') _ = [ label.set_visible(False) for label in ax1.get_xticklabels() + ax2.get_xticklabels() ] fig.savefig("plots/compare_video_auto.png")
def addProfilePlot(hm, plt, fig, grids, iterNum, iterNum2, perGroup, averageType, plot_type, yAxisLabel, color_list, yMin, yMax, wspace, hspace, colorbar_position, label_rotation=0.0): """ A function to add profile plots to the given figure, possibly in a custom grid subplot which mimics a tight layout (if wspace and hspace are not None) """ if wspace is not None and hspace is not None: if colorbar_position == 'side': gridsSub = gridspec.GridSpecFromSubplotSpec( 1, iterNum, subplot_spec=grids[0, :-1], wspace=wspace, hspace=hspace) else: gridsSub = gridspec.GridSpecFromSubplotSpec( 1, iterNum, subplot_spec=grids[0, :], wspace=wspace, hspace=hspace) ax_list = [] globalYmin = np.inf globalYmax = -np.inf for sample_id in range(iterNum): if perGroup: title = hm.matrix.group_labels[sample_id] tickIdx = sample_id % hm.matrix.get_num_samples() else: title = hm.matrix.sample_labels[sample_id] tickIdx = sample_id if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: ax_profile = fig.add_subplot(grids[0, sample_id]) else: if wspace is not None and hspace is not None: ax_profile = fig.add_subplot(gridsSub[0, sample_id]) else: ax_profile = fig.add_subplot(grids[0, sample_id]) ax_profile.set_title(title) for group in range(iterNum2): if perGroup: sub_matrix = hm.matrix.get_matrix(sample_id, group) line_label = sub_matrix['sample'] else: sub_matrix = hm.matrix.get_matrix(group, sample_id) line_label = sub_matrix['group'] plot_single(ax_profile, sub_matrix['matrix'], averageType, color_list[group], line_label, plot_type=plot_type) if sample_id > 0 and len(yMin) == 1 and len(yMax) == 1: plt.setp(ax_profile.get_yticklabels(), visible=False) if sample_id == 0 and yAxisLabel != '': ax_profile.set_ylabel(yAxisLabel) xticks, xtickslabel = hm.getTicks(tickIdx) if np.ceil(max(xticks)) != float(sub_matrix['matrix'].shape[1] - 1): tickscale = float(sub_matrix['matrix'].shape[1] - 1) / max(xticks) xticks_use = [x * tickscale for x in xticks] ax_profile.axes.set_xticks(xticks_use) else: ax_profile.axes.set_xticks(xticks) ax_profile.axes.set_xticklabels(xtickslabel, rotation=label_rotation) ax_list.append(ax_profile) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax_profile.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') globalYmin = min(np.float64(globalYmin), ax_profile.get_ylim()[0]) globalYmax = max(globalYmax, ax_profile.get_ylim()[1]) # It turns out that set_ylim only takes np.float64s for sample_id, subplot in enumerate(ax_list): localYMin = yMin[sample_id % len(yMin)] localYMax = yMax[sample_id % len(yMax)] lims = [globalYmin, globalYmax] if localYMin: if localYMax: lims = (np.float64(localYMin), np.float64(localYMax)) else: lims = (np.float64(localYMin), lims[1]) elif localYMax: lims = (lims[0], np.float64(localYMax)) if lims[0] >= lims[1]: lims = (lims[0], lims[0] + 1) ax_list[sample_id].set_ylim(lims) return ax_list
if not opts.projection: # Add scale bar, 1/4 width of the plot ax.plot([0.0625, 0.3125], [0.0625, 0.0625], color='black', linewidth=1, transform=ax.transAxes) ax.text(0.0625, 0.0625, '{0:d} Mpc'.format(int(np.round(0.5 * max_distance))), fontsize=8, transform=ax.transAxes, verticalalignment='bottom') # Create marginal distance plot. progress.update(-1, 'Plotting distance') gs1 = gridspec.GridSpecFromSubplotSpec(5, 5, gs[0, 1]) ax = fig.add_subplot(gs1[1:-1, 1:-1]) # Plot marginal distance distribution, integrated over the whole sky. d = np.linspace(0, max_distance) ax.fill_between(d, marginal_pdf(d, prob, mu, sigma, norm), alpha=0.5, color=colors[0]) # Plot conditional distance distribution at true position # and mark true distance. for (ra, dec, dist), color in zip(opts.radecdist, colors[1:]): theta = 0.5 * np.pi - np.deg2rad(dec) phi = np.deg2rad(ra) ipix = hp.ang2pix(nside, theta, phi)
def plot_img_with_dendrograms(self, use_abs_cor=True): ''' Plot an image or correlation matrix along with dendrograms Uses methods from: http://nbviewer.ipython.org/github/ucsd-scientific-python/user-group/blob/master/presentations/20131016/hierarchical_clustering_heatmaps_gridspec.ipynb Parameters ----------- use_abs_cor : {True, False}, optional Use the absolute values of correlation matrix ''' import matplotlib.gridspec as gridspec import scipy.cluster.hierarchy as sch # helper for cleaning up axes by removing ticks, tick labels, frame, etc. def clean_axis(ax): """Remove ticks, tick labels, and frame from axis""" ax.get_xaxis().set_ticks([]) ax.get_yaxis().set_ticks([]) for sp in ax.spines.values(): sp.set_visible(False) fig = plt.figure() heatmapGS = gridspec.GridSpec(2, 2, wspace=0.0, hspace=0.0, width_ratios=[1, 0.25], height_ratios=[0.25, 1]) D = np.abs(self.array) ## Col Dendrogram col_denAX = fig.add_subplot(heatmapGS[0, 0]) clusters1 = sch.linkage(D, method='centroid') sch.set_link_color_palette(['black']) col_denD = sch.dendrogram(clusters1, labels=self.df.columns.values, orientation='top', color_threshold=np.inf) clean_axis(col_denAX) ## Row Dendrogram row_denAX = fig.add_subplot(heatmapGS[1, 1]) clusters2 = sch.linkage(D, method='single') sch.set_link_color_palette(['black']) row_denD = sch.dendrogram(clusters2, labels=self.df.index.values, orientation='left', color_threshold=np.inf) clean_axis(row_denAX) # Heatmap heatmapAX = fig.add_subplot(heatmapGS[1, 0]) idx1 = row_denD['leaves'] idx2 = col_denD['leaves'] D_remap = D.copy() D_remap = D_remap[idx1, :] D_remap = D_remap[:, idx2] axi = heatmapAX.imshow(D_remap, interpolation='nearest', aspect='auto', origin='lower', vmin=0, vmax=1) def _format_coord(x, y): x = int(x + 0.5) y = int(y + 0.5) par_row = row_denD.items()[0][1][y] par_col = col_denD.items()[0][1][x] try: return "%.3f %s | %s" % (D_remap[y, x], par_row, par_col) except IndexError: return "" heatmapAX.format_coord = _format_coord clean_axis(heatmapAX) ## row labels ## heatmapAX.set_yticks(np.arange(self.df.shape[0])) heatmapAX.yaxis.set_ticks_position('left') heatmapAX.set_yticklabels(self.df.index[row_denD['leaves']]) # remove the tick lines for l in heatmapAX.get_xticklines() + heatmapAX.get_yticklines(): l.set_markersize(0) ## col labels ## heatmapAX.set_xticks(np.arange(self.df.shape[1])) heatmapAX.xaxis.set_ticks_position('bottom') xlabelsL = heatmapAX.set_xticklabels( self.df.columns[col_denD['leaves']]) # rotate labels 90 degrees for label in xlabelsL: label.set_rotation(90) # remove the tick lines for l in heatmapAX.get_xticklines() + heatmapAX.get_yticklines(): l.set_markersize(0) ### scale colorbar ### scale_cbGSSS = gridspec.GridSpecFromSubplotSpec( 1, 2, subplot_spec=heatmapGS[0, 1], wspace=0.5, hspace=0.5) scale_cbAX = fig.add_subplot( scale_cbGSSS[0, 0]) # colorbar for scale in upper corner cb = fig.colorbar( axi, scale_cbAX ) # note that we tell colorbar to use the scale_cbAX axis cb.set_label('Abs. Cor.') cb.ax.yaxis.set_ticks_position( 'right' ) # move ticks to left side of colorbar to avoid problems with tight_layout cb.ax.yaxis.set_label_position( 'right' ) # move label to left side of colorbar to avoid problems with tight_layout cb.outline.set_linewidth(0) # make colorbar labels smaller tickL = cb.ax.yaxis.get_ticklabels() for t in tickL: t.set_fontsize(t.get_fontsize() - 3) heatmapGS.tight_layout(fig, h_pad=0.1, w_pad=0.5)
i = 0 n = 0 l = 0 swcf = 'SWCF' lwcf = 'LWCF' present = '_10' casenames = ['_105', '_1025', '_10', '_0975', '_095', '_0925', '_09'] titles = ['1.05', '1.025', '1.0', '0.975', '0.95', '0.925', '0.9'] while i < 2: inner_grid = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=outer_grid[i], wspace=0.0, hspace=0.3) if i == 0: field = swcf outfilebase = outfilebase else: field = lwcf outfilebase = outfilebase n = 0 l = 1 for y in casenames: CASENAME = casenames[n] ax = fig.add_subplot(inner_grid[n])
error1d = np.random.random(len(noise1d)) + 0.4 # ---------------------------# # -- Initializing the image -- # # ---------------------------- # f = plt.figure(figsize=(10.5, 9)) gs0 = gridspec.GridSpec(2, 1, height_ratios=[1, 0.9], hspace=0.1) # the main subplots # ------------- # # -- TOP ROW -- # # ------------- # gs01 = gridspec.GridSpecFromSubplotSpec( 1, 2, subplot_spec=gs0[0], # the top panel's subplots width_ratios=[1.2, 2], wspace=0.22) # --> RIGHT SIDE: the Lya spectrum line = 'lya' band = 'Y' # The subplot gs001 is made up of 3 subplots where the top and bottom are just used to # center the middle one more accurately -- they aren't necessary if you don't care THAT much :) gs001 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs01[1], height_ratios=[0.05, 1, 0.12], hspace=0.0) # This is the real subplot for the data (the middle one from gs001), split into 2 subplots
def show( self, show: Optional[bool] = None, save: Union[str, bool, None] = None, ): """ Render the image Parameters ---------- show Show the plot, do not return axis. If false, plot is not shown and axes returned. save If `True` or a `str`, save the figure. A string is appended to the default filename. Infer the filetype if ending on {{`'.pdf'`, `'.png'`, `'.svg'`}}. Returns ------- If `show=False`: Dict of :class:`~matplotlib.axes.Axes`. The dict key indicates the type of ax (eg. mainplot_ax) Examples ------- >>> adata = sc.datasets.pbmc68k_reduced() >>> markers = ['C1QA', 'PSAP', 'CD79A', 'CD79B', 'CST3', 'LYZ'] >>> sc.pl.Plot(adata, markers, groupby='bulk_labels').show() Get the axes >>> axes_dict = sc.pl.Plot(adata, markers, groupby='bulk_labels').show(show=False) >>> axes_dict['mainplot_ax'].grid(True) >>> plt.show() Save image >>> sc.pl.BasePlot(adata, markers, groupby='bulk_labels').show(save='plot.pdf') """ category_height = self.DEFAULT_CATEGORY_HEIGHT category_width = self.DEFAULT_CATEGORY_WIDTH if self.height is None: mainplot_height = len(self.categories) * category_height mainplot_width = (len(self.var_names) * category_width + self.group_extra_size) if self.are_axes_swapped: mainplot_height, mainplot_width = mainplot_width, mainplot_height height = mainplot_height + 1 # +1 for labels # if the number of categories is small use # a larger height, otherwise the legends do not fit self.height = max([self.min_figure_height, height]) self.width = mainplot_width + self.legends_width else: self.min_figure_height = self.height mainplot_height = self.height mainplot_width = self.width - (self.legends_width + self.group_extra_size) return_ax_dict = {} # define a layout of 1 rows x 2 columns # first ax is for the main figure. # second ax is to plot legends legends_width_spacer = 0.7 / self.width fig, gs = make_grid_spec( self.ax or (self.width, self.height), nrows=1, ncols=2, wspace=legends_width_spacer, width_ratios=[ mainplot_width + self.group_extra_size, self.legends_width ], ) # the main plot is divided into three rows and two columns # first row is an spacer, that is adjusted in case the # legends need more height than the main plot # second row is for brackets (if needed), # third row is for mainplot and dendrogram (if needed) if self.has_var_groups: # add some space in case 'brackets' want to be plotted on top of the image if self.are_axes_swapped: var_groups_height = category_height else: var_groups_height = category_height / 2 else: var_groups_height = 0 mainplot_width = mainplot_width - self.group_extra_size spacer_height = self.height - var_groups_height - mainplot_height if not self.are_axes_swapped: height_ratios = [spacer_height, var_groups_height, mainplot_height] width_ratios = [mainplot_width, self.group_extra_size] else: height_ratios = [ spacer_height, self.group_extra_size, mainplot_height ] width_ratios = [mainplot_width, var_groups_height] # gridspec is the same but rows and columns are swapped if self.fig_title is not None and self.fig_title.strip() != '': # for the figure title use the ax that contains # all the main graphical elements (main plot, dendrogram etc) # otherwise the title may overlay with the figure. # also, this puts the title centered on the main figure and not # centered between the main figure and the legends _ax = fig.add_subplot(gs[0, 0]) _ax.axis('off') _ax.set_title(self.fig_title) mainplot_gs = gridspec.GridSpecFromSubplotSpec( nrows=3, ncols=2, wspace=0.0, hspace=0.0, subplot_spec=gs[0, 0], width_ratios=width_ratios, height_ratios=height_ratios, ) main_ax = fig.add_subplot(mainplot_gs[2, 0]) return_ax_dict['mainplot_ax'] = main_ax if not self.are_axes_swapped: if self.plot_group_extra is not None: group_extra_ax = fig.add_subplot(mainplot_gs[2, 1], sharey=main_ax) group_extra_orientation = 'right' if self.has_var_groups: gene_groups_ax = fig.add_subplot(mainplot_gs[1, 0], sharex=main_ax) var_group_orientation = 'top' else: if self.plot_group_extra: group_extra_ax = fig.add_subplot(mainplot_gs[1, 0], sharex=main_ax) group_extra_orientation = 'top' if self.has_var_groups: gene_groups_ax = fig.add_subplot(mainplot_gs[2, 1], sharey=main_ax) var_group_orientation = 'right' if self.plot_group_extra is not None: if self.plot_group_extra['kind'] == 'dendrogram': _plot_dendrogram( group_extra_ax, self.adata, self.groupby, dendrogram_key=self.plot_group_extra['dendrogram_key'], ticks=self.plot_group_extra['dendrogram_ticks'], orientation=group_extra_orientation, ) if self.plot_group_extra['kind'] == 'group_totals': self._plot_totals(group_extra_ax, group_extra_orientation) return_ax_dict['group_extra_ax'] = group_extra_ax # plot group legends on top or left of main_ax (if given) if self.has_var_groups: self._plot_var_groups_brackets( gene_groups_ax, group_positions=self.var_group_positions, group_labels=self.var_group_labels, rotation=self.var_group_rotation, left_adjustment=0.2, right_adjustment=0.7, orientation=var_group_orientation, ) return_ax_dict['gene_group_ax'] = gene_groups_ax # plot the mainplot normalize = self._mainplot(main_ax) # code from pandas.plot in add_totals adds # minor ticks that need to be removed main_ax.yaxis.set_tick_params(which='minor', left=False, right=False) main_ax.xaxis.set_tick_params(which='minor', top=False, bottom=False, length=0) main_ax.set_zorder(100) if self.legends_width > 0: legend_ax = fig.add_subplot(gs[0, 1]) self._plot_legend(legend_ax, return_ax_dict, normalize) _utils.savefig_or_show(self.DEFAULT_SAVE_PREFIX, show=show, save=save) self.ax_dict = return_ax_dict show = settings.autoshow if show is None else show if not show: return return_ax_dict
def plotstorms(flowserie, rainserie, selected_storm, tsfreq=None, tsfrequnit=None, make_comparable=False, period_title=False): """ Plot Flow-Rain plots for every storm period selected, optimal sizes and configuration done for 1 till 5 subplots (storms) """ if len(selected_storm) > 6: raise Exception('Split plotting up in multiple figures') fig = plt.figure(facecolor='white', figsize=(12, _getsize(len(selected_storm)))) gs0 = gridspec.GridSpec(len(selected_storm), 1) gs0.update(hspace=0.35) for j, storm in enumerate(selected_storm): gs00 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[j], hspace=0.0, height_ratios=[2, 4]) # RAIN PLOT ax0 = fig.add_subplot(gs00[0]) ax0.plot(rainserie[storm['startdate']:storm['enddate']].index. to_pydatetime(), rainserie[storm['startdate']:storm['enddate']].values, linestyle='steps') # FLOW PLOT stormflow = flowserie[storm['startdate']:storm['enddate']] ax1 = fig.add_subplot(gs00[1], sharex=ax0) ax1.plot(stormflow.index.to_pydatetime(), stormflow.values, label=r" Measured Flow ($m^3s^{-1}$)") # if single plots of flow/rain -> set specific color if flowserie.ndim == 1: ax1.lines[0].set_color('#08519c') if rainserie.ndim == 1: ax0.lines[0].set_color('#6baed6') # ADAPT ticks for storm-conditions (less than a month timeseries) ax0.yaxis.set_major_locator(LinearLocator(3)) ax1.yaxis.set_major_locator(LinearLocator(3)) ax1.xaxis.set_minor_locator(mpl.dates.DayLocator()) ax1.xaxis.set_minor_formatter(mpl.dates.DateFormatter('%d')) ax1.xaxis.set_major_locator(mpl.dates.MonthLocator(bymonthday= [1, storm['startdate'].day + \ _control_dayhour(storm['startdate'])])) ax1.xaxis.set_major_formatter(mpl.dates.DateFormatter('\n %b %Y')) # Add the labels of the different flows if j == 0: _add_labels_above(ax0, fig, flowserie.ndim, rainserie.ndim) # Print the start and end period as title above subplots if period_title: ax0.set_title(storm['startdate'].strftime("%d/%m/%y") + " - " + storm['enddate'].strftime("%d/%m/%y"), fontweight='bold', fontsize=12) # Looks of the rainplot ax0.set_xlabel('') ax0.invert_yaxis() ax0.yaxis.tick_right() ax0.spines['bottom'].set_visible(False) ax0.spines['top'].set_visible(False) plt.setp(ax0.get_xminorticklabels(), visible=False) plt.setp(ax0.get_xmajorticklabels(), visible=False) plt.setp(ax0.get_xminorticklabels(), visible=False) # looks of the flowplot ax1.spines['top'].set_visible(False) ax1.spines['bottom'].set_visible(False) ax1.set_xlabel('') plt.draw() all_axes = fig.get_axes() # Give all the subplots the same y-bounds if make_comparable: _make_comparable(all_axes) return fig, all_axes
#################################################### # # Set up Figure (2 panels, arranged in 2x1 grid) # #################################################### pylab.figure(1, figsize=(7, 1.7)) fig = pylab.gcf() # make three panels panels outer_grid = gridspec.GridSpec(1, 2, width_ratios=[3, 4], wspace=0.2) species_grid = gridspec.GridSpecFromSubplotSpec( 2, 1, height_ratios=[1.0 / 1.7, 0.7 / 1.7], subplot_spec=outer_grid[1], hspace=0) example_axis = plt.Subplot(fig, outer_grid[0]) fig.add_subplot(example_axis) example_axis.set_ylabel('Linkage disequilibrium, $\sigma^2_d$') example_axis.set_xlabel('Distance between SNPs, $\ell$') example_axis.spines['top'].set_visible(False) example_axis.spines['right'].set_visible(False) example_axis.spines['bottom'].set_zorder(22) example_axis.get_xaxis().tick_bottom() example_axis.get_yaxis().tick_left()
def plot_carpet_ts(timeseries, modules, atlas=None, background_file=None, nskip=0, size=(950, 800), subplot=None, title=None, output_file="regts.png"): """ Adapted from: https://github.com/poldracklab/niworkflows Plot an image representation of voxel intensities across time also know as the "carpet plot" or "Power plot". See Jonathan Power Neuroimage 2017 Jul 1; 154:150-158. Parameters ---------- timeseries : numpy.ndarray See http://nilearn.github.io/manipulating_images/input_output.html 4D input image modules: ndarray axes : matplotlib axes, optional The axes used to display the plot. If None, the complete figure is used. title : string, optional The title displayed on the figure. output_file : string, or None, optional The name of an image file to export the plot to. Valid extensions are .png, .pdf, .svg. If output_file is not None, the plot is saved to a file, and the display is closed. legend : bool Whether to render the average functional series with ``atlaslabels`` as overlay. """ import numpy as np import nibabel as nb import pandas as pd import os import matplotlib.pyplot as plt from matplotlib import gridspec as mgs import matplotlib.cm as cm from matplotlib.colors import ListedColormap import PUMI.utils.globals as glb from nilearn.plotting import plot_img legend = False if atlas: legend = True # actually load data timeseries = pd.read_csv(timeseries, sep="\t") #normalise all timeseries v = (None, None) timeseries = (timeseries - timeseries.mean()) / timeseries.std() v = (-2, 2) timeseries = timeseries.transpose() minimum = np.min(timeseries) maximum = np.max(timeseries) myrange = maximum - minimum modules = pd.Series(modules).values lut = pd.factorize(modules)[0] + 1 # If subplot is not defined if subplot is None: subplot = mgs.GridSpec(1, 1)[0] # Define nested GridSpec wratios = [2, 120, 20] gs = mgs.GridSpecFromSubplotSpec(1, 2 + int(legend), subplot_spec=subplot, width_ratios=wratios[:2 + int(legend)], wspace=0.0) mycolors = ListedColormap(cm.get_cmap('Set1').colors[:7][::-1]) # Segmentation colorbar ax0 = plt.subplot(gs[0]) ax0.set_yticks([]) ax0.set_xticks([]) lutt = pd.DataFrame({'1': lut}) ax0.imshow(lutt, interpolation='none', aspect='auto', cmap=mycolors, vmin=0, vmax=8) ax0.grid(False) ax0.spines["left"].set_visible(False) ax0.spines["bottom"].set_color('none') ax0.spines["bottom"].set_visible(False) # Carpet plot ax1 = plt.subplot(gs[1]) ax1.imshow(timeseries, interpolation='nearest', aspect='auto', cmap='gray', vmin=v[0], vmax=v[1]) ax1.grid(False) ax1.set_yticks([]) ax1.set_yticklabels([]) # Set 10 frame markers in X axis interval = max((int(timeseries.shape[-1] + 1) // 10, int(timeseries.shape[-1] + 1) // 5, 1)) xticks = list(range(0, timeseries.shape[-1])[::interval]) ax1.set_xticks(xticks) ax1.set_xlabel('time') #ax1.set_xticklabels(['%.02f' % t for t in labels.tolist()], fontsize=5) # Remove and redefine spines for side in ["top", "right"]: # Toggle the spine objects ax0.spines[side].set_color('none') ax0.spines[side].set_visible(False) ax1.spines[side].set_color('none') ax1.spines[side].set_visible(False) ax1.yaxis.set_ticks_position('left') ax1.xaxis.set_ticks_position('bottom') ax1.spines["bottom"].set_visible(False) ax1.spines["left"].set_color('none') ax1.spines["left"].set_visible(False) if legend: gslegend = mgs.GridSpecFromSubplotSpec(5, 1, subplot_spec=gs[2], wspace=0.0, hspace=0.0) if not background_file: background_file = atlas #glb._FSLDIR_ + "/data/standard/MNI152_T1_2mm_brain.nii.gz" #TODO: works only for 3mm atlas background = nb.load(background_file) atlas = nb.load(atlas) nslices = background.shape[-1] #coords = np.linspace(int(0 * nslices), int(0.99 * nslices), 5).astype(np.uint8) coords = [-40, 20, 0, 20, 40] #works in MNI space lut2 = lut lut2 = np.array([0] + lut2.tolist()) relabeled = lut2[np.array(atlas.get_data(), dtype=int)] atl = nb.Nifti1Image(relabeled, atlas.get_affine()) for i, c in enumerate(coords): ax2 = plt.subplot(gslegend[i]) plot_img(atl, bg_img=background, axes=ax2, display_mode='z', annotate=False, cut_coords=[c], threshold=0.1, cmap=mycolors, interpolation='nearest', vmin=1, vmax=7) if output_file is not None: figure = plt.gcf() figure.savefig(output_file, bbox_inches='tight') plt.close(figure) figure = None return os.getcwd() + '/' + output_file return [ax0, ax1], gs
plot_atoms(ax, atoms, [0,2,1], colors, alp, z=-1) #-----------------------------------------------------------# fig = plt.figure(figsize=(13.0,10.5)) outer = gridspec.GridSpec(4, 9, wspace=0.04, hspace=0.2) color_lib = ['#00FF00','#377eb8','#4daf4a','#00FFFF','#a65628','#FF0000','#0000FF', '#FF00FF','#FFFF00','#000000'] #---------------------- Pt7 clusters -------------------------------------# data=read(sys.argv[1]+'@:') energydif =np.zeros(len(data)) for j in range(len(data)): GM_energy = data[0].get_potential_energy() energydif[j] = (data[j].get_potential_energy() - GM_energy) for j in range(0,len(data)): inner = gridspec.GridSpecFromSubplotSpec(2, 1,subplot_spec=outer[j], wspace=0.00, hspace=0.0, height_ratios=[6.86,9.9]) atoms = data[j] colorlenth = len(atoms) atoms =atoms*(3,3,1) print(colorlenth) # write('newimage.traj',atoms) a=atoms del atoms[[atom.index for atom in atoms if atom.index <=colorlenth*5-10 or atom.index >=colorlenth*5]] #view(atoms) centreofmass = a.get_center_of_mass() atoms = data[j]*(3,3,1) a=atoms del atoms[atoms.positions[:,0] >=centreofmass[0]+8.10] del atoms[atoms.positions[:,0] <= centreofmass[0]-8.10] del atoms[atoms.positions[:,1] >= centreofmass[1]+7.8] del atoms[atoms.positions[:,1] <= centreofmass[1]-7.10]
if(rbuffer.full and samplesInBuffer>=sendEverySmpl): data_raw = np.array(rbuffer.get())[:,0:3] data_filtered = filterbank.process(data_raw) data_batch = specGen.process(data_filtered) outlet.push_sample(data_batch.flatten()) samplesSent += 1 sys.stdout.write('\rsamples sent: %i' %samplesSent) # \r requires stdout to work samplesInBuffer = 0 if verbose: if samplesSent < 2: inner = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=outer[0], wspace=0, hspace=0.05) ax11 = plt.Subplot(fig,inner[0]) h11, = ax11.plot(data_raw[:,0]) fig.add_subplot(ax11) ax12 = plt.Subplot(fig,inner[1]) h12, = ax12.plot(data_raw[:,1]) fig.add_subplot(ax12, sharex=ax11) ax13 = plt.Subplot(fig,inner[2]) h13, = ax13.plot(data_raw[:,2]) fig.add_subplot(ax13, sharex=ax11) inner = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=outer[1], wspace=0, hspace=0.05) ax21 = plt.Subplot(fig,inner[0]) h21, = ax21.plot(data_filtered[:,0]) fig.add_subplot(ax21) ax22 = plt.Subplot(fig,inner[1]) h22, = ax22.plot(data_filtered[:,1])
True, { "orientation": "horizontal", 'label': 'Coupling strength (a.u.)' }, ax_cb ] else: cbar_info = [ False, { "orientation": "horizontal", 'label': 'Coupling strength (a.u.)' }, ax_cb ] inner = gridspec.GridSpecFromSubplotSpec(1, 3, subplot_spec=outer[i], wspace=0.8, hspace=0.8) a = a_all[i] b = b_all[i] K = K_all[i] a_ax = plt.Subplot(fig, inner[0]) vis_heatmap(a, vmin, vmax, a_ax, np.array(['\n $a_{ij}$', 'osci. $j$', 'osci. $i$']), cbar_info, linewidths=0.0)
fig = plt.figure(figsize=(8.27, 12 / 3), dpi=100) gs = gridspec.GridSpec(2, 2, width_ratios=[30, 1]) gs.update(left=0.08, right=0.92, bottom=0.05, top=0.94, wspace=0.05, hspace=0.3) h = 0.08 w = 0.15 gs00 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[0], hspace=h, wspace=w, height_ratios=[3, 2]) gs00_cb = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[1], height_ratios=[3, 2]) gs01 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[2], hspace=h, wspace=w, height_ratios=[3, 2]) gs01_cb = gridspec.GridSpecFromSubplotSpec(2, 1,
def plot_data_model(self, t, tbin, df_data, df_model_params, df_ALS_params, delta_xtick=20.0, conc_units=False, save_fn=None, print_cost=True): ''' Method for plotting the scaled model overlaid on the inputted species data. (No fit is performed.) Residuals are also plotted and the value of the cost function with these parameters is optionally outputted. Only species for which fit is True in df_data are shown and included in the cost calculation. See ex_notebook_1.ipynb for API documentation. ''' # Run the model t_start = float(t.min()) t_end = float(t.max()) t_model, c_model = self._model(t_start, t_end, tbin, df_model_params['val'].to_dict(), df_ALS_params['val'].to_dict()) # Only plot the species for which fit=True # Columns of data_val and data_err are species and the rows correspond to times in t array data_fit = df_data[df_data['fit']] species_names = list(data_fit.index) nSpecies = len(species_names) data_val = pd.DataFrame(list(data_fit['val']), index=species_names).T if self._err_weight: data_err = pd.DataFrame(list(data_fit['err']), index=species_names).T # Setup for residual and cost computations cost = 0 idx_cost = np.full(t.shape, True) if self._fit_pre_photo else ( t >= df_ALS_params.at['t0', 'val'] ) # Boolean array of indices to use in cost calculation idx_model = [ np.abs(t_model - t[_]).argmin() for _ in range(t.size) ] # Array of positional indices, maps model axis --> data axis # Set up the grid of subplots ncols = 3 nrows = (nSpecies // ncols) if (nSpecies % ncols) == 0 else (nSpecies // ncols) + 1 dpi = 120 plt.rc('font', size=9) plt.rc('axes.formatter', useoffset=False) f = plt.figure(figsize=(1000 / dpi, 450 * nrows / dpi), dpi=dpi) gs = gridspec.GridSpec(nrows, ncols, figure=f, hspace=0.3, wspace=0.3, top=0.9) # Determine x-axis ticks tick_low = (t_start // delta_xtick) * delta_xtick tick_high = t_end if t_end % delta_xtick == 0. else ( (t_end // delta_xtick) + 1) * delta_xtick ticks = np.linspace(tick_low, tick_high, num=round(((tick_high - tick_low) / delta_xtick) + 1), endpoint=True) # Make the subplots c_data = [] s_model = [] for i, species in enumerate(species_names): obs = data_val[species] mod = df_ALS_params.at['S_' + species, 'val'] * c_model[species] s_model.append(mod) c_obs = data_val[species] / df_ALS_params.at['S_' + species, 'val'] c_mod = c_model[species] c_data.append(c_obs) # Compute this species' residual and cost contribution # Important to perform .values conversion to array BEFORE we subtract obs and mod (pandas subtracts Series by index agreement not position) res = obs.values - mod.values[idx_model] cost_i = np.sqrt(data_fit.at[species, 'weight']) * res[idx_cost] if self._err_weight: err = data_err[species] cost_i = cost_i / err.values[idx_cost] cost += (cost_i**2).sum() c_res = c_obs.values - c_mod.values[idx_model] j = i // 3 # Row index k = i % 3 # Col index gs_jk = gridspec.GridSpecFromSubplotSpec(2, 1, hspace=0, height_ratios=[3, 1], subplot_spec=gs[j, k]) ax0 = plt.subplot(gs_jk[0]) # Data & Model ax1 = plt.subplot(gs_jk[1]) # Data - Model if conc_units: ax0.ticklabel_format(axis='y', style='sci', scilimits=(0, 0)) ax0.plot(t, c_obs, 'o') # Plot the data ax0.plot(t_model, c_mod, linewidth=2) # Plot the model # OOM calculation adapted from matplotlib.ticker.ScalarFormatter._set_orderOfMagnitude source code oom = np.floor(np.log10(ax0.get_yticks().max())) ax1.ticklabel_format(axis='y', style='plain') ax1.plot(t, c_res / (10**oom), 'o') # Plot residual else: ax0.plot(t, obs, 'o') # Plot the data ax0.plot(t_model, mod, linewidth=2) # Plot the model ax1.plot(t, res, 'o') # Plot residual ax1.plot(t_model, np.zeros(t_model.shape)) # Plot zero residual line # Manually set x-axis ticks ax0.set_xticks(ticks) ax1.set_xticks(ticks) # Labels ax0.set_title(species, fontweight='bold') # Make the title the species name ax0.set_xticklabels([]) # Hide x-axis tick labels for top plot ax1.set_xlabel('Time (ms)') # Set x-axis label for bottom plot if k == 0: # Set y-axis labels if plot is in first column ax0.set_ylabel('Data & Model') ax1.set_ylabel('Data - Model') plt.show() # Print the value of the cost function if print_cost: print() print('Cost Function Value = {:g}'.format(cost)) # Save the scaled model traces if save_fn: append_data = (lambda _: _ + '-data') append_model = (lambda _: _ + '-model') if conc_units: df_data_out = pd.DataFrame(c_data).T.rename( columns=append_data) df_model_out = c_model.rename(columns=append_model) else: df_data_out = data_val.rename(columns=append_data) df_model_out = pd.DataFrame(s_model).T.rename( columns=append_model) df_data_out.insert(0, 't-data', t) df_model_out.insert(0, 't-model', t_model) df = pd.concat((df_data_out, df_model_out), axis='columns') df.to_csv(save_fn, index=False)
def __init__(self): with open("transmission_peaks.pkl", "rb") as f: exp_freqs, trans_data, fit_data = pickle.load(f) with open("fig3a.pkl", "rb") as f: exp_data = pickle.load(f) with open("5q-res-converted.pkl", "rb") as f: sim_data = pickle.load(f) with open("chain_spectrum.pkl", "rb") as f: evals = pickle.load(f) fig = plt.figure() meta_spec = fig.add_gridspec(ncols=4, nrows=1, wspace=.5) spec = gridspec.GridSpecFromSubplotSpec(1, 3, subplot_spec=meta_spec[0, 0:3]) oneDmetaspec = gridspec.GridSpecFromSubplotSpec(1, 2, subplot_spec=spec[0, 0:1]) # Plot the transmission peaks in the linear regime ax = fig.add_subplot(oneDmetaspec[0, 0]) ax.plot(real(trans_data), exp_freqs, ",", color="gray", alpha=0.3) ax.plot(real(fit_data), exp_freqs, "-", color="black", lw=1) ax.yaxis.set_major_locator(MaxNLocator(5)) ax.set_ylabel("$\omega_d/2\pi$ (GHz)") ax.set_xlabel("Re $S_{21}$") ax.set_ylim(3.8, 4) ax.set_title("A - A", position=(1.1, 1)) plt.text(-.9, 1.05, "(a)", fontdict={"name": "STIX"}, fontsize=15, transform=ax.transAxes) ax = fig.add_subplot(oneDmetaspec[0, 1]) ax.plot(imag(trans_data), exp_freqs, ",", color="gray", alpha=0.3) ax.plot(imag(fit_data), exp_freqs, "-", color="black", lw=1) ax.yaxis.set_major_locator(MaxNLocator(5)) ax.set_yticklabels([]) ax.set_xlabel("Im $S_{21}$") ax.set_ylim(3.8, 4) # Plot the power scan theory and experimental data exp_data["data"] *= exp(2j * pi * 50.5e-9 * exp_data["Frequency [Hz]"] + 0.7j * pi) ax = fig.add_subplot(spec[0, 1]) X = (10**(exp_data["Power [dBm]"] / 10)) * 1000 Y = exp_data["Frequency [Hz]"] mappable = ax.pcolormesh(X, Y / 1e9, 10 * log10(abs(exp_data["data"].T)) + 10, rasterized=True, cmap="RdBu_r", vmin=-50) ax.set_xticklabels([]) ax.set_xscale("log") ax.set_xlim((1 * 1e-3 / 2.1)**2 * 1e3, (100 * 1e-3 / 2.1)**2 * 1e3) ax.xaxis.set_major_locator(LogLocator(base=10, numticks=15)) ax.xaxis.set_minor_locator( LogLocator(base=10, subs=linspace(0.2, 0.9, 7), numticks=15)) ax.set_xlabel(r"VNA power (mW)") ax.set_yticklabels([]) ax.annotate("A", xy=(.3e-3, 3.825), xytext=(.3e-3, 3.805), ha="left", fontsize=10, arrowprops=dict(facecolor='black', width=.5, headwidth=3, headlength=3.5, shrink=0.05)) ax.annotate("A", xy=(.3e-3, 3.975), xytext=(.3e-3, 3.99), ha="left", fontsize=10, arrowprops=dict(facecolor='black', width=.5, headwidth=3, headlength=3.5, shrink=0.05)) plt.text(-.2, 1.05, "(b)", fontdict={"name": "STIX"}, fontsize=15, transform=ax.transAxes) ax = fig.add_subplot(spec[0, 2]) X = sim_data[0] Y = sim_data[1] # print(X) mappable = ax.pcolormesh(X * 1e3, Y, 10 * log10(abs(sim_data[2].T)), rasterized=True, cmap="RdBu_r", vmax=0, vmin=-50) ax.set_yticklabels([]) ax.set_xscale("log") ax.set_xlim(1, 100) ax.set_xlabel(r"$\Omega$ (MHz)") cbaxes2 = fig.add_axes([0.4, .95, 0.15, .01]) cb = plt.colorbar(mappable, ax=ax, cax=cbaxes2, orientation="horizontal") cb.ax.xaxis.set_major_locator(MaxNLocator(5)) cb.ax.tick_params(axis='both', which='minor', labelsize=7) cbaxes2.set_title(r"$|S_{21}|$ (dB)", position=(1.3, 0), y=-2, transform=cbaxes2.transAxes, fontsize=10) plt.text(1.05, 1.05, "(c)", fontdict={"name": "STIX"}, fontsize=15, transform=ax.transAxes) X = [1.25] lw = 1 ms = 3 for E in evals[-1][1:6]: ax.plot(X, ones_like(X) * E, marker="_", color="C0", lw=lw, ms=ms) for E in evals[-1][6:11]: ax.plot(X, ones_like(X) * E / 2, marker="_", color="C1", lw=lw, ms=ms) X = [4] for E in evals[-1][11:21]: ax.plot(X, ones_like(X) * E / 2, marker="_", color="C2", lw=lw, ms=ms) X = [12.5] for E in evals[-1][26:56]: ax.plot(X, ones_like(X) * E / 3, marker="_", color="C4", lw=lw, ms=ms) X = [40] for E in evals[-1][61:126]: ax.plot(X, ones_like(X) * E / 4, marker="_", color="C5", lw=lw, ms=ms) ax.set_ylim(3.8, 4) ax.annotate("$E_1$", (1.35, evals[-1][1] + 1e-3), (1.6, 3.84), arrowprops=dict(facecolor='black', width=.25, headwidth=3, headlength=3.5, shrink=0.0), fontsize=10) ax.annotate("$E_{41}$", (11, evals[-1][46] / 3), (5, evals[-1][46] / 3), arrowprops=dict(facecolor='black', width=.25, headwidth=3, headlength=3.5, shrink=0.0), fontsize=10) ax.annotate("$E_{119}$", (41, evals[-1][124] / 4 + 1e-3), (50, evals[-1][124] / 4 + 10e-3), arrowprops=dict(facecolor='black', width=.25, headwidth=3, headlength=3.5, shrink=0.0), fontsize=10) ax.annotate("$E_{90}$", (41, evals[-1][95] / 4 + 1e-3), (50, evals[-1][95] / 4 - 10e-3), arrowprops=dict(facecolor='black', width=.25, headwidth=3, headlength=3.5, shrink=0.0), fontsize=10) # ax.annotate("$E_{21}$", (3.9, evals[-1][46]/2-1e-3), (3.5, evals[-1][46]/2 - 15e-3),ha="right", arrowprops=dict(facecolor='black', width=.25, headwidth=3, headlength=3.5, # shrink=0.0), fontsize = 10) ##### Panel (c) eval_intervals = [ slice(1, 6), slice(6, 21), slice(21, 56), slice(56 + 5, 126), # slice(126+5, 247) ] minizones = 0 linewidth = .5 spec = gridspec.GridSpecFromSubplotSpec(4, 1, subplot_spec=meta_spec[0, 3]) axes = [fig.add_subplot(spec[3 - idx, 0]) for idx in range(4)] for idx, ax in enumerate(axes): ax.yaxis.set_major_locator(MaxNLocator(2)) data = list( zip( array(evals[-1])[eval_intervals[idx]], array(evals[0])[eval_intervals[idx]])) if idx not in [1, 2]: for level, level_unpert in data: ax.plot([.525, .95], ones(2) * level, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) ax.plot([0.05, .475], ones(2) * level_unpert, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) else: for level, level_unpert in data[:5]: ax.plot([0.05, .475], ones(2) * level_unpert, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) ax.plot([.525, .95], ones(2) * level, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) minizones += 1 for level, level_unpert in data[5:]: ax.plot([0.05, .475], ones(2) * level_unpert, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) ax.plot([.525, 0.95], ones(2) * level, lw=linewidth, color="C%d" % (idx + minizones), alpha=0.5) d = 0.025 if idx > 0: kwargs = dict(transform=ax.transAxes, color='k', clip_on=False) ax.plot((-d, +d), (-d, +d), lw=1, **kwargs) # top-left diagonal ax.plot((1 - d, 1 + d), (-d, +d), lw=1, **kwargs) # top-right diagonal ax.spines['bottom'].set_visible(False) ax.set_xticks([]) if idx < 3: ax.spines["top"].set_visible(False) kwargs = dict(transform=ax.transAxes, color='k', clip_on=False) ax.plot((-d, +d), (1 - d, 1 + d), lw=1, **kwargs) # top-left diagonal ax.plot((1 - d, 1 + d), (1 - d, 1 + d), lw=1, **kwargs) # top-right diagonal def plot_single_circle(ax, xposition, yposition, color, yscale): step = ptp(cartoons_x) / 5 ax.plot([cartoons_x[0] + xposition * step + step / 2], [yposition], color=color, marker="o", ms=5, mec="black", mew=0.5) def plot_double_circle(ax, xposition, yposition, color, yscale): plot_single_circle(ax, xposition - 0.2, yposition - 0.01 * yscale, color, yscale) plot_single_circle(ax, xposition + 0.2, yposition + 0.01 * yscale, color, yscale) def plot_triple_circle(ax, xposition, yposition, color, yscale): plot_double_circle(ax, xposition, yposition, color, yscale) plot_single_circle(ax, xposition - 0.1, yposition + 0.05 * yscale, color, yscale) cartoon_position_level_indices = [[1], [6, 11], [21 + 2, 26, 46], [61, 81, 91, 121]] cartoons_x = linspace(.05, .475, 100) shifts = [-2, -2, 2, -2] single_circle_xpositions = [[[2]], [[], [1, 3]], [[], [2], [1, 3, 4]], [[2], [], [2, 4], [0, 1, 3, 4]]] double_circle_xpositions = [[[]], [[2], []], [[], [0], []], [[], [1, 3], [1], []]] triple_circle_xpositions = [[[]], [[], []], [[2], [], []], [[1], [], [], []]] colors = ["C0", "C1", "C2", "C3"] + ["C4"] * 2 + ["C5"] * 4 color_counter = 0 for idx, ax in enumerate(axes): for idx2, level_index in enumerate( cartoon_position_level_indices[idx]): wells = array(evals[0])[level_index] + \ (cos(linspace(0, 2 * pi * 5, 100)) + shifts[idx]) * .5e-1 * ptp( ax.get_ylim()) ax.plot(cartoons_x, wells, color="grey", lw=1) for xposition in single_circle_xpositions[idx][idx2]: plot_single_circle(ax, xposition, wells[2], colors[color_counter], ptp(ax.get_ylim())) for xposition in double_circle_xpositions[idx][idx2]: plot_double_circle(ax, xposition, wells[2], colors[color_counter], ptp(ax.get_ylim())) for xposition in triple_circle_xpositions[idx][idx2]: plot_triple_circle(ax, xposition, wells[2], colors[color_counter], ptp(ax.get_ylim())) color_counter += 1 axes[0].set_xticks([0.05 + 0.425 / 2, .75]) axes[0].set_xticklabels(["0", "40"]) axes[0].set_xlabel("$J/2\pi$ (MHz)") axes[2].set_ylabel("$E_n/h$ (GHz)") axes[2].yaxis.set_label_coords(-.3, -0.25) plt.gcf().set_size_inches(11, 4) plt.savefig("../fig3.pdf", bbox_inches="tight")
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 29 14:24:44 2018 @author: ppxee """ import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec fig = plt.figure(figsize=(10, 8)) outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2) for i in range(4): inner = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=outer[i], wspace=0.1, hspace=0.1) for j in range(2): ax = plt.Subplot(fig, inner[j]) t = ax.text(0.5, 0.5, 'outer=%d, inner=%d' % (i, j)) t.set_ha('center') ax.set_xticks([]) ax.set_yticks([]) fig.add_subplot(ax) fig.show()
smooths = [col for col in smooth_fit.reshape((-1, len(ks)))] print(len(mods), len(smooths), len(errs)) titles = [f"pk{n}" for n in model.data[0]["fit_poles"]] ax1 = fig1.add_subplot(axes1[counter]) axes = fig2.add_subplot(axes2[counter]) axes.spines["top"].set_color("none") axes.spines["bottom"].set_color("none") axes.spines["left"].set_color("none") axes.spines["right"].set_color("none") axes.tick_params(axis="both", which="both", labelcolor="none", top=False, bottom=False, left=False, right=False) mfcs = ["#666666", "w"] lines = ["-", "--"] inner = gridspec.GridSpecFromSubplotSpec(1, len(titles), subplot_spec=axes2[counter], wspace=0.08) for i, (inn, err, mod, smooth, title, line, mfc) in enumerate(zip(inner, errs, mods, smooths, titles, lines, mfcs)): ax1.errorbar(ks, ks * data[0][title], yerr=ks * err, fmt="o", ms=4, c="#666666", mfc=mfc) ax1.plot(ks, ks * mod, c=extra["color"], ls=line) if counter != (len(names) - 1): ax1.tick_params(axis="x", which="both", labelcolor="none", bottom=False, labelbottom=False) ax1.annotate(extra["name"], xy=(0.98, 0.95), xycoords="axes fraction", ha="right", va="top") ax2 = fig2.add_subplot(inn) ax2.errorbar(ks, ks ** 2 * (data[0][title] - smooth), yerr=ks ** 2 * err, fmt="o", ms=4, c="#666666") ax2.plot(ks, ks ** 2 * (mod - smooth), c=extra["color"]) ax2.set_ylim(-4.0, 4.0) if counter == 0: if i == 0: ax2.set_title(r"$P_{0}(k)$")
def plotReportNoTraces(figName, timeStep, outputU, outputRho, target, data, figSize, wCurrent, eligs, signDeltaW, simTime=None): """ Function to plot the report of one iteration """ # make the figure and the axes grid plt.rcParams["font.family"] = "serif" width = 12. ratio = 0.7 fig = plt.figure(figsize=(width, ratio * width)) gs_main = gs.GridSpec(1, 1, height_ratios=[1], left=0.07, right=0.93, top=.97, bottom=0.07) gs_lower = gs.GridSpecFromSubplotSpec(2, 3, gs_main[0, 0], wspace=0.38, hspace=.15, width_ratios=[1, 1, 1]) axOutputRaw = plt.Subplot(fig, gs_lower[0, 0]) fig.add_subplot(axOutputRaw) axData = plt.Subplot(fig, gs_lower[0, 1]) fig.add_subplot(axData) axCurrentW = plt.Subplot(fig, gs_lower[0, 2]) fig.add_subplot(axCurrentW) axOutputRho = plt.Subplot(fig, gs_lower[1, 0]) fig.add_subplot(axOutputRho) axDeltaW = plt.Subplot(fig, gs_lower[1, 1]) fig.add_subplot(axDeltaW) axDeltaWSign = plt.Subplot(fig, gs_lower[1, 2]) fig.add_subplot(axDeltaWSign) # bar plots of the membrane potentials nOutput = len(outputU) width = 0.7 xPos = np.arange(1, nOutput + 1) make_spines(axOutputRaw) axOutputRaw.bar(xPos, outputU, width=width) axOutputRaw.set_xlabel('output neurons') axOutputRaw.set_ylabel('memb. pot. [a.u.]') axOutputRaw.set_xticks(np.arange(1, nOutput + 1)) # bar plots of the membrane potentials nOutput = len(outputRho) width = 0.7 xPos = np.arange(1, nOutput + 1) make_spines(axOutputRho) axOutputRho.bar(xPos, outputRho, width=width, zorder=1) axOutputRho.set_xlabel('output neurons') axOutputRho.set_ylabel('activity') axOutputRho.set_xticks(np.arange(1, nOutput + 1)) # Add target marker to the plot h = np.max(outputRho) / 2. axOutputRho.scatter(target, h, marker='x', s=100, zorder=2) hwinner = np.max(outputRho) * .75 axOutputRho.scatter(np.argmax(outputRho) + 1, hwinner, marker='D', s=100, zorder=2) # print the data im = np.reshape(data, figSize) show_axis(axData) axData.imshow(im, cmap='bwr', aspect=1., interpolation='nearest') axData.set_xticks([], []) axData.set_yticks([], []) axData.set_title('presented data') # plot the current weights show_axis(axCurrentW) maxAbs = np.max(np.abs(wCurrent)) imAx = axCurrentW.imshow(wCurrent, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1. * maxAbs, vmax=maxAbs) cax = inset_axes( axCurrentW, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axCurrentW.transAxes, borderpad=0, ) cbar = colorbar(imAx, cax=cax) axCurrentW.set_ylabel('input') axCurrentW.set_xlabel('neurons') axCurrentW.set_title('current weights') axCurrentW.set_xticks(np.arange(0, nOutput)) # plot the final eligibility traces show_axis(axDeltaW) maxAbs = np.max(np.abs(eligs)) imAx = axDeltaW.imshow(eligs, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1. * maxAbs, vmax=maxAbs) cax = inset_axes( axDeltaW, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axDeltaW.transAxes, borderpad=0, ) cbar = colorbar(imAx, cax=cax) axDeltaW.set_ylabel('input') axDeltaW.set_title('final elig.') axDeltaW.set_xlabel('neurons') axDeltaW.set_xticks(np.arange(0, nOutput)) # plot the sign of the appplied weight chages show_axis(axDeltaWSign) imAxSign = axDeltaWSign.imshow(signDeltaW, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1., vmax=1.) caxSign = inset_axes( axDeltaWSign, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axDeltaWSign.transAxes, borderpad=0, ) cbar = colorbar(imAxSign, cax=caxSign) axDeltaWSign.set_ylabel('input') axDeltaWSign.set_title(r'$\mathrm{sign} (\Delta W)$') axDeltaWSign.set_xlabel('neurons') axDeltaWSign.set_xticks(np.arange(0, nOutput)) fig.savefig(figName, dpi=200) plt.close(fig)
curr_trace, _ = dt.get_single_trace(nwb_f=nwb_f, plane_n=plane_n, roi_n=roi_n) if np.min(curr_trace) < bias: add_to_trace = -np.min(curr_trace) + bias else: add_to_trace = 0. f = plt.figure(figsize=(8.5, 11)) gs_out = gridspec.GridSpec(len(tf_lst), 1) gs_in_dict = {} for gs_ind, gs_o in enumerate(gs_out): curr_gs_in = gridspec.GridSpecFromSubplotSpec(len(sf_lst), len(dire_lst), subplot_spec=gs_o, wspace=0.05, hspace=0.05) gs_in_dict[gs_ind] = curr_gs_in v_max = 0 v_min = 0 dff_mean_max = 0 dff_mean_min = 0 for grating_n in grating_ns: grating_grp = res_grp[grating_n] curr_sta = grating_grp['sta_' + trace_type].value[roi_i] + add_to_trace _ = get_dff(traces=curr_sta, t_axis=t_axis, response_span=response_span,
def plotReport(figName, timeStep, traces, outputU, outputRho, target, data, figSize, wCurrent, eligs, signDeltaW, error, errorHidden, simTime=None): """ Function to plot the report of one iteration """ # make the figure and the axes grid plt.rcParams["font.family"] = "serif" width = 12. ratio = 0.75 fig = plt.figure(figsize=(width, ratio * width)) gs_main = gs.GridSpec(2, 1, hspace=0.2, height_ratios=[1, 2], left=0.07, right=0.93, top=.97, bottom=0.07) gs_upper = gs.GridSpecFromSubplotSpec(1, 2, gs_main[0, 0], wspace=0.2, width_ratios=[1, 1]) gs_lower = gs.GridSpecFromSubplotSpec(3, 3, gs_main[1, 0], wspace=0.2, hspace=.35, width_ratios=[1, 1, 1]) axMemb = plt.Subplot(fig, gs_upper[0]) fig.add_subplot(axMemb) axElig = plt.Subplot(fig, gs_upper[1]) fig.add_subplot(axElig) axOutputRaw = plt.Subplot(fig, gs_lower[0, 0]) fig.add_subplot(axOutputRaw) axData = plt.Subplot(fig, gs_lower[0, 1]) fig.add_subplot(axData) axCurrentW = plt.Subplot(fig, gs_lower[0, 2]) fig.add_subplot(axCurrentW) axOutputRho = plt.Subplot(fig, gs_lower[1, 0]) fig.add_subplot(axOutputRho) axDeltaW = plt.Subplot(fig, gs_lower[1, 1]) fig.add_subplot(axDeltaW) axDeltaWSign = plt.Subplot(fig, gs_lower[1, 2]) fig.add_subplot(axDeltaWSign) axCurrentError = plt.Subplot(fig, gs_lower[2, 0]) fig.add_subplot(axCurrentError) axCurrentErrorHidden = plt.Subplot(fig, gs_lower[2, 1]) fig.add_subplot(axCurrentErrorHidden) if not (simTime is None): lastN = int(simTime / timeStep) traces['uMem'] = traces['uMem'][-lastN:, :] traces['eligibilities'] = traces['eligibilities'][-lastN:, :] # make a timearray timeArray = np.arange(len(traces['uMem'][:, 0])) * timeStep # Plot the membrane potentials uMems = traces['uMem'] make_spines(axMemb) axMemb.plot(timeArray, uMems) axMemb.set_xlabel(r'time $[ms]$') axMemb.set_ylabel('memb. pot. [a.u.]') # Plot the eligibility traces uElig = traces['eligibilities'] make_spines(axElig) axElig.plot(timeArray, uElig[:, :10]) axElig.set_xlabel(r'time $[ms]$') axElig.set_ylabel('elig. traces [a.u.]') # bar plots of the membrane potentials nOutput = len(outputU) width = 0.7 xPos = np.arange(1, nOutput + 1) make_spines(axOutputRaw) axOutputRaw.bar(xPos, outputU, width=width) axOutputRaw.set_xlabel('output neurons') axOutputRaw.set_ylabel('memb. pot. [a.u.]') axOutputRaw.set_xticks(np.arange(1, nOutput + 1)) # bar plots of the membrane potentials nOutput = len(outputRho) width = 0.7 xPos = np.arange(1, nOutput + 1) make_spines(axOutputRho) axOutputRho.bar(xPos, outputRho, width=width, zorder=1) axOutputRho.set_xlabel('output neurons') axOutputRho.set_ylabel('activity') axOutputRho.set_xticks(np.arange(1, nOutput + 1)) # Add target marker to the plot h = np.max(outputRho) / 2. axOutputRho.scatter(target, h, marker='x', s=100, zorder=2) hwinner = np.max(outputRho) * .75 axOutputRho.scatter(np.argmax(outputRho) + 1, hwinner, marker='D', s=100, zorder=2) # print the data im = np.reshape(data, figSize) show_axis(axData) axData.imshow(im, cmap='bwr', aspect=1., interpolation='nearest') axData.set_xticks([], []) axData.set_yticks([], []) axData.set_title('presented data') # plot the current weights show_axis(axCurrentW) maxAbs = np.max(np.abs(wCurrent)) imAx = axCurrentW.imshow(wCurrent, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1. * maxAbs, vmax=maxAbs) cax = inset_axes( axCurrentW, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axCurrentW.transAxes, borderpad=0, ) cbar = colorbar(imAx, cax=cax) axCurrentW.set_ylabel('input') axCurrentW.set_xlabel('neurons') axCurrentW.set_title('current weights') axCurrentW.set_xticks(np.arange(0, nOutput)) # plot the final eligibility traces show_axis(axDeltaW) maxAbs = np.max(np.abs(eligs)) imAx = axDeltaW.imshow(eligs, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1. * maxAbs, vmax=maxAbs) cax = inset_axes( axDeltaW, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axDeltaW.transAxes, borderpad=0, ) cbar = colorbar(imAx, cax=cax) axDeltaW.set_ylabel('input') axDeltaW.set_title('final elig.') axDeltaW.set_xlabel('neurons') axDeltaW.set_xticks(np.arange(0, nOutput)) # plot the sign of the appplied weight chages show_axis(axDeltaWSign) maxAbs = np.max(np.abs(signDeltaW)) imAxSign = axDeltaWSign.imshow(signDeltaW, cmap='bwr', aspect=1, interpolation='nearest', vmin=-1. * maxAbs, vmax=maxAbs) caxSign = inset_axes( axDeltaWSign, width="5%", # width = 10% of parent_bbox width height="100%", # height : 50% loc=3, bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=axDeltaWSign.transAxes, borderpad=0, ) cbar = colorbar(imAxSign, cax=caxSign) axDeltaWSign.set_ylabel('input') axDeltaWSign.set_title(r'$\mathrm{sign} (\Delta W)$') axDeltaWSign.set_xlabel('neurons') axDeltaWSign.set_xticks(np.arange(0, nOutput)) # bar plot of the current error (nudging) on the neurons nOutput = len(error) width = 0.7 xPos = np.arange(1, nOutput + 1) make_spines(axCurrentError) axCurrentError.bar(xPos, error, width=width) axCurrentError.set_xlabel('output neurons') axCurrentError.set_ylabel('error [a.u.]') axCurrentError.set_xticks(np.arange(1, nOutput + 1)) axCurrentError.ticklabel_format(style='sci', axis='both', scilimits=(0, 0)) # bar plot of the current error (nudging) on the neurons nOutput = len(errorHidden) width = .95 xPos = np.arange(1, nOutput + 1) make_spines(axCurrentErrorHidden) axCurrentErrorHidden.bar(xPos, errorHidden, width=width) axCurrentErrorHidden.set_xlabel('hidden neurons') axCurrentErrorHidden.set_ylabel('error [a.u.]') axCurrentErrorHidden.set_xticks(np.arange(1, nOutput + 1)) axCurrentErrorHidden.ticklabel_format(style='sci', axis='both', scilimits=(0, 0)) fig.savefig(figName, dpi=150) plt.close(fig)
gsAll = gridspec.GridSpec(2, 2, left=0.00, right=1.0, top=0.95, bottom=0, hspace=0.08, wspace=0.08) gsGeno = [] for i in range(4): gsGeno.append( gridspec.GridSpecFromSubplotSpec(6, 6, subplot_spec=gsAll[i], hspace=0.01, wspace=0.01)) geno = -1 for i, s, w in zip(range(nworms), strains, wids): print i, s, w if i % 36 == 0: geno += 1 if geno != 3: ax = plt.Subplot(fig, gsGeno[geno][(i % 36) / 6, i % 6]) else: k = (i % 36) ax = plt.Subplot(fig, gsGeno[geno][k / 3, k % 3]) if i % 36 == 0:
def plot_survey_data_and_metadata(fig, S_data, plot_map=True, bus_timestamps=False, t1=None, t2=None, line_plots=[ 'Lshell', 'altitude', 'velocity', 'lat', 'lon', 'used_sats', 'solution_status', 'solution_type' ], show_plots=False, lshell_file='resources/Lshell_dict.pkl', cal_file=None, E_gain=False, B_gain=False): logger = logging.getLogger() if plot_map: from mpl_toolkits.basemap import Basemap from scipy.interpolate import interp1d, interp2d if plot_map or (len(line_plots) > 0): # The full plot: gs_root = GS.GridSpec(2, 2, height_ratios=[1, 2.5], width_ratios=[1, 1.5], wspace=0.2, hspace=0.1, figure=fig) gs_data = GS.GridSpecFromSubplotSpec(2, 2, width_ratios=[20, 1], wspace=0.05, hspace=0.05, subplot_spec=gs_root[:, 1]) m_ax = fig.add_subplot(gs_root[0, 0]) else: gs_data = GS.GridSpec(2, 2, width_ratios=[20, 1], wspace=0.05, hspace=0.05, figure=fig) # colormap -- parula is a clone of the Matlab colormap; also try plt.cm.jet or plt.cm.viridis cm = parula() #plt.cm.viridis; # Sort by header timestamps S_data = sorted(S_data, key=lambda f: f['header_timestamp']) # Subset of data with GPS stamps included. # We need these for the line plots, regardless if we're using payload or bus timestamps. # Also confirm that we have at least one field from BESTPOS and BESTVEL messages, # since on rare occasions we miss one or the other. S_with_GPS = list( filter( lambda x: (('GPS' in x) and ('timestamp' in x['GPS'][0]) and ('lat' in x['GPS'][0]) and ('horiz_speed' in x['GPS'][0])), S_data)) S_with_GPS = sorted(S_with_GPS, key=lambda f: f['GPS'][0]['timestamp']) logger.info(f'{len(S_with_GPS)} GPS packets') T_gps = np.array([x['GPS'][0]['timestamp'] for x in S_with_GPS]) dts_gps = np.array([ datetime.datetime.fromtimestamp(x, tz=datetime.timezone.utc) for x in T_gps ]) # Build arrays E = [] B = [] T = [] F = np.arange(512) * 40 / 512 # # Only plot survey data if we have GPS data to match if bus_timestamps: logger.info('Using bus timestamps') # Sort using bus timestamp (finer resolution, but # includes transmission error from payload to bus) for S in S_data: T.append(S['header_timestamp']) E.append(S['E_data']) B.append(S['B_data']) else: logger.info('using payload timestamps') # Sort using payload GPS timestamp (rounded to nearest second. # Ugh, why didn't we just save a local microsecond counter... do that on CANVAS please) for S in S_with_GPS: T.append(S['GPS'][0]['timestamp']) E.append(S['E_data']) B.append(S['B_data']) T = np.array(T) dates = np.array([datetime.datetime.utcfromtimestamp(t) for t in T]) if t1 is None: t1 = dates[0] if t2 is None: t2 = dates[-1] # ----------------------------------- # Spectrograms # ----------------------------------- E = np.array(E) B = np.array(B) T = np.array(T) logger.debug(f'E has shape {np.shape(E)}, B has shape {np.shape(B)}') # gs_data = GS.GridSpec(2, 2, width_ratios=[20, 1], wspace = 0.05, hspace = 0.05, subplot_spec=gs_root[1]) ax1 = fig.add_subplot(gs_data[0, 0]) ax2 = fig.add_subplot(gs_data[1, 0], sharex=ax1, sharey=ax1) e_cbax = fig.add_subplot(gs_data[0, 1]) b_cbax = fig.add_subplot(gs_data[1, 1]) e_clims = [50, 255] #[0,255] #[-80,-40] b_clims = [150, 255] #[0,255] #[-80,-40] date_edges = np.insert(dates, 0, dates[0] - datetime.timedelta(seconds=26)) # Insert columns of NaNs wherever we have gaps in data (dt > 27 sec) per_sec = 26 # Might want to look this up for the shorter survey modes gaps = np.where( np.diff(date_edges) > datetime.timedelta(seconds=(per_sec + 2)))[0] d_gapped = np.insert(dates, gaps, dates[gaps] - datetime.timedelta(seconds=per_sec + 3)) E_gapped = np.insert(E.astype('float'), gaps - 1, np.nan * np.ones([1, 512]), axis=0) B_gapped = np.insert(B.astype('float'), gaps - 1, np.nan * np.ones([1, 512]), axis=0) # Plot E data p1 = ax1.pcolormesh(d_gapped, F, E_gapped.T, vmin=e_clims[0], vmax=e_clims[1], shading='flat', cmap=cm) p2 = ax2.pcolormesh(d_gapped, F, B_gapped.T, vmin=b_clims[0], vmax=b_clims[1], shading='flat', cmap=cm) cb1 = fig.colorbar(p1, cax=e_cbax) cb2 = fig.colorbar(p2, cax=b_cbax) cb1.set_label(f'Raw value [{e_clims[0]}-{e_clims[1]}]') cb2.set_label(f'Raw value [{b_clims[0]}-{b_clims[1]}]') # # vertical lines at each edge (kinda nice, but messy for big plots) # g1 = ax1.vlines(dates, 0, 40, linewidth=0.2, alpha=0.5, color='w') # g2 = ax2.vlines(dates, 0, 40, linewidth=0.2, alpha=0.5, color='w') ax1.set_xticklabels([]) ax1.set_ylim([0, 40]) ax2.set_ylim([0, 40]) formatter = mdates.DateFormatter('%H:%M:%S') ax2.xaxis.set_major_formatter(formatter) fig.autofmt_xdate() ax2.set_xlabel( "Time (H:M:S) on \n%s" % datetime.datetime.utcfromtimestamp(T[0]).strftime("%Y-%m-%d")) # ax2.set_xlabel("Time (H:M:S)") ax1.set_ylabel('E channel\nFrequency [kHz]') ax2.set_ylabel('B channel\nFrequency [kHz]') # ----------------------------------- # Ground track Map # ----------------------------------- if plot_map: m = Basemap(projection='mill', lon_0=0, ax=m_ax, llcrnrlon=-180, llcrnrlat=-70, urcrnrlon=180, urcrnrlat=70) lats = [x['GPS'][0]['lat'] for x in S_with_GPS] lons = [x['GPS'][0]['lon'] for x in S_with_GPS] sx, sy = m(lons, lats) m.drawcoastlines(color='k', linewidth=1, ax=m_ax) m.drawparallels(np.arange(-90, 90, 30), labels=[1, 0, 0, 0]) m.drawmeridians(np.arange(m.lonmin, m.lonmax + 30, 60), labels=[0, 0, 1, 0]) m.drawmapboundary(fill_color='cyan') m.fillcontinents(color='white', lake_color='cyan') # This is sloppy -- we need to stash the scatterplot in a persistent object, # but because this is just a script and not a class, it vanishes. So we're # sticking it into the top figure for now. (This is so we can update the point # visibility when zooming in and out in the GUI) m_ax.s = m.scatter(sx, sy, c=T_gps, marker='.', s=10, cmap=get_cmap('plasma'), zorder=100, picker=5) hits = np.where(dates >= datetime.datetime(1979, 1, 1, 0, 0, 0)) # Enable click events on the map: def onpick(event): ''' Event handler for a point click ''' ind = event.ind t_center = dates[ind[0]] logger.info(f't = {t_center}') ax_lines[-1].set_xlim(t_center - datetime.timedelta(minutes=15), t_center + datetime.timedelta(minutes=15)) onzoom(ax1) fig.canvas.draw() def onzoom(axis, *args, **kwargs): # Update the map to only show points within range: [tt1, tt2] = axis.get_xlim() d1 = mdates.num2date(tt1) d2 = mdates.num2date(tt2) hits = np.where((dts_gps >= d1) & (dts_gps <= d2))[0] logger.debug(f'zoomed to {d1}, {d2} ({len(hits)} hits)') try: m_ax.s.remove() except: logger.debug('failed to remove scatter points') m_ax.s = m.scatter(np.array(sx)[hits], np.array(sy)[hits], c=T_gps[hits], marker='.', s=10, cmap=get_cmap('plasma'), zorder=100, picker=5) # Attach callback ax1.callbacks.connect('xlim_changed', onzoom) # ax2.callbacks.connect('xlim_changed', onzoom) cid = fig.canvas.mpl_connect('pick_event', lambda event: onpick(event)) # ----------------------------------- # Line plots # ----------------------------------- if len(line_plots) > 0: gs_lineplots = GS.GridSpecFromSubplotSpec(len(line_plots), 1, hspace=0.5, subplot_spec=gs_root[1, 0]) ax_lines = [] for ind, a in enumerate(line_plots): ax_lines.append(fig.add_subplot(gs_lineplots[ind])) markersize = 4 markerface = '.' markeralpha = 0.6 for ind, a in enumerate(line_plots): if a in S_with_GPS[0]['GPS'][0]: yvals = np.array([x['GPS'][0][a] for x in S_with_GPS]) ax_lines[ind].plot(dts_gps, yvals, markerface, markersize=markersize, label=a, alpha=markeralpha) ax_lines[ind].set_ylabel(a, rotation=0, labelpad=30) elif a in 'altitude': yvals = np.array([x['GPS'][0]['alt'] for x in S_with_GPS]) / 1000. ax_lines[ind].plot(dts_gps, yvals, markerface, markersize=markersize, label=a, alpha=markeralpha) ax_lines[ind].set_ylabel('Altitude\n[km]', rotation=0, labelpad=30) ax_lines[ind].set_ylim([450, 500]) elif a in 'dt': ax_lines[ind].plot(dts_gps, T - T_gps, markerface, markersize=markersize, label=a, alpha=markeralpha) ax_lines[ind].set_ylabel(r't$_{header}$ - t$_{GPS}$', rotation=0, labelpad=30) elif a in 'velocity': v_horiz = np.array( [x['GPS'][0]['horiz_speed'] for x in S_with_GPS]) v_vert = np.array( [x['GPS'][0]['vert_speed'] for x in S_with_GPS]) vel = np.sqrt(v_horiz * v_horiz + v_vert * v_vert) / 1000. ax_lines[ind].plot(dts_gps, vel, markerface, markersize=markersize, alpha=markeralpha, label='Velocity') ax_lines[ind].set_ylabel('Velocity\n[km/sec]', rotation=0, labelpad=30) ax_lines[ind].set_ylim([5, 10]) elif a in 'Lshell': try: # This way using a precomputed lookup table: with open(lshell_file, 'rb') as file: Ldict = pickle.load(file) L_interp = interp2d(Ldict['glon'], Ldict['glat'], Ldict['L'], kind='cubic') Lshell = np.array( [L_interp(x, y) for x, y in zip(lons, lats)]) ax_lines[ind].plot(dts_gps, Lshell, markerface, markersize=markersize, alpha=markeralpha, label='L shell') ax_lines[ind].set_ylabel('L shell', rotation=0, labelpad=30) ax_lines[ind].set_ylim([1, 8]) except: logger.warning(f'Missing {lshell_file}') elif a in 'daylight': # Day or night based on ground track, using the daynight terminator from Basemap dayvec = np.array( [is_day(x, y, z) for x, y, z in zip(dts_gps, lats, lons)]) ax_lines[ind].plot(dts_gps, dayvec, markerface, markersize=markersize, alpha=markeralpha, label='Day / Night') ax_lines[ind].set_yticks([False, True]) ax_lines[ind].set_yticklabels(['Night', 'Day']) fig.autofmt_xdate() for a in ax_lines[:-1]: a.set_xticklabels([]) # Link line plot x axes: for a in ax_lines: ax_lines[0].get_shared_x_axes().join(ax_lines[0], a) # Link data x axes: ax_lines[0].get_shared_x_axes().join(ax_lines[0], ax1) ax_lines[0].get_shared_x_axes().join(ax_lines[0], ax2) ax_lines[-1].set_xticklabels(ax_lines[-1].get_xticklabels(), rotation=30) ax_lines[-1].xaxis.set_major_formatter(formatter) ax_lines[-1].set_xlabel( "Time (H:M:S) on \n%s" % datetime.datetime.utcfromtimestamp(T[0]).strftime("%Y-%m-%d")) ax_lines[-1].set_xlim([t1, t2]) fig.subplots_adjust(left=0.1, right=0.95, top=0.9, bottom=0.12) # fig.suptitle(f"VPM Survey Data\n {dts[0].strftime('%D%, %H:%m:%S')} -- {dts[-1].strftime('%D%, %H:%m:%S')}") fig.suptitle( f"VPM Survey Data\n {t1.strftime('%D%, %H:%M:%S')} -- {t2.strftime('%D%, %H:%M:%S')}" ) return fig, p1, p2
def plot_heatmap(self): matrix_flatten = None if self.y_min is None: matrix_flatten = self.hm.matrix.flatten() # try to avoid outliers by using np.percentile self.y_min = np.percentile(matrix_flatten, 1.0) if np.isnan(self.y_min): self.y_min = None if self.y_max is None: if matrix_flatten is None: matrix_flatten = self.hm.matrix.flatten() # try to avoid outliers by using np.percentile self.y_max = np.percentile(matrix_flatten, 98.0) if np.isnan(self.y_max): self.y_max = None ax_list = [] # turn off y ticks for plot in range(self.numplots): labels = [] col = plot % self.plots_per_row row = int(plot / float(self.plots_per_row)) # split the ax to make room for the colorbar sub_grid = gridspec.GridSpecFromSubplotSpec(1, 2, subplot_spec=self.grids[row, col], width_ratios=[0.92, 0.08], wspace=0.05) ax = self.fig.add_subplot(sub_grid[0]) cax = self.fig.add_subplot(sub_grid[1]) ax.tick_params( axis='y', which='both', left='off', right='off', labelleft='on') if self.per_group: title = self.hm.matrix.group_labels[plot] else: title = self.hm.matrix.sample_labels[plot] ax.set_title(title) mat = [] # when drawing a heatmap (in contrast to drawing lines) for data_idx in range(self.numlines): if self.per_group: row, col = plot, data_idx else: row, col = data_idx, plot sub_matrix = self.hm.matrix.get_matrix(row, col) if self.per_group: label = sub_matrix['sample'] else: label = sub_matrix['group'] labels.append(label) mat.append(np.__getattribute__(self.averagetype)(sub_matrix['matrix'], axis=0)) img = ax.imshow(np.vstack(mat), interpolation='nearest', cmap='RdYlBu_r', aspect='auto', vmin=self.y_min, vmax=self.y_max) self.fig.colorbar(img, cax=cax) totalWidth = np.vstack(mat).shape[1] if np.ceil(max(self.xticks)) != float(totalWidth): tickscale = float(totalWidth) / max(self.xticks) xticks_use = [x * tickscale for x in self.xticks] ax.axes.set_xticks(xticks_use) else: ax.axes.set_xticks(self.xticks) ax.axes.set_xticklabels(self.xtickslabel) # align the first and last label # such that they don't fall off # the heatmap sides ticks = ax.xaxis.get_major_ticks() ticks[0].label1.set_horizontalalignment('left') ticks[-1].label1.set_horizontalalignment('right') # add labels as y ticks labels ymin, ymax = ax.axes.get_ylim() pos, distance = np.linspace(ymin, ymax, len(labels), retstep=True, endpoint=False) d_half = float(distance) / 2 yticks = [x + d_half for x in pos] ax.axes.set_yticks(yticks) # TODO: make rotation a parameter # ax.axes.set_yticklabels(labels[::-1], rotation='vertical') ax.axes.set_yticklabels(labels[::-1]) ax_list.append(ax) plt.subplots_adjust(wspace=0.05, hspace=0.3) plt.tight_layout() plt.savefig(self.out_file_name, dpi=self.dpi, format=self.image_format) plt.close()
cornerfile = kid + "_corner.png" errfile = kid + "_err.dat" light_curve = lc.LightCurve.everest(f) try: light_curve.compute(mcmc=True, mcmc_draws=500, tune=500, target_accept=0.9, prior_sig=3.0, with_SHOTerm=False, cores=28) fig = plt.figure(figsize=(20, 10)) really_outer = gridspec.GridSpec(1, 1, wspace=0.2, hspace=0.2) outer = gridspec.GridSpecFromSubplotSpec(1, 2, subplot_spec=really_outer[0], wspace=0.2, hspace=0.2) inner = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=outer[0], wspace=0.1, hspace=0.3) # raw light curve and polynomial fit ax = plt.Subplot(fig, inner[0]) light_curve.plot_raw(ax, 'k.', label="everest flux") light_curve.plot_trend(ax, linewidth=3, color="#f55649", label="third order polynomial fit")
def confoundplot(tseries, gs_ts, gs_dist=None, name=None, normalize=True, units=None, tr=None, hide_x=True, color='b', nskip=0, cutoff=None, ylims=None): # Define TR and number of frames notr = False if tr is None: notr = True tr = 1. ntsteps = len(tseries) # Normalize time series tseries = np.array(tseries) if normalize: tseries /= tr # Define nested GridSpec gs = mgs.GridSpecFromSubplotSpec(1, 2, subplot_spec=gs_ts, width_ratios=[1, 100], wspace=0.0) ax_ts = plt.subplot(gs[1]) ax_ts.grid(False) ax_ts.plot(tseries, color=color) ax_ts.set_xlim((0, ntsteps - 1)) # Set 10 frame markers in X axis interval = max((ntsteps // 10, ntsteps // 5, 1)) xticks = list(range(0, ntsteps)[::interval]) ax_ts.set_xticks(xticks) if not hide_x: if notr: ax_ts.set_xlabel('time (frame #)') else: ax_ts.set_xlabel('time (s)') labels = tr * np.array(xticks) ax_ts.set_xticklabels(['%.02f' % t for t in labels.tolist()]) else: ax_ts.set_xticklabels([]) no_scale = notr or not normalize if name is not None: var_label = name if units is not None: var_label += (' [{}]' if no_scale else ' [{}/s]').format(units) ax_ts.set_ylabel(var_label) for side in ["top", "right"]: ax_ts.spines[side].set_color('none') ax_ts.spines[side].set_visible(False) if not hide_x: ax_ts.spines["bottom"].set_position(('outward', 20)) ax_ts.xaxis.set_ticks_position('bottom') else: ax_ts.spines["bottom"].set_color('none') ax_ts.spines["bottom"].set_visible(False) ax_ts.spines["left"].set_position(('outward', 30)) ax_ts.yaxis.set_ticks_position('left') # Calculate Y limits def_ylims = [ 0.95 * tseries[~np.isnan(tseries)].min(), 1.1 * tseries[~np.isnan(tseries)].max() ] if ylims is not None: if ylims[0] is not None: def_ylims[0] = min([def_ylims[0], ylims[0]]) if ylims[1] is not None: def_ylims[1] = max([def_ylims[1], ylims[1]]) ax_ts.set_ylim(def_ylims) yticks = sorted(def_ylims) ax_ts.set_yticks(yticks) ax_ts.set_yticklabels(['%.02f' % y for y in yticks]) yrange = def_ylims[1] - def_ylims[0] # Plot average if cutoff is None: cutoff = [] cutoff.insert(0, tseries[~np.isnan(tseries)].mean()) for i, thr in enumerate(cutoff): ax_ts.plot((0, ntsteps - 1), [thr] * 2, linewidth=.75, linestyle='-' if i == 0 else ':', color=color if i == 0 else 'k') if i == 0: mean_label = r'$\mu$=%.3f%s' % (thr, units if units is not None else '') ax_ts.annotate(mean_label, xy=(ntsteps - 1, thr), xytext=(11, 0), textcoords='offset points', va='center', color='w', size=10, bbox=dict(boxstyle='round', fc=color, ec='none', color='none', lw=0), arrowprops=dict(arrowstyle='wedge,tail_width=0.8', lw=0, patchA=None, patchB=None, fc=color, ec='none', relpos=(0.01, 0.5))) else: y_off = [0.0, 0.0] for pth in cutoff[:i]: inc = abs(thr - pth) if inc < yrange: factor = (-(inc / yrange) + 1)**2 if (thr - pth) < 0.0: y_off[0] -= factor * 20 else: y_off[1] += factor * 20 offset = y_off[0] if abs(y_off[0]) > y_off[1] else y_off[1] a_label = '%.2f%s' % (thr, units if units is not None else '') ax_ts.annotate(a_label, xy=(ntsteps - 1, thr), xytext=(11, offset), textcoords='offset points', va='center', color='w', size=10, bbox=dict(boxstyle='round', fc='dimgray', ec='none', color='none', lw=0), arrowprops=dict(arrowstyle='wedge,tail_width=.9', lw=0, patchA=None, patchB=None, fc='dimgray', ec='none', relpos=(.1, .5))) if not gs_dist is None: ax_dist = plt.subplot(gs_dist) sns.displot(tseries, vertical=True, ax=ax_dist) ax_dist.set_xlabel('Timesteps') ax_dist.set_ylim(ax_ts.get_ylim()) ax_dist.set_yticklabels([]) return [ax_ts, ax_dist], gs else: return ax_ts, gs
cax = plt.Subplot(fig, outer_grid[1]) fig.add_subplot(cax) ############## # # Real figure # ############### pylab.figure(2,figsize=(6,4)) fig2 = pylab.gcf() # make three panels panels outer_grid = gridspec.GridSpec(2,1, height_ratios=[2,1],hspace=0.85) upper_grid = gridspec.GridSpecFromSubplotSpec(1,4, width_ratios=[1,1,1,0.6],wspace=0.45,subplot_spec=outer_grid[1]) dnds_axis = plt.Subplot(fig2, upper_grid[0]) fig2.add_subplot(dnds_axis) dnds_axis.set_ylabel('# changes') dnds_axis.spines['top'].set_visible(False) dnds_axis.spines['right'].set_visible(False) dnds_axis.get_xaxis().tick_bottom() dnds_axis.get_yaxis().tick_left() dnds_axis.set_xlim([0.3,2.7]) dnds_axis.set_xticks([1,2]) dnds_axis.set_xticklabels(['non','syn']) dnds_axis.set_ylim([0,300]) dnds_axis.set_yticks([0,100,200,300])