def sub_retro(rx, ry, sx, sy, times): """Creates a 2 by 3 subplot of the system at 6 chosen times Parameters ---------- rx: array, x postitions of all stars ry: array, y postitions of all stars sx: array, x postitions of S sy: array, y postitions of S times: float, time at which you are plotting Returns ------- 2 by 3 subplot of the system at 6 chosen times """ fig, ax = plt.subplots(2,3, figsize=(12,8)) c=1 for i in range(2): for j in range(3): plt.sca(ax[i,j]) t=times[i,j] plt.scatter(0,0,color= 'c', label= 'M', s=40) plt.scatter(sx[t], sy[t], color = 'b', label = 'S', s=40) plt.scatter(rx[t], ry[t], color = 'g', label = 'stars', s=7) plt.xlabel(c+2, fontsize=15) plt.xlim(-45,45) plt.ylim(-45,45) plt.box(False) plt.tick_params(axis = 'x', top = 'off', bottom = "off", labelbottom= 'off') plt.tick_params(axis = 'y', right = 'off', left= "off", labelleft= 'off') c+=1 plt.tight_layout()
def init_plot(self): self.get_subject_data() self.compute_bounds() self.compute_confidence_mapping() self.compute_predition() plt.sca(self.rt_ax) plt.step(self.rt_centers,self.subject_rt[0],'b',label='Subject hit') plt.step(self.rt_centers,-self.subject_rt[1],'r',label='Subject miss') self.hit_rt, = plt.plot(self.model_t,self.model_rt[0],'b',label='Model hit',linewidth=2) self.miss_rt, = plt.plot(self.model_t,-self.model_rt[1],'r',label='Model hit',linewidth=2) self.rt_ax.set_xlim([0,self._max_RT]) plt.xlabel('RT') plt.ylabel('Prob density') plt.legend(loc='best', fancybox=True, framealpha=0.5) plt.sca(self.conf_ax) plt.step(self.c_centers,self.subject_conf[0],'b',label='Subject hit') plt.step(self.c_centers,-self.subject_conf[1],'r',label='Subject miss') self.hit_conf, = plt.plot(np.linspace(0,1,self.model_conf.shape[1]),self.model_conf[0],'b',label='Model hit',linewidth=2) self.miss_conf, = plt.plot(np.linspace(0,1,self.model_conf.shape[1]),-self.model_conf[1],'r',label='Model hit',linewidth=2) plt.xlabel('RT') plt.ylabel('Prob density') plt.legend(loc='best', fancybox=True, framealpha=0.5) self.title = plt.suptitle('full_nLL = {full_nLL}\nconfidence_only_nLL = {confidence_only_nLL}\nfull_confidence_nLL = {full_confidence_nLL}\n'.format( full_nLL=self.full_nLL,confidence_only_nLL=self.confidence_only_nLL,full_confidence_nLL=self.full_confidence_nLL))
def stick_bgplvm(model=None, optimize=True, verbose=True, plot=True): """Interactive visualisation of the Stick Man data from Ohio State University with the Bayesian GPLVM.""" from GPy.models import BayesianGPLVM from matplotlib import pyplot as plt import numpy as np import GPy import pods data = pods.datasets.osu_run1() Q = 6 kernel = GPy.kern.RBF(Q, lengthscale=np.repeat(.5, Q), ARD=True) m = BayesianGPLVM(data['Y'], Q, init="PCA", num_inducing=20, kernel=kernel) m.data = data m.likelihood.variance = 0.001 # optimize try: if optimize: m.optimize('bfgs', messages=verbose, max_iters=5e3, bfgs_factor=10) except KeyboardInterrupt: print("Keyboard interrupt, continuing to plot and return") if plot: fig, (latent_axes, sense_axes) = plt.subplots(1, 2) plt.sca(latent_axes) m.plot_latent(ax=latent_axes) y = m.Y[:1, :].copy() data_show = GPy.plotting.matplot_dep.visualize.stick_show(y, connect=data['connect']) dim_select = GPy.plotting.matplot_dep.visualize.lvm_dimselect(m.X.mean[:1, :].copy(), m, data_show, latent_axes=latent_axes, sense_axes=sense_axes) fig.canvas.draw() # Canvas.show doesn't work on OSX. #fig.canvas.show() raw_input('Press enter to finish') return m
def plot_some_shots(run, ax=None): file_name = h5_file_name_funk(run) h5 = load_file(file_name) spec_group = h5['spectral_properties'] ##### # Select region in the tree to get traces # centers = (spec_group['center_eV'].value - # h5['photoelectron_energy_prediction_eV'].value) widths = spec_group['width_eV'].value #I = np.abs(centers) < (center_max - center_min) / 10 # I = ((np.nanmax(widths) - (np.nanmax(widths) - np.nanmin(widths)) / 5) < # widths) I = np.isfinite(widths) selected = np.random.choice(np.where(I)[0], 5) selected.sort() energy_scale_eV = h5['energy_scale_eV'].value traces = h5['energy_signal'].value if ax is None: plt.figure('energy traces ' + str(run)) plt.clf() else: plt.sca(ax) for shot in selected: plt.plot(energy_scale_eV, traces[shot, :] * 1e3) plt.xlim(70, 130)
def plotBestFit(weights): m = shape(dataMat)[0] xcord1 = [] ycord1 = [] xcord2 = [] ycord2 = [] for i in range(m): if labelMat[i] == 1: xcord1.append(dataMat[i, 1]) ycord1.append(dataMat[i, 2]) else: xcord2.append(dataMat[i, 1]) ycord2.append(dataMat[i, 2]) plt.figure(1) ax = plt.subplot(111) ax.scatter(xcord1, ycord1, s=30, c='red', marker='s') ax.scatter(xcord2, ycord2, s=30, c='green') x = arange(0.2, 0.8, 0.1) y = array((-weights[0] - weights[1] * x) / weights[2]) print shape(x) print shape(y) plt.sca(ax) plt.plot(x, y) # ramdomgradAscent # plt.plot(x,y[0]) #gradAscent plt.xlabel('density') plt.ylabel('ratio_sugar') # plt.title('gradAscent logistic regression') plt.title('ramdom gradAscent logistic regression') plt.show()
def _plot_cc_mat(pdata, ax): plt.sca(ax) plt.imshow(pdata['CC'], interpolation='nearest', aspect='auto', vmin=-0.5, vmax=0.5, cmap=plt.cm.seismic) plt.yticks(range(nelectrodes), ['%d' % _e for _e in electrode_order]) plt.xticks(range(nfreqs), ['%d' % _f for _f in freqs]) plt.title(pdata['aprop']) plt.colorbar()
def demo_alternatinggraphcut(): t = 12 ## Simulate small image im, G = simulation.simulate_image(11,13,t,theta=0.4*np.pi ) # Show grid overlaid image fig, ax = plt.subplots(1,2) plt.sca(ax[0]) plt.imshow(im,cmap=plt.cm.gray) plt.title('True grid') plotgrid(G) # Add random, disturbing, points Nnew = 15 x_new = np.random.uniform(np.min(G.xy[:,0]),np.max(G.xy[:,0]), Nnew) y_new = np.random.uniform(np.min(G.xy[:,1]),np.max(G.xy[:,1]), Nnew ) xy_new = np.vstack((x_new,y_new)).T # Add to grid and make naive guess for edges G.xy = np.vstack((G.xy,xy_new)) G.resolve_edges(remove_long=False) # Show this modified grid plt.sca(ax[1]) plotgrid(G) plt.title('Modified/disturbed grid') plt.show() # Clean up using alternating graph cut xy_hat, simplices_hat = alternating_graphcut.cleanup(G.xy,im,alpha=3,beta=2) G_hat = grid.TriangularGrid.from_simplices(xy_hat,simplices_hat) plt.figure() plotgrid(G_hat) plt.show(block=True)
def map(self, func, **kwargs): """Plot with the same function in every subplot. Parameters ---------- func : callable plotting function Must take x, y arrays as positional arguments and draw onto the "currently active" matplotlib Axes. """ kw_color = kwargs.pop("color", None) for i, y_var in enumerate(self.y_vars): for j, x_var in enumerate(self.x_vars): hue_grouped = self.data.groupby(self.hue_vals) for k, (label_k, data_k) in enumerate(hue_grouped): ax = self.axes[i, j] plt.sca(ax) color = self.palette[k] if kw_color is None else kw_color func(data_k[x_var], data_k[y_var], label=label_k, color=color, **kwargs) self._clean_axis(ax) self._update_legend_data(ax) if kw_color is not None: kwargs["color"] = kw_color self._add_axis_labels()
def map_upper(self, func, **kwargs): """Plot with a bivariate function on the upper diagonal subplots. Parameters ---------- func : callable plotting function Must take x, y arrays as positional arguments and draw onto the "currently active" matplotlib Axes. """ kw_color = kwargs.pop("color", None) for i, j in zip(*np.triu_indices_from(self.axes, 1)): hue_grouped = self.data.groupby(self.hue_vals) for k, (label_k, data_k) in enumerate(hue_grouped): ax = self.axes[i, j] plt.sca(ax) x_var = self.x_vars[j] y_var = self.y_vars[i] color = self.palette[k] if kw_color is None else kw_color func(data_k[x_var], data_k[y_var], label=label_k, color=color, **kwargs) self._clean_axis(ax) self._update_legend_data(ax) if kw_color is not None: kwargs["color"] = kw_color
def colorline(x, y, z=None, cmap=plt.get_cmap('copper'), ax=None, norm=plt.Normalize(0.0, 1.0), label='', linewidth=3, alpha=1.0): ''' Plot a colored line with coordinates x and y Optionally specify colors in the array z Optionally specify a colormap, a norm function and a line width ''' from matplotlib.collections import LineCollection # Default colors equally spaced on [0,1]: if z is None: z = np.linspace(0.0, 1.0, len(x)) # Special case if a single number: if not hasattr(z, "__iter__"): # to check for numerical input -- this is a hack z = np.array([z]) z = np.asarray(z) if ax is None: fig, ax = plt.subplots(nrows=1, ncols=1) fig.patch.set_facecolor('white') else: plt.sca(ax) fig = plt.gcf() segments = make_segments(x, y) lc = LineCollection(segments, array=z, cmap=cmap, norm=norm, linewidth=linewidth, alpha=alpha) ax.add_collection(lc) return lc
def plot_marginals(self, func, **kwargs): """Draw univariate plots for `x` and `y` separately. Parameters ---------- func : plotting callable This must take a 1d array of data as the first positional argument, it must plot on the "current" axes, and it must accept a "vertical" keyword argument to orient the measure dimension of the plot vertically. kwargs : key, value mappings Keyword argument are passed to the plotting function. Returns ------- self : JointGrid instance Returns `self`. """ plt.sca(self.ax_marg_x) func(self.x, **kwargs) kwargs["vertical"] = True plt.sca(self.ax_marg_y) func(self.y, **kwargs) return self
def add_bovy_rix(axScatter,Rmin,Rmax,zmin,zmax): #Now calculate and plot our model fehs= numpy.linspace(-1.6,0.5,26) afes= numpy.linspace(-0.15,0.55,25) ourDist= numpy.zeros((len(fehs),len(afes))) for ii in range(len(fehs)): for jj in range(len(afes)): ourDist[ii,jj]= maps.abundanceDist(fehs[ii],afes[jj], z=[zmin*1000.,zmax*1000.], r=[Rmin,Rmax]) _ADJUSTABUNDANCES= True _ADJUSTFEH= True if _ADJUSTABUNDANCES: if _ADJUSTFEH: fehs+= 0.15 afes/= 1.5 afes-= 0.025 #Contour this in axScatter ourDist[numpy.isnan(ourDist)]= 0. sortindx= numpy.argsort(ourDist.flatten())[::-1] cumul= numpy.cumsum(numpy.sort(ourDist.flatten())[::-1])/numpy.sum(ourDist.flatten()) cntrThis= numpy.zeros(numpy.prod(ourDist.shape)) cntrThis[sortindx]= cumul cntrThis= numpy.reshape(cntrThis,ourDist.shape) pyplot.sca(axScatter) CS= pyplot.contour(fehs,afes, cntrThis.T,levels=[0.68,0.95], linewidths=3.,linestyles='dashed', colors='r',zorder=10)
def plot_locations(self, ax=None, lu=None): if self.annotations_loaded == False: return if ax is None: fig, ax = pl.subplots(1, 1, sharex=True, sharey=False, figsize=(20, 5)) else: pl.sca(ax) if lu is None: lu = (self.meta['start'], self.meta['end']) palette = it.cycle(sns.husl_palette()) offsets = self.get_offsets() for ai in xrange(self.num_annotators): col = next(palette) offset = offsets[ai] for index, rr in slice_df_start_stop(self.locations[ai], lu).iterrows(): pl.plot([rr['start'], rr['end']], [self.location_targets.index(rr['name']) + offset * 2] * 2, color=col, linewidth=5, alpha=0.5) pl.yticks(np.arange(len(self.location_targets)), self.location_targets) pl.ylim((-1, len(self.location_targets))) pl.xlim(lu)
def plot_session_PSTH(self, session, tetrode, experiment=-1, site=-1, cluster = None, sortArray='currentFreq', timeRange = [-0.5, 1], replace=0, lw=3, colorEachCond=None): sessionObj = self.get_session_obj(session, experiment, site) sessionDir = sessionObj.ephys_dir() ephysData, bdata, info = self.load_session_data(session, experiment, site, tetrode, cluster) eventOnsetTimes = ephysData['events']['stimOn'] spikeTimestamps = ephysData['spikeTimes'] if bdata is not None: sortArray = bdata[sortArray] if colorEachCond is None: colorEachCond = self.get_colours(len(np.unique(sortArray))) else: sortArray = [] plotTitle = info['sessionDir'] ephysData = ephyscore.load_ephys(sessionObj.subject, sessionObj.paradigm, sessionDir, tetrode, cluster) eventOnsetTimes = ephysData['events']['stimOn'] spikeTimestamps = ephysData['spikeTimes'] if replace==1: plt.cla() elif replace==2: plt.sca(ax) else: plt.figure() plot_psth(spikeTimestamps, eventOnsetTimes, sortArray = sortArray, timeRange=timeRange, lw=lw, colorEachCond=colorEachCond, plotLegend=0)
def niceplot(ax=None, axfs='12', lfs='14', tightlayout=True, mew=1.25, lw=2.0, ms=7.0, **kwargs): """Pretty up a plot for publication. Parameters ---------- ax : matplotlib.axes.AxesSubplot, optional An axis to niceify. Default is all axes in the current figure. axfs : string, float, or int, optional Axis tick label font size. lfs : string, float, or int, optional Axis label font size. tightlayout : bool, optional Run `plt.tight_layout`. **kwargs Any line or marker property keyword. """ import matplotlib.pyplot as plt if ax is None: for ax in plt.gcf().get_axes(): niceplot(ax, tightlayout=tightlayout, axfs=axfs, lfs=lfs, **kwargs) # for the axes plt.setp(ax.get_ymajorticklabels(), fontsize=axfs) plt.setp(ax.get_xmajorticklabels(), fontsize=axfs) # axis labes labels = (ax.xaxis.get_label(), ax.yaxis.get_label()) plt.setp(labels, fontsize=lfs) # for plot markers, ticks lines = ax.get_lines() mew = kwargs.pop('markeredgewidth', kwargs.pop('mew', None)) if mew is not None: plt.setp(lines, mew=mew) ms = kwargs.pop('markersize', kwargs.pop('ms', None)) if ms is not None: plt.setp(lines, ms=ms) lw = kwargs.pop('linewidth', kwargs.pop('lw', None)) if lw is not None: plt.setp(lines, lw=lw) if len(kwargs) > 0: plt.setp(lines, **kwargs) lines = ax.xaxis.get_minorticklines() + ax.xaxis.get_majorticklines() + \ ax.yaxis.get_minorticklines() + ax.yaxis.get_majorticklines() plt.setp(lines, mew=1.25) # the frame plt.setp(ax.patch, lw=2.0) if hasattr(plt, "tight_layout") and tightlayout: plt.sca(ax) plt.tight_layout()
def plot2d(signal, tmin=0.0, tmax=None, axes=None, **kwargs): plot_type = kwargs.pop('type', 'contourf') nlevels = int(kwargs.pop('nlevels', 100)) default_min = float(kwargs.pop('minrange', 0.)) default_max = float(kwargs.pop('maxrange', 1.)) plot_func = getattr(plt, plot_type) xaxis = getattr(signal, signal.axes[1]) yaxis = getattr(signal, signal.axes[0]) xaxis[:] yaxis[:] if tmax is None: tmax = yaxis[-1] # this should be yaxis.max() when fixed plot_range = set_range(signal, default_min, default_max) levels = np.linspace(plot_range[0], plot_range[1], nlevels) plt.sca(axes) artist = plot_func(np.array(xaxis), np.array(yaxis), np.array(signal), levels=levels, **kwargs) plt.ylabel('{} ({})'.format(yaxis._name, yaxis.units)) plt.xlabel('{} ({})'.format(xaxis._name, xaxis.units)) plt.title('{} -- {} -- {}'.format(signal._parent._name.upper(), signal._name, signal.shot)) plt.ylim(tmin, tmax) if plot_type == 'contourf': cbar = plt.colorbar(artist, format='%.1e') cbar.set_label(signal.units, rotation=270)
def plot(w): dataMat=np.array(df[['density','ratio_sugar']].values[:,:]) labelMat=np.mat(df['good'].values[:]).transpose() m=np.shape(dataMat)[0] xcord1=[] ycord1=[] xcord2=[] ycord2=[] for i in range(m): if labelMat[i]==1: xcord1.append(dataMat[i,0]) ycord1.append(dataMat[i,1]) else: xcord2.append(dataMat[i,0]) ycord2.append(dataMat[i,1]) plt.figure(1) ax=plt.subplot(111) ax.scatter(xcord1,ycord1,s=30,c='red',marker='s') ax.scatter(xcord2,ycord2,s=30,c='green') x=np.arange(-0.2,0.8,0.1) y=np.array((-w[0,0]*x)/w[0,1]) plt.sca(ax) plt.plot(x,y) #gradAscent plt.xlabel('density') plt.ylabel('ratio_sugar') plt.title('LDA') plt.show()
def plot(): plt.ioff() fig_mpl, ax = plt.subplots(1,figsize=(8,8), facecolor='white'); plt.xlim(-75,75); plt.ylim(-75,75); plt.sca(ax); ax.set_xlabel("X Position (kpc)"); ax.set_ylabel("Y Position (kpc)"); plt.title("Relative Motion"); plt.legend() scat = ax.scatter(X[0],Y[0],color='red',label='Disrupting Galaxy, Mass S',s=10); scatt = ax.scatter(x,y,color='green',s=5); scattt = ax.scatter(0,0,color='black',label='Central Galaxy, Mass M',s=10); def make_gif(u): newX,newY = X[u*40],Y[u*40] scat.set_offsets(np.transpose(np.vstack([newX,newY]))) newx=[x[k][u*40] for k in range(120)] newy=[y[k][u*40] for k in range(120)] scatt.set_offsets(np.transpose(np.vstack([newx,newy]))) return mplfig_to_npimage(fig_mpl) if parabolic == 1: animation = mpy.VideoClip(make_gif,duration=tmax*25); else: animation = mpy.VideoClip(make_gif,duration=tmax*5); #animation.ipython_display(fps=50) animation.write_videofile("Retrograde Elliptic Passage.mp4", fps=50)
def create_pseudo_random_code(clen=10000,rseed=0,verbose=False): """ Create waveform files for hfradar Juha Vierinen """ Npt = 200 # number of points to plot, just for plotting, arbitrary """ seed is a way of reproducing the random code without having to store all actual codes. the seed can then act as a sort of station_id. """ seed(rseed) """ generate a uniform random phase modulated (complex) signal 'sig". It's single precision floating point for SDR, since DAC is typically <= 16 bits! """ sig = np.exp(1j*2.0*np.pi*random(clen)).astype('complex64') if stuffr is not None: stuffr.plot_cts(sig[:Npt]) if verbose and hist is not None: fg,ax = subplots(3,1) sca(ax[0]) hist(sig.real)#,50) sca(ax[1]) hist(sig.imag) #hist(random(clen)) return sig
def plot_col_pos(cs_xzdf, cs_xydf, cs_xdf, colposperiod, colpostype): dat=pd.date_range(start=cs_xzdf.index[0],end=cs_xzdf.index[-1], freq=colposperiod) fig,ax=plt.subplots(nrows=1,ncols=2, sharex=True, sharey=True,figsize=fig_size) plt.suptitle(colname+"\n"+colpostype+" col pos") cm = plt.get_cmap('gist_rainbow') ax[0].set_color_cycle([cm(1.*(len(dat)-i-1)/len(dat)) for i in range(len(dat))]) ax[1].set_color_cycle([cm(1.*(len(dat)-i-1)/len(dat)) for i in range(len(dat))]) for d in range(len(dat)): curxz=cs_xzdf[(cs_xzdf.index==dat[d])] curxy=cs_xydf[(cs_xydf.index==dat[d])] curx=cs_xdf[(cs_xdf.index==dat[d])] if colpostype=='rel': curxz=curxz.sub(cs_xzdf.iloc[0,:],axis=1) curxy=curxy.sub(cs_xydf.iloc[0,:],axis=1) plt.sca(ax[0]) plt.axis('equal') plt.plot([[0]]+curxz.values.T.tolist(),[[0.0]]+curx.values.T.tolist(), '.-') plt.sca(ax[1]) plt.axis('equal') plt.plot([[0.0]]+curxy.values.T.tolist(),[[0.0]]+curx.values.T.tolist(), '.-',label=datetime.strftime(dat[d],'%Y-%m-%d')) ax[0].set_xlabel("XZ disp, m \n (+) downslope", fontsize='small',horizontalalignment='center') ax[0].set_ylabel("X disp, m \n (+) towards surface", fontsize='small', rotation='vertical',horizontalalignment='center') ax[1].set_xlabel("XY disp, m \n (+) to the right, facing downslope", fontsize='small', horizontalalignment='center') plt.legend(loc='lower right', fontsize='small') plt.tight_layout() plt.subplots_adjust(left=None, bottom=None, right=None, top=0.9,wspace=None, hspace=None) return fig,ax
def scale_axis_limits( self, k1, k2, k3, k4 ): # k1, k2, k3, k4 are scaling parameters for # xmin, xmax, ymin, ymax, respectively. sca( self.ax ) [xmin, xmax, ymin, ymax] = axis() self.ax.set_xlim( xmin * k1, xmax * k2 ) self.ax.set_ylim( ymin * k3, ymax * k4 )
def labeledDendrogram(dmat, labels, method='complete', cmap=None): """Perform hierarchical clustering on df columns and plot square heatmap of pairwise distances""" """TODO: add tick labels, with sparsity option""" Z = sch.linkage(dmat, method=method) den = sch.dendrogram(Z, color_threshold=np.inf, no_plot=True) figh = plt.gcf() figh.clf() denAX = figh.add_axes([0.32, 0.05, 0.6, 0.9]) cbAX = figh.add_axes([0.25, 0.05, 0.05, 0.9]) plt.sca(denAX) denD = sch.dendrogram(Z, color_threshold=np.inf, orientation='left') ind = denD['leaves'] clean_axis(denAX) cbSE, lookup = mapColors2Labels(labels, cmap=cmap, returnLookup=True) axi = cbAX.imshow([[x] for x in cbSE.iloc[ind].values], interpolation='nearest', aspect='auto', origin='lower') clean_axis(cbAX) colorLegend(list(lookup.values()), list(lookup.keys()), axh=denAX)
def update(self, t, alpha, beta, x_basis=False): from matplotlib import pyplot as pl ax = self.ax pl.sca(ax) pl.cla() x_basis = self.x_basis if x_basis: from sglib import ip, col from numpy import sqrt alpha_x = ip(col(1,1)/sqrt(2), col(alpha,beta)) beta_x = ip(col(1,-1)/sqrt(2), col(alpha,beta)) alpha = alpha_x; beta = beta_x prob_plus = alpha*alpha.conjugate() prob_minus = beta*beta.conjugate() pos_plus = self.pos_plus pos_minus = self.pos_minus width = self.width p1 = pl.bar([pos_plus], prob_plus, width, color='blue') p2 = pl.bar([pos_minus], prob_minus, width, color='red') pl.xticks(self.xticks, self.xlabels) pl.ylabel('Probability') pl.yticks(self.yticks) pl.xlim(0, 1.1) pl.ylim(0, 1.1) pl.draw() pl.show()
def draw_all(self): for i in range(0, len(self.cpustats)): ax = plt.subplot(2, len(self.cpustats)/2, i) plt.sca(ax) self.draw(self.cpustats[i], plt) plt.show()
def updateImage(self): # clear plot plt.sca(self.ax) plt.cla() self.initImage() # Fetch data from SQL database # try: if self.current_plot == 'time_stack': x,y = self.main_widget.imageAquisitionTab.stack.fetch_mologram_data() elif self.current_plot == 'post_processing': x,y = self.main_widget.postProcessingTab.get_time_and_molo_intensity() else: print('Current plot is empty') return # except Exception: # print('Couldn\'t get data to plot') # return # plot appearance plt.xlim([0, np.amax(x)]) plt.ylim([np.amin(y),np.amax(y)]) self.figure.tight_layout() # add titles self.ax.set_xlabel('Time (s)') self.ax.set_ylabel('Mologram Intensity (arbitrary units)') self.ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0)) # plot data and show canvas self.points = plt.plot(x,y) self.canvas.draw()
def plot_confusion_matrix(cm, classes, ax, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ print(cm) print('') ax.imshow(cm, interpolation='nearest', cmap=cmap) ax.set_title(title) tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.sca(ax) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): ax.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") ax.set_ylabel('True label') ax.set_xlabel('Predicted label')
def map(self, func, *args, **kwargs): """ Apply a plotting function to each facet's subset of the data. Parameters ---------- func : callable A plotting function that takes data and keyword arguments. It must plot to the currently active matplotlib Axes and take a `color` keyword argument. If faceting on the `hue` dimension, it must also take a `label` keyword argument. args : strings Column names in self.data that identify variables with data to plot. The data for each variable is passed to `func` in the order the variables are specified in the call. kwargs : keyword arguments All keyword arguments are passed to the plotting function. Returns ------- self : FacetGrid object """ import matplotlib.pyplot as plt for ax, namedict in zip(self.axes.flat, self.name_dicts.flat): if namedict is not None: data = self.data[namedict] plt.sca(ax) innerargs = [data[a].values for a in args] func(*innerargs, **kwargs) return self
def visualize_predictions(prediction_seqs, label_seqs, num_classes, fig_width=6.5, fig_height_per_seq=0.5): """ Visualize predictions vs. ground truth. Args: prediction_seqs: A list of int NumPy arrays, each with shape `[duration, 1]`. label_seqs: A list of int NumPy arrays, each with shape `[duration, 1]`. num_classes: An integer. fig_width: A float. Figure width (inches). fig_height_per_seq: A float. Figure height per sequence (inches). Returns: A tuple of the created figure, axes. """ num_seqs = len(label_seqs) max_seq_length = max([seq.shape[0] for seq in label_seqs]) figsize = (fig_width, num_seqs*fig_height_per_seq) fig, axes = plt.subplots(nrows=num_seqs, ncols=1, sharex=True, figsize=figsize) for pred_seq, label_seq, ax in zip(prediction_seqs, label_seqs, axes): plt.sca(ax) plot_label_seq(label_seq, num_classes, 1) plot_label_seq(pred_seq, num_classes, -1) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) plt.xlim(0, max_seq_length) plt.ylim(-2.75, 2.75) plt.tight_layout() return fig, axes
def visualize(model, filename): values = model.all_params() cols = OrderedDict() for key, value in values.items(): if isinstance(value, np.ndarray): # TODO group by prefix, showing W and b side by side cols.setdefault(key[:-2] + key[-1] if len(key) > 1 and key[-2] in "bW" else key, []).append((key, value)) _, axes = plt.subplots(2, len(cols), figsize=(5*len(cols), 10)) # TODO https://stackoverflow.com/a/13784887/223267 plt.tight_layout() for j, col in enumerate(tqdm(cols.values(), unit="param", desc=filename)): for i in range(2): axis = axes[i, j] if len(values) > 1 else axes if len(col) <= i: plt.delaxes(axis) else: plt.sca(axis) key, value = col[i] plt.colorbar(plt.pcolormesh(smooth(value))) for pattern, repl in REPLACEMENTS: # TODO map 0123->ifoc key = re.sub(pattern, repl, key) plt.title(key) for axis in (plt.gca().xaxis, plt.gca().yaxis): axis.set_major_locator(MaxNLocator(integer=True)) output_file = filename + ".png" plt.savefig(output_file) plt.clf() print("Saved '%s'." % output_file)
def plotrfACC(): #data = json.loads(open('rf_accs.json').read()) data = json.loads(open('rf_accs_top3.json').read()) data = json.loads(open('rf_accs_nowindow.json').read()) nLetter = 3 #14 data["texts/ADHD_various_half/"] = [data["texts/ADHD_various_half/"][i] for i in [1,2,3]] sns.set_style("dark") #f, (ax1, ax2) = plt.subplots(1, 2) f, ax1 = plt.subplots() bar1 = ax1.bar(range(nLetter),data["texts/ADHD_various_half/"]) ax1.set_title('RF accs for half SAX') plt.sca(ax1) plt.xticks(np.arange(nLetter) + .4, range(3,nLetter+3)) plt.xlabel('# of bins (letters)/word') ax1.set_ylim([0.6,0.9]) #bar2 = ax2.bar(range(nLetter),data["texts/ADHD_various_full/"]) #ax2.set_title('RF accs for full SAX') #plt.sca(ax2) #plt.xticks(np.arange(nLetter) + .4, range(2,nLetter+2)) #plt.xlabel('# of bins (letters)/word') #ax2.set_ylim([0.6,0.9]) plt.show()
plt.xlabel("covariate", fontsize=15) plt.xticks(np.arange(36), covariates, fontsize=12, rotation=90) plt.axhline(y=0, color="k", alpha=0.5, ls="--") plt.tight_layout() # Get expected states: posterior_probs = [ bandit_glmhmm.expected_states(data=meta_true_choices, input=meta_inpts)[0] for meta_true_choices, meta_inpts in zip(meta_true_choices, meta_inpts) ] fig, ax = plt.subplots(5, 4, figsize=(15, 20)) for i in range(num_sessions): column = int(np.floor(i / 5)) row = i % 5 plt.sca(ax[row, column]) for k in range(num_states): plt.plot(posterior_probs[i][:, k], label="State " + str(k + 1), lw=2, color=cols[k]) plt.ylim((-0.01, 1.01)) plt.yticks([0, 0.5, 1], fontsize=10) plt.xlabel("trial #", fontsize=15) plt.ylabel("p(state)", fontsize=15) plt.tight_layout() plt.plot(log_likelihoods.mean(axis=1)) plt.xticks(np.arange(5), np.arange(1, 6)) plt.xlabel('Number of latents') plt.ylabel('LL')
0] = 1e-8 #replace zero values with a small value to avoid dividing by zero. KL[i] = stats.entropy(int_density[:, i], qk=int_density_prior[:, i]) ax3_twin = ax3.twinx() ax3_twin.plot(x_density[1, :], KL, 'r') ax3_twin.set_ylabel('KL divergence', fontsize=16, color='r') ax3_twin.tick_params('y', colors='r', labelsize=16) ax3_twin.tick_params(direction='out', pad=5) # produce an image of the prior intensity: threshold_prior = 0.001 int_density_prior_refined = int_density_prior.copy() int_density_prior_refined[ int_density_prior_refined < threshold_prior] = 0.0 fig8, ax8 = plt.subplots(figsize=(8, 5)) plt.sca(ax8) plt.imshow(int_density_prior_refined, origin='lower', cmap=cm.jet, extent=(x_density_prior[0, 0], x_density_prior[0, -1], y_density_prior[0, 0], y_density_prior[-1, 0]), aspect='auto', norm=LogNorm(vmin=0.001, vmax=0.1), interpolation="nearest") ax8.set_xlim(x_density_prior[0, 0], x_density_prior[0, -1]) ax8.set_ylim(y_density_prior[0, 0], y_density_prior[-1, 0]) ax8.set_xlabel('Age/yr', fontsize=16) ax8.set_ylabel('Intensity/$\mu$T', fontsize=16) cb = plt.colorbar(ticks=[ 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01
df_emp1 = df_emp.groupby(['rango_n_transac']).size().to_frame() df_emp1.rename(columns={0: 'donante empresas'}, inplace=True) for frame in [df_co1, df_int1, df_ext1, df_emp1]: frame.plot(ax=ax1) ax1.set_xlabel('Número de transacciones promedio al año') ax1.set_ylabel('Cantidad de donantes') ax1.set_title("Número promedio de transacciones al año de donantes") df_ext1 = df_ext.groupby(['rango_n_transac' ])['donacion_total'].sum().to_frame() df_ext1.rename(columns={'donacion_total': 'donante particulares externas'}, inplace=True) # print(df_ext1) df_int1 = df_int.groupby(['rango_n_transac' ])['donacion_total'].sum().to_frame() df_int1.rename(columns={'donacion_total': 'donante particulares internas'}, inplace=True) df_co1 = df_co.groupby(['rango_n_transac'])['donacion_total'].sum().to_frame() df_co1.rename(columns={'donacion_total': 'donante cooperadoras'}, inplace=True) df_emp1 = df_emp.groupby(['rango_n_transac' ])['donacion_total'].sum().to_frame() df_emp1.rename(columns={'donacion_total': 'donante empresas'}, inplace=True) for frame in [df_co1, df_int1, df_ext1, df_emp1]: frame.plot(ax=ax2) ax2.set_ylabel('Monto acumulado (en miles de millones)') ax2.set_xlabel('Número de transacciones promedio al año') ax2.set_title("Monto total acumulado por transacciones") for ax in fig.axes: plt.sca(ax) plt.xticks(rotation=0) fig.savefig('./images/todos/descriptivo_14.png')
] axBW.set_xticks(range(2)) axBW.set_xlim([-0.5, 1.5]) extraplots.boxoff(axBW) extraplots.set_ticks_fontsize(axBW, fontSizeTicks) axBW.set_xticklabels(tickLabels, fontsize=fontSizeLabels, rotation=45) zstat, pVal = stats.mannwhitneyu(nD1PopStat, D1PopStat) #Nick used stats.ranksum messages.append("{} p={}".format(popStatCol, pVal)) yDataMax = max([max(D1PopStat), max(nD1PopStat)]) yStars = yDataMax + yDataMax * starYfactor yStarHeight = (yDataMax * starYfactor) * starHeightFactor plt.sca(axBW) starString = None if pVal < 0.05 else 'n.s.' extraplots.significance_stars([0, 1], yStars, yStarHeight, starMarker='*', starSize=fontSizeStars + 2, starString=starString, gapFactor=starGapFactor) plt.hold(1) # plt.hold(True) if PANELS[8]: popStatCol = 'thresholdFRA' nD1PopStat = D1.query('{} == {}'.format(popStatCol,
def perform_fft(input_info, plot=False): print("\nspawn FFT process nr : ", input_info["process_nr"]) existing_shm = shared_memory.SharedMemory(name=input_info["shm"].name) # check if poly calculation is on to be executed, because then there must be an additional key in the dictionary if "poly" in input_info.keys(): poly = True reference_to_data_block = numpy.ndarray( input_info["dim"], dtype=numpy.float64, buffer=existing_shm.buf)[:, input_info["from"]:input_info["to"], :] else: poly = False reference_to_data_block = numpy.ndarray( input_info["dim"], dtype=numpy.float64, buffer=existing_shm.buf)[:, input_info["from"]:input_info["to"], :] # get data to process out of buffer print("ref data dtype: ", reference_to_data_block.dtype) print("\n") # reshape data from buffer to 2d matrix with the time as y coords and x as the values data_mat = reference_to_data_block.reshape( reference_to_data_block.shape[0], reference_to_data_block.shape[1] * reference_to_data_block.shape[2]) # strore orig time, cols and row information - needed for reshaping orig_time = reference_to_data_block.shape[0] orig_rows = reference_to_data_block.shape[1] orig_cols = reference_to_data_block.shape[2] # if plots are whished if plot: existing_shm_qual = shared_memory.SharedMemory( name=input_info["shm_qual"].name) if poly: reference_to_qual_block = numpy.ndarray( input_info["dim"], dtype=numpy.float64, buffer=existing_shm_qual.buf )[:, input_info["from"]:input_info["to"], :] else: reference_to_qual_block = numpy.ndarray( input_info["dim"], dtype=numpy.float64, buffer=existing_shm_qual.buf )[:, input_info["from"]:input_info["to"], :] qual_weights = input_info["weights"] qual_factor = 1 qual_mat = reference_to_qual_block.reshape( reference_to_qual_block.shape[0], reference_to_qual_block.shape[1] * reference_to_qual_block.shape[2]) qual_mat[qual_mat == 255] = numpy.nan print("Data Mat Shape", data_mat.shape) # setting int Nan value to numpy.nan --> transforms dataytpe to floa64!!! data_mat = numpy.where(data_mat == 32767, numpy.nan, data_mat) n = data_mat.shape[0] t = numpy.arange(0, n, 1) # iter through for i in range(0, data_mat.shape[1], 1): data_mat_v_nan = numpy.isfinite(data_mat[:, i]) data_mat_v_t = numpy.arange(0, len(data_mat_v_nan), 1) if False in data_mat_v_nan: try: # interpolate on that spots data_mat_v_interp = numpy.round( numpy.interp(data_mat_v_t, data_mat_v_t[data_mat_v_nan], data_mat[:, i][data_mat_v_nan])) # calculate the fft f_hat = numpy.fft.fft(data_mat_v_interp, n) # and the power spectrum - which frequencies are dominant power_spectrum = f_hat * numpy.conj(f_hat) / n # get the max power value max_fft_spectr_value = numpy.max(power_spectrum) # set it to zeros so one can find those frequencies that are far lower and important but still no noise power_spec_no_max = numpy.where( power_spectrum == max_fft_spectr_value, 0, power_spectrum) threshold_remaining_values = numpy.nanmax( power_spec_no_max) / 2 indices = power_spectrum > threshold_remaining_values f_hat = indices * f_hat ffilt = numpy.fft.ifft(f_hat) if plot: if i <= 3: print("proces nr %d i == %d" % (input_info["process_nr"], i)) print("data mat: ", data_mat[:, i]) print("data_mat_v_interp", data_mat_v_interp) # print("data mat:", data_mat[:, i]) # print("data_mat.dtype: ", data_mat.dtype) # print("data_mat_interp.dtype: ", data_mat_v_interp.dtype) ffilt = numpy.round(ffilt).astype(numpy.int16) # print("\ntransfrom to int16: ", data_mat_v_interp) # print("\ndata_mat_interp.dtype: ", data_mat_v_interp.dtype) fig, axs = plt.subplots(3, 1) good_qual = numpy.where( qual_mat[:, i] == qual_weights[0], qual_weights[0], numpy.nan) * qual_factor okay_qual = numpy.where( qual_mat[:, i] == qual_weights[1], qual_weights[1], numpy.nan) * qual_factor bad_qual = numpy.where( qual_mat[:, i] == qual_weights[2], qual_weights[2], numpy.nan) * qual_factor really_bad_qual = numpy.where( qual_mat[:, i] == qual_weights[3], qual_weights[3], numpy.nan) * qual_factor nan_values = numpy.where(qual_mat[:, i] == 255, qual_weights[3], numpy.nan) plt.sca(axs[0]) plt.plot(t, data_mat[:, i], color='c', LineWidth=3, label="raw data") plt.plot(t, data_mat_v_interp, color='k', LineWidth=1, linestyle='--', label='lin interp') plt.plot(t, ffilt, color="k", LineWidth=2, label='FFT Filtered') plt.plot(t, good_qual, 'go', label="Good Quality") plt.plot(t, okay_qual, 'yo', label="Okay Quality") plt.plot(t, bad_qual, 'o', color='orange', label="Bad Quality") plt.plot(t, really_bad_qual, 'ro', label="Really Bad Quality") plt.plot(t, nan_values, 'ko', label="NaN Values") plt.xlim(t[0], t[-1]) plt.ylabel("Intensity [%]") plt.xlabel("Time [days]") plt.legend() plt.sca(axs[1]) plt.plot(t, power_spectrum, color="c", LineWidth=2, label="Noisy") plt.plot(t, power_spectrum, 'b*', LineWidth=2, label="Noisy") plt.plot(t[0], t[-1]) plt.xlabel("Power Spectrum [Hz]") plt.ylabel("Power") plt.title( "Power Spectrum Analyses - Max: {} - Threshold: {}" .format(max_fft_spectr_value, numpy.nanmean(power_spectrum))) plt.sca(axs[2]) plt.plot(t, power_spec_no_max, color="c", LineWidth=2, label="Noisy") plt.plot(t, power_spec_no_max, 'b*', LineWidth=2, label="Noisy") plt.plot(t[0], t[-1]) plt.xlabel("Power Spectrum no max [Hz]") plt.ylabel("Power") plt.title( "Power Spectrum Analysis - removed big max {} - Max: {} - Threshold: {}" .format(max_fft_spectr_value, numpy.nanmax(power_spec_no_max), threshold_remaining_values)) # plot data plt.show() data_mat[:, i] = ffilt except: # gets triggered most if there are only nans in the array continue else: # calculate the fft f_hat = numpy.fft.fft(data_mat[:, i], n) # and the power spectrum - which frequencies are dominant power_spectrum = f_hat * numpy.conj(f_hat) / n # get the max power value max_fft_spectr_value = numpy.max(power_spectrum) # set it to zeros so one can find those frequencies that are far lower and important but still no noise power_spec_no_max = numpy.where( power_spectrum == max_fft_spectr_value, 0, power_spectrum) threshold_remaining_values = numpy.nanmax(power_spec_no_max) / 2 indices = power_spectrum > threshold_remaining_values f_hat = indices * f_hat ffilt = numpy.fft.ifft(f_hat) data_mat[:, i] = ffilt # transorm float64 back to INT16!! # save interpolation results on the shared memory object if poly: reference_to_data_block[:] = numpy.round( data_mat.reshape(orig_time, orig_rows, orig_cols)) else: reference_to_data_block[:] = numpy.round( data_mat.reshape(orig_time, orig_rows, orig_cols))
ax[0].set_xlim(-2, 2) ax[0].set_ylim(-1, 1) ax[1].set_xlim(0, max(t)) ax[1].set_ylim(min(x) * 1.2, max(x * 1.2)) ax[1].set_ylabel('X') ax[2].set_xlim(0, max(t)) ax[2].set_ylim(min(x) * 1.2, max(x * 1.2)) ax[2].set_ylabel('Y') line, = ax[0].plot([], [], lw=2, alpha=0.5) dot, = ax[0].plot([], [], 'ro', ms=5) marker1, = ax[0].plot([], [], 'k', alpha=0.5, lw=2) marker2, = ax[1].plot([], [], 'k', alpha=0.5, lw=2) var1, = ax[1].plot([], [], lw=2, alpha=0.5) var2, = ax[2].plot([], [], lw=2, alpha=0.5) for this_ax in ax: plt.sca(this_ax) plt.axis('off') # initialization function: plot the background of each frame def init(): line.set_data([], []) dot.set_data([], []) marker1.set_data([], []) marker2.set_data([], []) var1.set_data([], []) var2.set_data([], []) return dot, line, var1, var2, marker1, marker2 # animation function. This is called sequentially
def param_plot(x, func, params, **kwargs): """ Plot a function with user-adjustable parameters. Parameters ---------- x : array_like Independent (x-axis) variable. func : function Function that takes as its first argument an independent variable and as subsequent arguments takes parameters. It should return an output array the same dimension as `x`, which is plotted as the y-variable. params : dict Dictionary whose keys are strings named exactly as the parameter arguments to `func` are. [More info on options] Returns ------- final_params : dict A dict whose keys are the same as `params` and whose values correspond to the values selected by the slider. `final_params` will continue to change until the figure is closed, at which point it has the final parameter values the user chose. This is useful for hand-fitting curves. """ params = _initialize_params(params) flat_params = {k: _unitify(v, v['init']) for k, v in params.iteritems()} # Set up figure and axes that we'll plot on fig = plt.figure() ax0 = fig.add_axes([0, 0, 1, 1]) l, = ax0.plot(x, func(x, **flat_params), **kwargs) # Function for redrawing curve when a parameter value is changed def update(val): param_args = { k: _unitify(v, v['slider'].val) for k, v in params.iteritems() } l.set_ydata(func(x, **param_args)) fig.canvas.draw_idle() # Update values in flat_params so end user can get their final values flat_params.update(param_args) # Create axes and sliders for each parameter for i, (name, vals) in enumerate(params.iteritems()): ax = plt.axes(_slider_bbox(i).bounds) slider = Slider(ax, vals['label'], vals['min'], vals['max'], vals['init']) slider.on_changed(update) vals['ax'] = ax vals['slider'] = slider # Function for auto-scaling sliders to (ironically) keep them fixed def resize_func(event): ax0.set_position(_main_ax_bbox(ax0, len(params), fig).bounds) for i, val in enumerate(params.itervalues()): box = _slider_bbox(i) val['ax'].set_position(box) fig.canvas.mpl_connect('resize_event', resize_func) # Set current axes to the one you'd expect plt.sca(ax0) return flat_params
def state_vars_timeseries(rec, modelspec, ax=None, state_colors=None, decimate_by=1, channel=None): if ax is not None: plt.sca(ax) pred = rec['pred'] resp = rec['resp'] fs = resp.fs chanidx = get_channel_number(resp, channel) r1 = resp.as_continuous()[chanidx, :].T * fs p1 = pred.as_continuous()[chanidx, :].T * fs nnidx = np.isfinite(p1) r1 = r1[nnidx] p1 = p1[nnidx] if decimate_by > 1: r1 = scipy.signal.decimate(r1, q=decimate_by, axis=0) p1 = scipy.signal.decimate(p1, q=decimate_by, axis=0) fs /= decimate_by t = np.arange(len(r1)) / fs plt.plot(t, r1, linewidth=1, color='gray') plt.plot(t, p1, linewidth=1, color='black') mmax = np.nanmax(p1) * 0.8 if 'state' in rec.signals.keys(): s = None g = None d = None for m in modelspec: if 'state_dc_gain' in m['fn']: g = np.array(m['phi']['g']) d = np.array(m['phi']['d']) if len(g) < 10: s = ",".join(rec["state"].chans) g_string = np.array2string(g, precision=3) d_string = np.array2string(d, precision=3) s += " g={} d={} ".format(g_string, d_string) else: s = None num_vars = rec['state'].shape[0] ts = rec['state'].as_continuous().copy() if state_colors is None: state_colors = [None] * num_vars print(nnidx.shape) print(ts.shape) for i in range(1, num_vars): st = ts[i, :].T if decimate_by > 1: st = scipy.signal.decimate(st[nnidx], q=decimate_by, axis=0) else: st = st[nnidx] st = st / np.nanmax(st) * mmax - (0.1 + i) * mmax plt.plot(t, st, linewidth=1, color=state_colors[i - 1]) if g is not None: if g.ndim == 1: tstr = "{} (d={:.3f},g={:.3f})".format( rec['state'].chans[i], d[i], g[i]) else: tstr = "{} (d={:.3f},g={:.3f})".format( rec['state'].chans[i], d[0, i], g[0, i]) else: tstr = "{}".format(rec['state'].chans[i]) plt.text(t[0], (-i + 0.1) * mmax, tstr) ax = plt.gca() # plt.text(0.5, 0.9, s, transform=ax.transAxes, # horizontalalignment='center') # if s: # plt.title(s, fontsize=8) plt.xlabel('time (s)') plt.axis('tight') ax_remove_box(ax)
def test_rupture_depth(interactive=False): DIP = 17.0 WIDTH = 20.0 GRIDRES = 0.1 names = [ 'single', 'double', 'triple', 'concave', 'concave_simple', 'ANrvSA' ] means = [ 3.1554422780092461, 2.9224454569459781, 3.0381968625073563, 2.0522694624400271, 2.4805390352818755, 2.8740121776209673 ] stds = [ 2.1895293825074575, 2.0506459673526174, 2.0244588429154402, 2.0112565876976416, 2.1599789955270019, 1.6156220309120068 ] xp0list = [ np.array([118.3]), np.array([10.1, 10.1]), np.array([10.1, 10.1, 10.3]), np.array([10.9, 10.5, 10.9]), np.array([10.9, 10.6]), np.array([ -76.483, -76.626, -76.757, -76.99, -77.024, -76.925, -76.65, -76.321, -75.997, -75.958 ]) ] xp1list = [ np.array([118.3]), np.array([10.1, 10.3]), np.array([10.1, 10.3, 10.1]), np.array([10.5, 10.9, 11.3]), np.array([10.6, 10.9]), np.array([ -76.626, -76.757, -76.99, -77.024, -76.925, -76.65, -76.321, -75.997, -75.958, -76.006 ]) ] yp0list = [ np.array([34.2]), np.array([34.2, 34.5]), np.array([34.2, 34.5, 34.8]), np.array([34.2, 34.5, 34.8]), np.array([35.1, 35.2]), np.array([ -52.068, -51.377, -50.729, -49.845, -49.192, -48.507, -47.875, -47.478, -47.08, -46.422 ]) ] yp1list = [ np.array([34.5]), np.array([34.5, 34.8]), np.array([34.5, 34.8, 35.1]), np.array([34.5, 34.8, 34.6]), np.array([35.2, 35.4]), np.array([ -51.377, -50.729, -49.845, -49.192, -48.507, -47.875, -47.478, -47.08, -46.422, -45.659 ]) ] for i in range(0, len(xp0list)): xp0 = xp0list[i] xp1 = xp1list[i] yp0 = yp0list[i] yp1 = yp1list[i] name = names[i] mean_value = means[i] std_value = stds[i] zp = np.zeros(xp0.shape) strike = azimuth(xp0[0], yp0[0], xp1[-1], yp1[-1]) widths = np.ones(xp0.shape) * WIDTH dips = np.ones(xp0.shape) * DIP strike = [strike] origin = Origin({ 'id': 'test', 'lon': 0, 'lat': 0, 'depth': 5.0, 'mag': 7.0, 'netid': 'us', 'network': '', 'locstring': '', 'time': HistoricTime.utcfromtimestamp(time.time()) }) rupture = QuadRupture.fromTrace(xp0, yp0, xp1, yp1, zp, widths, dips, origin, strike=strike) # make a grid of points over both quads, ask for depths ymin = np.nanmin(rupture.lats) ymax = np.nanmax(rupture.lats) xmin = np.nanmin(rupture.lons) xmax = np.nanmax(rupture.lons) xmin = np.floor(xmin * (1 / GRIDRES)) / (1 / GRIDRES) xmax = np.ceil(xmax * (1 / GRIDRES)) / (1 / GRIDRES) ymin = np.floor(ymin * (1 / GRIDRES)) / (1 / GRIDRES) ymax = np.ceil(ymax * (1 / GRIDRES)) / (1 / GRIDRES) geodict = GeoDict.createDictFromBox(xmin, xmax, ymin, ymax, GRIDRES, GRIDRES) nx = geodict.nx ny = geodict.ny depths = np.zeros((ny, nx)) for row in range(0, ny): for col in range(0, nx): lat, lon = geodict.getLatLon(row, col) depth = rupture.getDepthAtPoint(lat, lon) depths[row, col] = depth np.testing.assert_almost_equal(np.nanmean(depths), mean_value) np.testing.assert_almost_equal(np.nanstd(depths), std_value) if interactive: fig, axes = plt.subplots(nrows=2, ncols=1) ax1, ax2 = axes xdata = np.append(xp0, xp1[-1]) ydata = np.append(yp0, yp1[-1]) plt.sca(ax1) plt.plot(xdata, ydata, 'b') plt.sca(ax2) im = plt.imshow(depths, cmap='viridis_r') # noqa ch = plt.colorbar() # noqa fname = os.path.join(os.path.expanduser('~'), 'quad_%s_test.png' % name) print('Saving image for %s quad test... %s' % (name, fname)) plt.savefig(fname) plt.close()
def state_var_psth_from_epoch(rec, epoch, psth_name='resp', psth_name2='pred', state_sig='pupil', ax=None, colors=None, channel=None, decimate_by=1): """ Plot PSTH averaged across all occurences of epoch, grouped by above- and below-average values of a state signal (state_sig) """ # TODO: Does using epochs make sense for these? if ax is not None: plt.sca(ax) chanidx = get_channel_number(rec[psth_name], channel) fs = rec[psth_name].fs d = rec[psth_name].get_epoch_bounds('PreStimSilence') PreStimSilence = np.mean(np.diff(d)) - 0.5 / fs d = rec[psth_name].get_epoch_bounds('PostStimSilence') if d.size > 0: PostStimSilence = np.min(np.diff(d)) - 0.5 / fs dd = np.diff(d) dd = dd[dd > 0] else: dd = np.array([]) if dd.size > 0: PostStimSilence = np.min(dd) - 0.5 / fs else: PostStimSilence = 0 full_psth = rec[psth_name] channel = 0 folded_psth = full_psth.extract_epoch(epoch)[:, [chanidx], :] * fs if psth_name2 is not None: full_psth2 = rec[psth_name2] folded_psth2 = full_psth2.extract_epoch(epoch)[:, [chanidx], :] * fs if state_sig == "each_passive": raise ValueError("each_passive state not supported") # extract high (=1) epochs from each passive state else: full_var = rec['state'].loc[state_sig] folded_var = full_var.extract_epoch(epoch) # remove masked out occurences if mask signal exists if 'mask' in rec.signals.keys(): folded_mask = rec['mask'].extract_epoch(epoch) keep_occurences = folded_mask[:, 0, 0] folded_psth = folded_psth[keep_occurences, :, :] folded_psth2 = folded_psth2[keep_occurences, :, :] folded_var = folded_var[keep_occurences, :, :] # print(state_sig) # print(folded_var.shape) # print(folded_mask.shape) # print(np.sum(np.isfinite(folded_mask))) if decimate_by > 1: folded_psth = scipy.signal.decimate(folded_psth, q=decimate_by, axis=2) folded_psth2 = scipy.signal.decimate(folded_psth2, q=decimate_by, axis=2) fs /= decimate_by # compute the mean state for each occurrence m = np.nanmean(folded_var[:, 0, :], axis=1) # compute the mean state across all occurrences mean = np.nanmean(m) gtidx = (m >= mean) ltidx = np.logical_not(gtidx) # low = response on epochs when state less than mean if np.sum(ltidx): low = np.nanmean(folded_psth[ltidx, :, :], axis=0).T low2 = np.nanmean(folded_psth2[ltidx, :, :], axis=0).T else: low = np.ones(folded_psth[0, :, :].shape).T * np.nan low2 = np.ones(folded_psth2[0, :, :].shape).T * np.nan # high = response on epochs when state greater than or equal to mean if np.sum(gtidx): high = np.nanmean(folded_psth[gtidx, :, :], axis=0).T high2 = np.nanmean(folded_psth2[gtidx, :, :], axis=0).T else: high = np.ones(folded_psth[0, :, :].shape).T * np.nan high2 = np.ones(folded_psth2[0, :, :].shape).T * np.nan title = state_sig hv = np.nanmean(m[m >= mean]) if np.sum(m < mean) > 0: lv = np.nanmean(m[m < mean]) if (hv > 0.95) and (hv < 1.05) and (lv > -0.05) and (lv < 0.05): legend = ('Lo', 'Hi') else: legend = ('< Mean', '>= Mean') timeseries_from_vectors([low, high], fs=fs, title=title, ax=ax, legend=legend, time_offset=PreStimSilence, colors=colors, ylabel="sp/sec") timeseries_from_vectors([low2, high2], fs=fs, title=title, ax=ax, linestyle='--', time_offset=PreStimSilence, colors=colors, ylabel="sp/sec") else: timeseries_from_vectors([low, high], fs=fs, title=title, ax=ax, time_offset=PreStimSilence, colors=colors, ylabel="sp/sec") timeseries_from_vectors([low2, high2], fs=fs, title=title, ax=ax, linestyle='--', time_offset=PreStimSilence, colors=colors, ylabel="sp/sec") ylim = ax.get_ylim() xlim = ax.get_xlim() ax.plot(np.array([0, 0]), ylim, 'k--') ax.plot(np.array([xlim[1], xlim[1]]) - PostStimSilence, ylim, 'k--') if state_sig == 'baseline': ax.set_xlabel(epoch)
def generate_plot(partial_quota, number_of_instances, f, a0, a1, tmp_y2, tmp_x, tmp_y, blocks_x, start_point, Xtics, yearly_quota, x_start, finished, User_t, System_t, y_start2, y_end2, beginning_dt, nutzergraph, fig, x_end, Data, filter_n, months): if filter_n: f.suptitle(str(filter_n), fontweight="bold") else: f.suptitle(str(Data[0]['Account'])[2:-1], fontweight="bold") global daily_eff_days global daily_eff_eff fmt = "%Y-%m-%d-%H-%M" # standard format for Dates, year month, day, hour, minute myFmt = mdates.DateFormatter('%b %y') nothing = mdates.DateFormatter(' ') monthly_cputime = [] monthly_used = [] effarray = [] tmp_y2.append(tmp_y[-1]) if partial_quota: #### drawing of the quota#### for i in range(0, number_of_instances ): # not possible for the last area, hence skipping it. col = colorization( D_.find_y_from_given_time( datetime.datetime.fromtimestamp(blocks_x[i * 2 + 1]), tmp_x, tmp_y) - D_.find_y_from_given_time( datetime.datetime.fromtimestamp(blocks_x[i * 2]), tmp_x, tmp_y), partial_quota) coordinates_x = (datetime.datetime.fromtimestamp(blocks_x[i * 2]), datetime.datetime.fromtimestamp(blocks_x[i * 2]), datetime.datetime.fromtimestamp(blocks_x[i * 2 + 1])) coordinates_y = [ tmp_y2[i * 2], tmp_y2[i * 2 + 1], tmp_y2[i * 2 + 1] ] a0.fill_between(coordinates_x, 0, coordinates_y, color=col, alpha=0.99) monthly_cputime.append(tmp_y2[i * 2 + 1] - tmp_y2[i * 2]) value1 = D_.find_y_from_given_time( datetime.datetime.fromtimestamp(blocks_x[-1]), tmp_x, tmp_y) value2 = D_.find_y_from_given_time( datetime.datetime.fromtimestamp(blocks_x[-2]), tmp_x, tmp_y) col = colorization(value1 - value2, partial_quota) coordinates_x = (datetime.datetime.fromtimestamp(blocks_x[-2]), datetime.datetime.fromtimestamp(blocks_x[-2]), datetime.datetime.fromtimestamp(blocks_x[-1])) coordinates_y = (value2, value2 + partial_quota, value2 + partial_quota) a0.fill_between(coordinates_x, 0, coordinates_y, color=col, alpha=0.99) # determines the last interval's color and draws it (uses the highest # recorded value as the end value of the ongoing time span). axis = plt.gca() # for plotting/saving the plot as it's own image # Sets the visual borders for the graphs; area of occurring values (main graph) +- 5%. if start_point: # setting the beginning and end of the graph beginning = start_point.timestamp() end = start_point.timestamp() + 365 * 24 * 3600 beginning = beginning - 30 * 24 * 3600 end = end + 30 * 24 * 3600 extrapolation_x = [] extrapolation_y = [] if len(tmp_y2) < 3: tmp_y2.append(0) tmp_y2.append(0) usedmonths = 0 usedmonths += 12 * (int(x_end.strftime("%Y")) - int(x_start.strftime("%Y"))) usedmonths += int(x_end.strftime("%m")) - int(x_start.strftime("%m")) monthsleft = int(months - usedmonths) if yearly_quota and len(tmp_x) >= 1: extrapolation_point_x = D_.first_of_month(x_end) extrapolation_point_y = D_.find_y_from_given_time( tmp_x[-1], tmp_x, tmp_y) extrapolation_point_y = max( extrapolation_point_y, D_.find_y_from_given_time(D_.first_of_month(extrapolation_point_x), tmp_x, tmp_y) + partial_quota) extrapolation_x.append(D_.first_of_month(extrapolation_point_x)) extrapolation_x.append(D_.first_of_month(extrapolation_point_x)) extrapolation_x.append( D_.first_of_month( datetime.datetime.fromtimestamp( extrapolation_point_x.timestamp() + 2851200))) extrapolation_y.append( D_.find_y_from_given_time(extrapolation_point_x, tmp_x, tmp_y)) extrapolation_y.append( max(extrapolation_y[0] + partial_quota, tmp_y[-1])) extrapolation_y.append(extrapolation_y[-1]) expoint_y = extrapolation_y[-1] extrapolation_y[-2] = tmp_y[-1] extrapolation_y[-1] = tmp_y[-1] expoint_y = extrapolation_y[-1] xtr_pt_x = extrapolation_point_x xtr_pt_y = extrapolation_point_y for i in range(1, monthsleft): # The three points required for each block extrapolation_x.append( D_.first_of_month( datetime.datetime.fromtimestamp(xtr_pt_x.timestamp() + i * 2851200))) extrapolation_x.append( D_.first_of_month( datetime.datetime.fromtimestamp(xtr_pt_x.timestamp() + i * 2851200))) extrapolation_x.append( D_.first_of_month( datetime.datetime.fromtimestamp(xtr_pt_x.timestamp() + (i + 1) * 2851200))) extrapolation_y.append(expoint_y + (i - 1) * partial_quota) extrapolation_y.append(expoint_y + i * partial_quota) extrapolation_y.append(expoint_y + i * partial_quota) if monthsleft: a0.plot(extrapolation_x[3:], extrapolation_y[3:], "black") if monthsleft: extrapolation_y.append(0) else: extrapolation_y = [0] beg_14_months = beginning + 36817200 fourteen_dt = datetime.datetime.fromtimestamp(beg_14_months) # Print statements, to give feedback either onscreen or into a dedicated file to be piped into. print('The accumulated TotalCPU time is', int((User_t[-1] + System_t[-1]) * 100) / 100, "hours") print('and the number of accumulated corehours is', int(tmp_y[-1] * 100) / 100) efficiency = (User_t[-1] + System_t[-1]) / tmp_y[-1] # Added rounding to the efficiency percentage feedback. print('Which results in an efficiency of', int(efficiency * 10000) / 100 + 0.005, "%") if efficiency < 0 or efficiency > 1: print( "Efficiency is outside of it's boundaries, valid is only between 0 and 1" ) accum_total_time = np.zeros(len(tmp_x)) for i in range(0, len(accum_total_time)): accum_total_time[i] = User_t[i] + System_t[i] delta = [0] total_time = [] total_time.append(accum_total_time[0]) total_time.append(accum_total_time[0]) difference = [0] for i in range(1, len(accum_total_time)): total_time.append(accum_total_time[i] - accum_total_time[i - 1]) delta.append(100 * ((accum_total_time[i] - accum_total_time[i - 1]) / (tmp_y[i] - tmp_y[i - 1]))) if delta[i] > 100: a = 0 if yearly_quota: # ensuring that the extrapolated quota is still in frame a0.set_ylim([ y_start2 - (0.05 * y_end2), max(tmp_y[-1], max(extrapolation_y), max(coordinates_y)) * 1.2 ]) # print("limit",a0.get_ylim()[1]) else: # No quota given, image is focused around occupied and utilized resources. print("NO YEARLY DETECTED") a0.set_ylim([y_start2 - (0.05 * y_end2), tmp_y[-1] * 1.05]) ##### Creation of patches for Legend ##### red_patch = mpatches.Patch(color='#ff0000', alpha=0.7, label='>=150%') orange_patch = mpatches.Patch(color='#ffa500', alpha=0.7, label='>=110%,<150%') green_patch = mpatches.Patch(color='#008000', alpha=0.8, label='>=70%,<110%') light_green_patch = mpatches.Patch(color='#81c478', alpha=0.8, label='<70%') grey_patch = mpatches.Patch(color='dimgrey', alpha=0.75, label='Allocated corehours') yellow_patch = mpatches.Patch(color='#d9e72e', alpha=0.49, label='Utilized corehours') black_patch = mpatches.Patch(color='black', alpha=1, label='Granted corehours') a0.plot(tmp_x, accum_total_time, '#d9e72e') # plotting the TotatlCPU Graph if yearly_quota: # Legends for if there is a quota, or a shorter Legend in case there isn't. a0.legend(handles=[ red_patch, orange_patch, green_patch, light_green_patch, grey_patch, yellow_patch, black_patch ]) else: a0.legend(handles=[grey_patch, yellow_patch]) a0.fill_between(tmp_x, 0, accum_total_time, color='#d9e72e', alpha=0.70) # plotting the area below TotalCPU graph a0.plot(tmp_x, tmp_y, 'dimgrey', fillstyle='bottom', alpha=0.75) # plotting the main graph (cores * hours) a0.fill_between(tmp_x, 0, tmp_y, color="grey", alpha=0.45) # plotting the area below the corehours graph for i in range(len(accum_total_time), number_of_instances * 3 + 4): # ensuring that empty months will be accounted for accum_total_time = np.append( accum_total_time, accum_total_time[-1]) # filling accumulated time with most recent if yearly_quota: for i in range(0, int(number_of_instances) ): # not possible for the last area, hence skipping it. monthly_used.append(accum_total_time[i * 3 + 3] - accum_total_time[i * 3]) percentages = [0] for i in range(len(monthly_cputime)): if monthly_used[i] >= 1: percentages.append(10 * (monthly_cputime[i] / monthly_used[i])) for i in range(len(percentages)): effarray.append(percentages[i]) a0.grid(True) axis2 = fig.add_subplot(212) a1legend1 = mpatches.Patch(color='Red', alpha=0.8, label="per day") a1legend2 = mpatches.Patch(color='purple', alpha=0.8, label='per job') a1.plot(tmp_x, delta, '.', color="purple", markersize=5, alpha=0.35) # percentages amplified by the lower bound to a1.legend(handles=[a1legend1, a1legend2]) plt.ylabel('Efficiency') # be more visible. daily = [] dates = [] for i in range(int(x_start.timestamp()), int(x_end.timestamp()), 2764800): r = D_.gather_efficiencies_for_month( datetime.datetime.fromtimestamp(i), Data) daily_eff_days = r[-2] daily_eff_eff = r[-1] r = r[:-2] for j in range(len(r[0])): if r[1][j] > 0: daily.append(100 * r[1][j] / r[0][j]) dates.append(r[2][j]) formatteddates = [] for i in dates: if len(str(i)) > 5 and "." not in str(i): transp = str(i)[2:18] formatteddates.append(datetime.datetime.strptime(transp, fmt)) eff_days = [] #for i in dates: # eff_days.append(datetime.datetime.strptime(str(i)[2:18], fmt)) a1.plot(formatteddates, daily, '.', color="Red", markersize=3, alpha=0.85) eff_distance = 0 - axis.get_ylim()[0] a1.grid( True ) # Creates a grid in the image to aid the viewer in visually processing the data. a1.set_ylim([-5, 105]) if nutzergraph: a1.set_ylim( [0, 100]) # Usergraphs don't display anything above 100% or below 0%. a1.set_yticks( np.arange(0, 101, 10), minor=True) # minor tick-lines are much thinner than regular ones a1.set_yticks(np.arange(0, 101, 25)) a1.yaxis.set_major_formatter(mtick.PercentFormatter()) plt.xlabel('Efficiency') plt.xlabel(' ') a0.xaxis.tick_top() a0.set_xlim((beginning_dt, fourteen_dt)) a1.xlim = (beginning_dt, fourteen_dt) plt.sca(a0) a0.yaxis.set_major_formatter(ScalarFormatter(useOffset=True)) plt.xticks(Xtics) plt.ylabel('CPUhours') emptylabels = [] for i in a0.get_xticklabels(): emptylabels.append(["", ""]) new_ylabels, unit = get_scaled_ylabels(a0.get_yticks()) plt.ylabel("CPUhours (" + unit + ")") a0.set_xticklabels = emptylabels a0.set_yticklabels(new_ylabels) plt.sca(a1) # dictates gap in height, left border, right border, gap in width, bottom border, top border plt.subplots_adjust(hspace=0.03, left=0.1, right=0.925, wspace=0.07, bottom=0.035, top=0.95) plt.xlim(beginning_dt, fourteen_dt) plt.xticks(Xtics) a0.xaxis.set_major_formatter(nothing) # removes the x-tic notations a1.xaxis.set_major_formatter(myFmt) a1.grid(which='minor', alpha=0.2) a1.grid(which='major', alpha=0.5) f.set_size_inches((11, 8.5), forward=False) ## for png compression #ram = io.BytesIO() #plt.savefig(ram, format='png') #ram.seek(0) #im = Image.open(ram) #im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE) #return im2 #print(tmp_x) #print(tmp_y) return f
#T,X,Y=read_tffile(glob( DATA_ROOT_PATH+'/data/'+ERROR_VS_TIME_PREFIX+'*' )[0]) T,Y,X=read_tffile(glob( DATA_ROOT_PATH+'/data/'+ERROR_VS_TIME_PREFIX+'*' )[0]) y_dnn=regressor.predict(input_fn=lambda :{'X': tf.convert_to_tensor(X, name='X'), 't': tf.convert_to_tensor(T, name = 't')}) logging.info('\tStart validation for '+model_name(parm)) L2_t0=dict() L2_t1=dict() for i,y in enumerate(y_dnn): if i >= len(X) or i >=NUM_TIMES*NUM_SAMPLES_FOR_ERR_VS_TIME: break if LEARN_INVERSE: y_pred_for_plot=np.array(y['predictions']) tim = T[i][0] if PLOT_ALL: fig, (ax1,ax2,ax3)=plt.subplots(3,1,figsize=(10,6), sharex=True) plt.sca(ax1) plt.title('t={:5f}'.format(float(0))) plt.plot(SAMPLING_SPACE , Y[i] , label='Real') plt.plot(SAMPLING_SPACE , y_pred_for_plot , label='Network') plt.ylabel('Heat profile') plt.legend(loc='best') plt.sca(ax2) plt.title('t={:5f}'.format(float(tim ))) plt.plot(SAMPLING_SPACE , X[i] , label='Real' ) plt.plot(SAMPLING_SPACE , apply_heat_kernel( y_pred_for_plot, tim ) , label='Network') plt.ylabel('Heat profile') plt.legend(loc='best') plt.sca(ax3) plt.plot(SAMPLING_SPACE , y_pred_for_plot-Y[i] , label=r'$\Delta(t=0)$') plt.plot(SAMPLING_SPACE , apply_heat_kernel( y_pred_for_plot, tim )-X[i] , label=r'$\Delta(t=$'+str(tim)+'$)$')
import numpy as np x_min, x_max = X_train[:, 0].min() - .5, X_train[:, 0].max() + .5 y_min, y_max = X_train[:, 1].min() - .5, X_train[:, 1].max() + .5 #error in case Xs or xs Xs = np.arange(x_min, x_max, 0.5) fig, axes = plt.subplots(1, 3) fig.set_size_inches(10, 6) for i in [0, 1, 2]: axes[i].set_aspect('equal') axes[i].set_title('Class ' + str(i) + ' versus the rest') axes[i].set_xlabel('Sepal length') axes[i].set_ylabel('Sepal width') axes[i].set_xlim(x_min, x_max) axes[i].set_ylim(y_min, y_max) #error here need plt. plt.sca(axes[i]) plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, cmap=plt.cm.prism) ys = (-clf.intercept_[i] - Xs * clf.coef_[i, 0]) / clf.coef_[i, 1] plt.plot(Xs, ys, hold=True) print(clf.predict(scaler.transform([[4.7, 3.1]]))) #[0] print(clf.decision_function(scaler.transform([[4.7, 3.1]]))) #[[ 19.73905808 8.13288449 -28.63499119]] from sklearn import metrics y_train_pred = clf.predict(X_train) print(metrics.accuracy_score(y_train, y_train_pred)) #0.821428571429
y = 4 * (X - 0.5)**2 y = y + np.random.randn(m, 1) / 10 tree_reg1 = DecisionTreeRegressor(random_state=42) tree_reg2 = DecisionTreeRegressor(random_state=42, min_samples_leaf=10) tree_reg1.fit(X, y) tree_reg2.fit(X, y) x1 = np.linspace(0, 1, 500).reshape(-1, 1) y_pred1 = tree_reg1.predict(x1) y_pred2 = tree_reg2.predict(x1) fig, axes = plt.subplots(ncols=2, figsize=(10, 4), sharey=True) plt.sca(axes[0]) plt.plot(X, y, "b.") plt.plot(x1, y_pred1, "r.-", linewidth=2, label=r"$\hat{y}$") plt.axis([0, 1, -0.2, 1.1]) plt.xlabel("$x_1$", fontsize=18) plt.ylabel("$y$", fontsize=18, rotation=0) plt.legend(loc="upper center", fontsize=18) plt.title("No restrictions", fontsize=14) plt.sca(axes[1]) plt.plot(X, y, "b.") plt.plot(x1, y_pred2, "r.-", linewidth=2, label=r"$\hat{y}$") plt.axis([0, 1, -0.2, 1.1]) plt.xlabel("$x_1$", fontsize=18) plt.title("min_samples_leaf={}".format(tree_reg2.min_samples_leaf), fontsize=14)
def plot_tree(t, **kwargs): """ Plots a tree (from module tree.py) kwargs that do something: nodecolors = color the nodes leafcolors = color just the leaf nodes title = set the title ax = plot on the given axis useplt = default true, if False, then ax must be specified and will plot directly to ax. """ node_locs = np.zeros([t.tree_size, 2]) node_order = [] for level in xrange(1, t.tree_depth + 1): nodes = t.dfs_level(level) node_order.extend([x.idx for x in nodes]) node_idxs = np.array([node.idx for node in nodes]) x_intervals = np.cumsum( np.array([0] + [node.size for node in nodes]) * 1.0 / t.size) node_xs = x_intervals[:-1] + np.diff(x_intervals) / 2.0 node_ys = (t.tree_depth - level) * np.ones(np.shape(node_xs)) node_locs[node_idxs, :] = np.hstack( [node_xs[:, np.newaxis], node_ys[:, np.newaxis]]) useplt = True if "useplt" in kwargs: if not kwargs["useplt"]: useplt = False ax = kwargs["ax"] if "ax" in kwargs: if useplt: plt.sca(kwargs["ax"]) if "nodecolors" in kwargs: nc = kwargs["nodecolors"] if useplt: plt.scatter(node_locs[:, 0], node_locs[:, 1], marker='.', edgecolors='none', c=nc, norm=binnorm, cmap=rbmap, s=80) else: ax.scatter(node_locs[:, 0], node_locs[:, 1], marker='.', edgecolors='none', c=nc, norm=binnorm, cmap=rbmap, s=80) elif "leafcolors" in kwargs: lc = kwargs["leafcolors"] nonleaves = (t.tree_size - t.size) nc = ['k'] * nonleaves if useplt: plt.scatter(node_locs[0:nonleaves, 0], node_locs[0:nonleaves, 1], edgecolors='none', marker='.', c=nc, s=80) plt.scatter(node_locs[nonleaves:, 0], node_locs[nonleaves:, 1], edgecolors='none', marker='.', c=lc, s=80) else: ax.scatter(node_locs[0:nonleaves, 0], node_locs[0:nonleaves, 1], edgecolors='none', marker='.', c=nc, s=80) ax.scatter(node_locs[nonleaves:, 0], node_locs[nonleaves:, 1], edgecolors='none', marker='.', c=lc, s=80) else: nc = 'k' if useplt: plt.scatter(node_locs[:, 0], node_locs[:, 1], marker='.', c=nc, s=80) else: ax.scatter(node_locs[:, 0], node_locs[:, 1], marker='.', c=nc, s=80) for node in t: if node.parent is not None: x1, y1 = node_locs[node.idx, :] x2, y2 = node_locs[node.parent.idx, :] if useplt: plt.plot((x1, x2), (y1, y2), 'r') else: ax.plot((x1, x2), (y1, y2), 'r') if useplt: plt.yticks(np.arange(0, t.tree_depth, 1)) plt.xlim([0.0, 1.0]) plt.ylim([-0.2, (t.tree_depth - 1) + 0.2]) else: ax.set_yticks(np.arange(0, t.tree_depth, 1)) ax.set_xlim([0.0, 1.0]) ax.set_ylim([-0.2, (t.tree_depth - 1) + 0.2]) if "title" in kwargs: if useplt: plt.title(kwargs["title"]) else: ax.set_title(kwargs["title"]) if "nodelocs" in kwargs: return node_locs
gape_array = hf5.root.ancillary_analysis.gapes[:] ltp_array = hf5.root.ancillary_analysis.ltps[:] gape_array_long = gape_array.reshape(\ (gape_array.shape[0],np.prod(gape_array.shape[1:3]),gape_array.shape[-1])) #gape_array_long = gape_array_long[:,:,2000:4500] gape_array_really_long = gape_array.reshape(\ (np.prod(gape_array.shape[:3]),gape_array.shape[3])) ltp_array_long = ltp_array.reshape(\ (ltp_array.shape[0],np.prod(ltp_array.shape[1:3]),ltp_array.shape[-1])) ltp_array_long = ltp_array_long[:, :, 2000:4500] fig, ax = plt.subplots(2, 2) plt.sca(ax[0, 0]) dat_imshow(gape_array_long[0, :, :]) plt.sca(ax[0, 1]) dat_imshow(gape_array_long[1, :, :]) plt.sca(ax[1, 0]) dat_imshow(ltp_array_long[0, :, :]) plt.sca(ax[1, 1]) dat_imshow(ltp_array_long[1, :, :]) plt.show() # Find bout length this_array = gape_array_long[0, :, :] def calc_bout_duration(gape_array): gape_array[gape_array < 0.5] = 0
if ts[0] < 2: aux = ax.imshow(ts[1], origin='lower', zorder=5, cmap=current_cmap) else: aux = ax.imshow(ts[1], origin='lower', zorder=5, cmap=current_cmap2) ax.plot(_bo_xs[:, 0], _bo_xs[:, 1], '^y', zorder=10) ax.plot(i_pos[0], i_pos[1], '^y', zorder=10) CS = ax.contour(ts[1], colors='k', alpha=0.6, linewidths=1.3, zorder=10) divider = make_axes_locatable(ax) cax = divider.append_axes('right', size='5%', pad=0.05) fig.colorbar(aux, cax=cax, orientation='vertical') ax.grid(True, zorder=0, color="white") ax.clabel(CS, inline=1, fontsize=12) ax.set_facecolor('#eaeaf2') ax.set_xlabel("x (m)", fontsize=17) ax.set_xticks(xticks) ax.set_xticklabels(xnticks, fontsize=17) ax.set_yticks(yticks) ax.set_yticklabels(ynticks, fontsize=0) if ts[0] == 0: ax.set_title("$\\alpha_1(x)$") elif ts[0] == 1: ax.set_title("$\\alpha_2(x)$") elif ts[0] == 2: ax.set_title("$\\sigma_1(x)$") else: ax.set_title("$\\sigma_2(x)$") plt.sca(axs[0]) plt.yticks(yticks, labels=ynticks, fontsize=17) plt.ylabel("y (m)", fontsize=17) plt.show(block=True)
def elements(elem,*args,**kwargs): """ NAME: elements PURPOSE: make a plot of measurements of the elemental abundances vs. atomic number INPUT: elem - dictionary with elemental abundances relative to H wrtFe= (True) if True, plot elements wrt Fe on the left Y inclwrtH= (True) if True, indicate what X/H is on the right Y bovy_plot.bovy_plot args and kwargs OUTPUT: plot to output HISTORY: 2015-03-10 - Written - Bovy (IAS) """ # Process the input dictionary xs= [] names= [] ys= [] wrtFe= kwargs.pop('wrtFe',True) for el in elem: try: xs.append(atomic_number(el)) except KeyError: # ignore things that aren't known elements continue names.append(r'$\mathrm{%s}$' % el.lower().capitalize()) try: if not wrtFe: raise KeyError ys.append(elem[el]-elem['Fe']) except KeyError: ys.append(elem[el]) wrtFe= False xs= numpy.array(xs,dtype='int') ys= numpy.array(ys) names= numpy.array(names) # sort sindx= numpy.argsort(xs) xs= xs[sindx] ys= ys[sindx] names= names[sindx] # add second y axis? inclwrtH= kwargs.pop('inclwrtH',True) if wrtFe: feh= elem['Fe'] ylabel= kwargs.pop('ylabel',r'$[\mathrm{X/Fe}]$') else: ylabel= kwargs.pop('ylabel',r'$[\mathrm{X/H}]$') if not kwargs.get('overplot',False): bovy_plot.bovy_print(fig_width=7.,fig_height=4.) basezorder= kwargs.pop('zorder',0) yrange=kwargs.pop('yrange',[-0.5,0.5]) ls= kwargs.pop('ls','-') lw= kwargs.pop('lw',0.25) bovy_plot.bovy_plot(xs,ys,*args, ylabel=ylabel, xrange=[4,numpy.amax(xs)+2], yrange=yrange,zorder=2+basezorder,ls=ls,lw=lw, **kwargs) pyplot.xticks(list(xs),names) pyplot.tick_params(axis='x',labelsize=11.) if wrtFe and inclwrtH: bovy_plot.bovy_plot([4,numpy.amax(xs)+2],[0.,0.],'-',lw=2., color='0.65',overplot=True,zorder=basezorder) ax= pyplot.gca() ax2= ax.twinx() ax2.set_ylim(yrange[0]+feh,yrange[1]+feh) ax2.set_ylabel(r'$[\mathrm{X/H}]$') pyplot.sca(ax2) bovy_plot._add_ticks(yticks=True,xticks=False) return None
def __init__(self, data): """ class initializer """ # store relevant dictionary entries into convenient local variables self.ultra_interp = data['ultra_interp'] self.ofdisp = data['ofdisp'] self.ult_no_frames = data['ult_no_frames'] self.ult_time = data['ult_time'] self.ult_period = self.ult_time[1] - self.ult_time[0] self.wav_time = data['wav_time'] self.wav_data = data['wav_data'] / max(abs(data['wav_data'])) self.wav_fs = data['wav_fs'] self.wav_num_samples = len(self.wav_data) # Visualization options self.flow_dir = FlowDir.Horizontal self.flow_polarity = 1 self.flow_half_field = 0 self.flow_dir_label = 'Horizontal' self.flow_pol_label = '' self.skip_increment = 3 self.pos_ylim_del = 1 self.vel_ylim_del = 10 self.pos_ylim_min = 1 self.pos_ylim_max = 200 self.vel_ylim_min = 10 self.vel_ylim_max = 2000 # TODO implement vector scaling as a user parameter self.scaling = 1.0 # visualize registration as quiver plot self.quiver_x_range = range(1, self.ultra_interp[0].shape[0]) self.quiver_y_range = range(1, self.ultra_interp[0].shape[1]) self.xx, self.yy = np.meshgrid(self.quiver_x_range, self.quiver_y_range) self.x_indices_full, self.y_indices_full = np.meshgrid( np.arange(0, self.xx.shape[0]), np.arange(0, self.xx.shape[1])) self.x_indices_half, self.y_indices_half = np.meshgrid( np.arange(0, int(self.xx.shape[0] / 2)), np.arange(0, self.xx.shape[1])) self.x_indices_plot, self.y_indices_plot = np.meshgrid( np.arange(0, self.xx.shape[0], self.skip_increment), np.arange(0, self.xx.shape[1], self.skip_increment)) self.quiver_colors = np.empty(shape=np.shape(self.x_indices_plot)) self.quiver_colors.fill(1) # TODO: A lazy hack to force the quiver colormapping to work (it autoscales to the values) self.quiver_colors[0, 0] = 0 self.quiver_plot_halfsize = math.ceil( self.x_indices_plot.shape[0] / 2) * self.x_indices_plot.shape[1] self.x_indices = self.x_indices_full self.y_indices = self.y_indices_full #TODO set the default figure size to be some sensible proportion of the screen real estate self.fig = plt.figure(figsize=(10, 8)) self.ax_quiver = self.fig.add_axes([0.1, 0.6, 0.4, 0.4]) plt.sca(self.ax_quiver) #plt.set_cmap('Greys') self.im = self.ax_quiver.imshow(self.ultra_interp[0]) self.quiver = plt.quiver(self.yy[self.x_indices_plot, self.y_indices_plot], self.xx[self.x_indices_plot, self.y_indices_plot], self.ofdisp[0]['of'][0][self.y_indices_plot, self.x_indices_plot], self.ofdisp[0]['of'][1][self.y_indices_plot, self.x_indices_plot], self.quiver_colors, cmap=cm.bwr, scale_units='xy', scale=0.5, angles='xy') # compute the velocity and position using the trimmed mean approach self.vel = np.empty((self.ult_no_frames - 1, 2)) self.pos = np.empty( (self.ult_no_frames - 2, 2) ) # integration of velocity leaves us with N - 1, we pad afterwards # Add a compass plot to visualize the consensus vector self.ax_compass = self.fig.add_axes([0.7, 0.7, 0.2, 0.2], projection='polar') self.ax_compass.set_ylim(0, 100) # create velocity plot self.ax_vel = self.fig.add_axes([0.1, 0.5, 0.8, 0.1]) self.line_vel, = plt.plot(self.ult_time[0:self.ult_no_frames - 1], np.zeros(self.ult_no_frames - 1) * np.nan) #self.vel[:, self.flow_dir.value]) plt.axhline(linewidth=1, color='k') # TODO: Setting the xlim changes the tick scaling but not the data scale self.ax_vel.set_xlim([0.0, self.ult_time[-1]]) self.ax_vel.set_ylim([-1e2, 1e2]) self.title_vel = self.ax_vel.set_title("Velocity (" + self.flow_dir_label + " in Video)") self.ax_vel.set_ylabel("velocity (mm/s)") self.ax_vel.tick_params(bottom=False) # create position plot self.ax_pos = self.fig.add_axes([0.1, 0.3, 0.8, 0.1]) self.line_pos, = plt.plot(self.ult_time[0:self.ult_no_frames - 1], np.zeros(self.ult_no_frames - 1) * np.nan) #self.pos[:, self.flow_dir.value]) plt.axhline(linewidth=1, color='k') # TODO: Setting the xlim changes the tick scaling but not the data scale self.ax_pos.set_xlim([0.0, self.ult_time[-1]]) self.ax_pos.set_ylim([-2e1, 2e1]) self.title_pos = self.ax_pos.set_title("Position (" + self.flow_dir_label + " in Video)") self.ax_pos.set_ylabel("relative position (mm)") self.ax_pos.set_xlabel("time (s)") # cache the axis for faster rendering # create audio plot self.ax_audio = self.fig.add_axes([0.1, 0.1, 0.8, 0.1]) self.line_audio, = plt.plot(self.wav_time, self.wav_data) self.ax_audio.set_xlim([0.0, self.ult_time[-1]]) self.ax_audio.set_ylim([-1, 1]) plt.axhline(linewidth=1, color='k') self.ax_audio.set_title("Audio") self.frame_index = 0 # connect the callbacks self.cid_scroll = self.fig.canvas.mpl_connect('scroll_event', self.mouse_scroll) self.fig.canvas.mpl_connect('key_press_event', self.key_press) # TODO: Need to stop execution upon closing the matplotlib figure (calling exit(0) via an on_close callback predictably generates an error) #self.fig.canvas.mpl_connect('close_event', self.on_close) # cache the axis for faster rendering self.fig.canvas.draw() self.ax_quiver_bg = self.fig.canvas.copy_from_bbox(self.ax_quiver.bbox) self.ax_vel_clear_bg = self.fig.canvas.copy_from_bbox(self.ax_vel.bbox) self.ax_pos_clear_bg = self.fig.canvas.copy_from_bbox(self.ax_pos.bbox) self.ax_audio_bg = self.fig.canvas.copy_from_bbox(self.ax_audio.bbox) self.ax_compass_bg = self.fig.canvas.copy_from_bbox( self.ax_compass.bbox) # compute the kinematics and update the filled plots self.pos, self.vel = compute_kinematics(self.ofdisp, self.ult_time, self.ult_no_frames, self.x_indices, self.y_indices, self.ult_period, self.scaling) self.line_vel.set_ydata(self.flow_polarity * self.vel[:, self.flow_dir.value]) self.line_pos.set_ydata(self.flow_polarity * self.pos[:, self.flow_dir.value]) self.fig.canvas.draw() self.ax_vel_filled_bg = self.fig.canvas.copy_from_bbox( self.ax_vel.bbox) self.ax_pos_filled_bg = self.fig.canvas.copy_from_bbox( self.ax_pos.bbox) # now that we have cached the background add the varying plot features (image, quiver, time markers) plt.sca(self.ax_quiver) self.quiver_frame_label = plt.text(1, self.quiver_y_range[-1] * 0.15, "1", fontsize=48, color="white") plt.sca(self.ax_vel) self.point_vel, = plt.plot(self.ult_time[0], self.vel[0, self.flow_dir.value], marker="o", ls="", color="r") plt.sca(self.ax_pos) self.point_pos, = plt.plot(self.ult_time[0], self.pos[0, self.flow_dir.value], marker="o", ls="", color="r") plt.sca(self.ax_audio) self.marker_line_audio, = plt.plot( [self.wav_time[0], self.wav_time[0]], [-1, 1], color="r") plt.sca(self.ax_compass) self.marker_line_compass, = plt.plot( [0, 1], [0, 1], color="r" ) #("", xy=(angle, radius), xytext=(0, 0), arrowprops=dict(arrowstyle="<-", color='k')) self.fig.canvas.draw() plt.show()
plt.scatter(X[:, 0], X[:, 1], c="k") plt.title("$Initial$", fontsize=18) plt.xlabel("X1", fontsize=15) plt.ylabel("X2", fontsize=15, rotation=0) plt.savefig("./K-means_Initial.png") plt.show() distances = distanceToCentroids(X, centroids) centroids, centroid_points = getNewCentroid(X, distances, num_centroids) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(14, 9)) for i in range(num_iterations): plt.sca(axes[i // 2, i % 2]) for idx, centroid in enumerate(centroids, 1): x1, x2 = [], [] for a, b in centroid_points[idx - 1]: x1.append(a) x2.append(b) plt.plot(centroid[0], centroid[1], marker="^", c=colors[idx - 1], markersize=12, label=f'k{idx}') plt.scatter(x1, x2, s=15, c=colors[idx - 1], label=f'Centroid: {idx}')
def update(self, event): if event.inaxes != self.axes4: return if event.xdata != None: x = (int(event.xdata)+self.imageWidth//2)%self.imageWidth y = (int(event.ydata)+self.imageHeight//2)%self.imageHeight plt.sca(self.axes5) plt.cla() waveImg = numpy.zeros((self.imageHeight,self.imageWidth)) waveImg[y,x] = 1 plt.imshow(numpy.real(fftpack.ifft2(waveImg)), cmap='gray') if not self.bCtrlPressed: bNeedUpdate = False if self.samples[y,x] != self.fftImage[y,x] and self.mouseButton == 1: #left button bNeedUpdate = True self.samples[y,x] = self.fftImage[y,x] self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,0] = 1 self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,3] = 1 elif self.samples[y,x] != numpy.complex(0.0,0.0) and self.mouseButton == 3: #right button bNeedUpdate = True self.samples[y,x] = numpy.complex(0.0,0.0) self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,0] = 0 self.samplePoints[(y-self.imageHeight//2)%self.imageHeight,(x-self.imageWidth//2)%self.imageWidth,3] = 0 if bNeedUpdate: plt.sca(self.axes4) plt.cla() p = plt.imshow(self.fftImageForPlot, cmap='gray') p.set_clim(self.fftMean-self.fftStd,self.fftMean+self.fftStd) plt.imshow(self.samplePoints) plt.sca(self.axes3) plt.cla() plt.imshow(numpy.real(fftpack.ifft2(self.samples)), cmap='gray') else: for xi in range(x-self.imageWidth//32, x+self.imageWidth//32): for yi in range(y-self.imageWidth//32, y+self.imageWidth//32): if xi>=self.imageWidth: xx = xi-self.imageWidth else: xx = xi if yi>=self.imageHeight: yy = yi-self.imageHeight else: yy = yi if self.mouseButton == 1: #left button self.samples[yy,xx] = self.fftImage[yy,xx] self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,0] = 1 self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,3] = 0.7 elif self.mouseButton == 3: #right button self.samples[yy,xx] = numpy.complex(0.0,0.0) self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,0] = 0 self.samplePoints[(yy-self.imageHeight//2)%self.imageHeight,(xx-self.imageWidth//2)%self.imageWidth,3] = 0 plt.sca(self.axes4) plt.cla() plt.imshow(self.samplePoints) plt.sca(self.axes3) plt.cla() plt.imshow(numpy.real(fftpack.ifft2(self.samples)), cmap='gray') self.fig.canvas.draw()
def ftAllSkyImage(allSkyImage, options, labelOptions, stationLocation, lVec, mVec): """Summary Args: allSkyImage (TYPE): Description options (TYPE): Description labelOptions (TYPE): Description stationLocation (TYPE): Description lVec (TYPE): Description mVec (TYPE): Description Returns: TYPE: Description """ plotOptions = options['plottingOptions'] logPlot, skyObjColor, gridThickness, backgroundColor, foregroundColor, radialLabelAngle, colorBar, obsSite, outputFolder, maxPercentile, minPercentile, figureShape, fontSizeFactor = plotOptions[ 'logPlot'], plotOptions['skyObjColor'], plotOptions[ 'gridThickness'], plotOptions['backgroundColor'], plotOptions[ 'foregroundColor'], plotOptions[ 'radialLabelAngle'], plotOptions['colorBar'], options[ 'stationID'], plotOptions['outputFolder'], plotOptions[ 'maxPercentile'], plotOptions[ 'minPercentile'], plotOptions[ 'figureShape'], plotOptions[ 'fontSizeFactor'] dateTime, rcuMode, subband, frequency, polarity, figNum = labelOptions if fontSizeFactor is None: fontSizeFactor = figureShape[0] / 18. if options['rfiMode']: fontSizeFactor = min(fontSizeFactor, 0.66) telescopeLoc = astropy.coordinates.EarthLocation( lon=stationLocation[0] * u.deg, lat=stationLocation[1] * u.deg, height=stationLocation[2] * u.m) if len(dateTime) == 15: dateTime = str(datetime.datetime.strptime(dateTime, '%Y%m%d-%H%M%S')) obsTime = astropy.time.Time(dateTime) if plotOptions['plotSkyObjects']: knownSources, referenceObject = getSkyObjects(options, obsTime, telescopeLoc) altAzRef = astropy.coordinates.AltAz(obstime=obsTime, location=telescopeLoc) altAzObjects = [ skyObj.transform_to(altAzRef) for skyObj in referenceObject ] gs = mpl.gridspec.GridSpec(1, 2, width_ratios=[18, 1]) gs2 = mpl.gridspec.GridSpec(1, 2, width_ratios=[18, 1]) fig = plt.figure(figNum, figsize=(figureShape[0], figureShape[1])) fig.patch.set_facecolor(backgroundColor) plt.suptitle( 'LOFAR mode {0}{1} all sky plot at {2}MHz (sb{3}) for {4}\n'.format( rcuMode, polarity, round(frequency / 1e6, 2), subband, obsSite), fontsize=int(28 * fontSizeFactor), color=foregroundColor) #, va = 'top') plt.rcParams["text.color"] = foregroundColor plt.rcParams["axes.labelcolor"] = foregroundColor plt.rcParams["xtick.color"] = foregroundColor plt.rcParams["ytick.color"] = foregroundColor plt.rcParams['axes.edgecolor'] = foregroundColor plt.rcParams['axes.linewidth'] = gridThickness axImage = fig.add_subplot(gs[0], label='ax_image') axImage.axis('off') global vmaxCache global vminCache if logPlot: allSkyImageLog = np.log10(allSkyImage) if isinstance(vmaxCache, float): vmaxVar = np.log10(vmaxCache) vminVar = np.log10(vminCache) else: vmaxVar = np.nanpercentile(allSkyImageLog, maxPercentile) vminVar = np.nanpercentile(allSkyImageLog, minPercentile) vminCache.append(vminVar) vmaxCache.append(vmaxVar) vminVar = np.mean(vminCache) vmaxVar = np.mean(vmaxCache) pltIm = axImage.imshow(allSkyImageLog, cmap='jet', label='ax_image', interpolation='lanczos', vmax=vmaxVar, vmin=vminVar) else: if isinstance(vmaxCache, float): vmaxVar = vmaxCache vminVar = vminCache else: vmaxVar = np.nanpercentile(allSkyImage, maxPercentile) vminVar = np.nanpercentile(allSkyImage, minPercentile) vminCache.append(vminVar) vmaxCache.append(vmaxVar) vminVar = np.mean(vminCache) vmaxVar = np.mean(vmaxCache) pltIm = axImage.imshow(allSkyImage, cmap='jet', label='ax_image', interpolation='lanczos', vmax=vmaxVar, vmin=vminVar) axImage.axis('off') if colorBar: axColorBar = plt.subplot(gs[1]) colorBarObj = plt.colorbar(pltIm, axColorBar) axColorBar.tick_params(which='minor', length=2) axColorBar.tick_params(which='major', length=4, width=1) axColorBar.yaxis.set_minor_locator(mpl.ticker.AutoMinorLocator(10)) if options['rfiMode']: cbCursor = axColorBar.plot([0, 1], [0, 0], 'k-') pltObj = fig.add_subplot(gs2[0], label='ax', polar=True) axImage.set_xlim((0, lVec.size)) axImage.set_ylim((lVec.size, 0)) pltObj.set_theta_zero_location("N") pltObj.set_theta_direction(1) if plotOptions['plotSkyObjects']: plotStatus = [[ True, __plotSkyObject(axImage, skyObj, lVec.size, skyObjColor, knownSources[idx], fontSizeFactor=fontSizeFactor) ] if (skyObj.alt.deg > 20.) or (options['rfiMode'] and skyObj.alt.deg > -10.) else [ False, __plotSkyObject(axImage, skyObj, lVec.size, skyObjColor, knownSources[idx], offset=True, fontSizeFactor=fontSizeFactor) ] for idx, skyObj in enumerate(altAzObjects)] legend = axImage.legend(loc=8, bbox_to_anchor=(0.5, -0.128), ncol=4, framealpha=0.0, fontsize=int(14 * fontSizeFactor), title=str(obsTime)[:-4]) legend.get_title().set_fontsize(str(int(22 * fontSizeFactor))) for idx, skyText in enumerate(legend.get_texts()): if not plotStatus[idx][0]: plt.setp(skyText, color='red') elif options['rfiMode'] and altAzObjects[ idx].alt.deg > -10. and altAzObjects[idx].alt.deg < 20.: plt.setp(skyText, color='orange') radii = [] if plotOptions['graticule']: for radius in range(0, 90, 15): # r grid at 15 degree intervals radii.append( 180 * np.cos(radius * np.pi / 180) ) # plot the radii so as to display as an orthographic grid pltObj.set_rgrids(radii) if radialLabelAngle: # you would not want to put y ticks on 0 anyhow as it would be messy yLabel = [ '', '15' + u'\xb0', '30' + u'\xb0', '45' + u'\xb0', '60' + u'\xb0', '75' + u'\xb0' ] pltObj.set_yticklabels(yLabel, color=foregroundColor) print(radialLabelAngle) pltObj.set_rlabel_position(radialLabelAngle) else: yLabel = [] pltObj.set_yticklabels(yLabel) thetaticks = np.arange(0, 360, 45) pltObj.set_thetagrids(thetaticks, weight='bold', color=foregroundColor, fontsize=int(18 * fontSizeFactor)) pltObj.tick_params('x', pad=0, rotation='auto') pltObj.grid(False, 'both', color=backgroundColor, linewidth=gridThickness) pltObj.patch.set(alpha=0.0) plt.sca(axImage) if options['rfiMode']: from rfiPlotter import onclick, hover, onaxesleave annot = pltObj.annotate("", xy=(0, 0), xytext=(15, 15), textcoords="offset points", bbox=dict(boxstyle="round", fc="w"), arrowprops=dict(arrowstyle="->")) annot.set_visible(False) global informationArr informationArr = { 'pltObj': pltObj, 'annot': annot, 'pixels': options['imagingOptions']['pixelCount'][0], 'cbCursor': cbCursor, 'rawdata': allSkyImage, 'axColorBar': axColorBar } onclickLambda = lambda clickEvent: onclick(clickEvent, **informationArr ) onaxesleaveLambda = lambda clickEvent: onaxesleave( clickEvent, **informationArr) hoverLambda = lambda event: hover(event, **informationArr) fig.canvas.mpl_connect("motion_notify_event", hoverLambda) fig.canvas.mpl_connect('button_press_event', onclickLambda) fig.canvas.mpl_connect('axes_leave_event', onaxesleaveLambda) plt.ioff() plt.show() plt.pause(1) elif plotOptions['displayImages']: plt.ioff() plt.show() plt.pause(1) outputFolder += '/sb_{0}/'.format(subband) if not os.path.exists(outputFolder): os.makedirs(outputFolder) plotFilename = "{7}{0}_{1}_sb{2}_mode{3}{4}_{5}_{6}MHz.png".format( dateTime, obsSite, subband, rcuMode, polarity, options['imagingOptions']['method'].replace('/', '-'), int(frequency / 1e6), outputFolder) plotFilename = plotFilename.replace(' ', '_').replace(':', '') print("Saving output to {0}".format(plotFilename)) fig.savefig(plotFilename, facecolor=fig.get_facecolor(), edgecolor='none') plt.close(figNum) return plotFilename
title('Lesion') xlabel('Time (seconds)') ylabel('Progression (cm)') xlim(0, 3) plt.subplot(1, 2, 2) [plot_trial_parameter(a, u1t, color='b') for a in cg] title('Sham/Control') xlabel('Time (seconds)') xlim(0, 3) ### PROGRESSION BEFORE AND AFTER COMPARISON ### fig, axs = plt.subplots(6, 2, True, True) axs = axs.ravel() subjects = [0, 1, 2, 3, 4, 5, 8, 7, 10, 11, 12, 13] for i in range(len(subjects)): plt.sca(axs[i]) plot_progression(subjects[i], label=False) if i == 0: title('Lesion') legend(bbox_to_anchor=(0, 0, 0.5, 1), bbox_transform=gcf().transFigure) if i == 1: title('Sham/Control') if i == 4: ylabel('Progression (cm)') if i > 9: xlabel('Time (seconds)') [plot(trial[:, 0], 'r') for trial in u1t[lg]] [plot(trial[:, 0], 'b') for trial in u1t[cg]] ### RAND ###
def draw(data): """ 画图 :return: """ types = data['types'] cpu_time = data['cpu_time'] cpu = data['cpu'] mem = data['mem'] jvm = data['jvm'] io_time = data['io_time'] io = data['io'] disk = data['disk'] rec = data['rec'] trans = data['trans'] net = data['nic'] length = len(cpu_time) io_length = len(io_time) net_length = len(net) if min(length, io_length, net_length) < 7: # 画图的最小刻度为7,故必须大于7个数据 logger.error('数据太少,请稍后再试') raise Exception('当前数据太少,请稍后再试') index = [[], []] # x轴坐标,一个是CPU和内存的x轴,一个是IO的x轴 labels = [[], []] # x轴刻度 delta = length / 6 # x轴每个坐标间隔 io_delta = io_length / 6 # x轴每个坐标间隔,IO for i in range(6): index[0].append(int(i * delta)) labels[0].append(utc2local(cpu_time[int(i * delta)])) if types == 'system': index[1].append(int(i * io_delta)) labels[1].append(utc2local(io_time[int(i * io_delta)])) # 添加最后一个时刻的数据 index[0].append(length - 1) labels[0].append(utc2local(cpu_time[length - 1])) # 数据的开始时间和结束时间,用于计算图中展示数据的总时长 cpu_start_time = time.mktime( datetime.datetime.strptime(cpu_time[0].split('.')[0], '%Y-%m-%dT%H:%M:%S').timetuple()) cpu_end_time = time.mktime( datetime.datetime.strptime(cpu_time[-1].split('.')[0], '%Y-%m-%dT%H:%M:%S').timetuple()) if types == 'system': # 添加最后一个时刻的数据 index[1].append(io_length - 1) labels[1].append(utc2local(io_time[io_length - 1])) # 用于计算总时长 io_start_time = time.mktime( datetime.datetime.strptime(io_time[0].split('.')[0], '%Y-%m-%dT%H:%M:%S').timetuple()) io_end_time = time.mktime( datetime.datetime.strptime(io_time[-1].split('.')[0], '%Y-%m-%dT%H:%M:%S').timetuple()) fig = plt.figure('figure', figsize=(20, 20)) ax1 = plt.subplot(4, 1, 1) ax2 = plt.subplot(4, 1, 2) ax3 = plt.subplot(4, 1, 3) ax4 = plt.subplot(4, 1, 4) # 画CPU使用率 plt.sca(ax1) plt.plot(cpu, color='r', linewidth=0.3, label='CPU') plt.grid() plt.xlim(0, len(cpu)) plt.ylim(0, 100) plt.title( 'CPU(%), max:{:.2f}%, average:{:.2f}%, duration:{:.1f}h'.format( max(cpu), sum(cpu) / len(cpu), (cpu_end_time - cpu_start_time) / 3600), size=12) plt.xticks(index[0], labels[0]) plt.margins(0, 0) # 画内存使用大小 plt.sca(ax2) plt.plot(mem, color='r', linewidth=1, label='Memory') plt.title('Free Memory(G), min:{:.2f}G, duration:{:.1f}h'.format( min(mem), (cpu_end_time - cpu_start_time) / 3600), size=12) plt.grid() plt.xlim(0, len(mem)) plt.ylim(0, max(mem) + 1) plt.xticks(index[0], labels[0]) plt.margins(0, 0) # 画磁盘IO plt.sca(ax3) plt.plot(io, color='r', linewidth=1, label='%util') plt.grid() plt.xlim(0, len(io)) plt.ylim(0, max(io)) plt.title('IO({}), max:{:.2f}%, duration:{:.1f}h'.format( disk, max(io), (io_end_time - io_start_time) / 3600), size=12) plt.xticks(index[1], labels[1]) plt.margins(0, 0) # 画带宽图 plt.sca(ax4) plt.plot(rec, color='orange', linewidth=0.5, label='rMbs') plt.plot(trans, color='blue', linewidth=0.5, label='tMbs') plt.legend(loc='upper left') plt.grid() plt.xlim(0, len(rec)) plt.ylim(0, max(max(rec), max(trans))) plt.title( 'NetWork, Rmax:{:.2f}Mb/s, Tmax:{:.2f}Mb/s, NetWork:{:.2f}%, duration:{:.1f}h' .format(max(rec), max(trans), max(net), (cpu_end_time - cpu_start_time) / 3600), size=12) plt.xticks(index[0], labels[0]) plt.margins(0, 0) ax_net = ax4.twinx() plt.sca(ax_net) plt.plot(net, color='red', linewidth=0.5, label='%net') plt.legend(loc='upper right') plt.ylim(0, max(net)) else: fig = plt.figure('figure', figsize=(20, 10)) ax1 = plt.subplot(2, 1, 1) ax2 = plt.subplot(2, 1, 2) # 画CPU使用率 plt.sca(ax1) plt.plot(cpu, color='r', linewidth=0.3, label='CPU') plt.grid() plt.xlim(0, len(cpu)) plt.ylim(0, 100) plt.title( 'CPU(%), max:{:.2f}%, average:{:.2f}%, duration:{:.1f}h'.format( max(cpu), sum(cpu) / len(cpu), (cpu_end_time - cpu_start_time) / 3600), size=12) plt.xticks(index[0], labels[0]) plt.margins(0, 0) # 画内存使用大小和jvm plt.sca(ax2) plt.plot(mem, color='r', linewidth=1, label='Memory') if sum(jvm) == 0: # 如果没有jvm数据,则不画jvm plt.title('Memory(G), max:{:.2f}G, duration:{:.1f}h'.format( max(mem), (cpu_end_time - cpu_start_time) / 3600), size=12) else: plt.plot(jvm, color='b', linewidth=1, label='JVM') plt.legend(loc='upper right') plt.title( 'Memory(G) max:{:.2f}G, JVM(G) max:{:.2f}G, duration:{:.1f}h'. format(max(mem), max(jvm), (cpu_end_time - cpu_start_time) / 3600), size=12) plt.grid() plt.xlim(0, len(mem)) plt.ylim(0, max(max(mem), max(jvm)) + 1) plt.xticks(index[0], labels[0]) plt.margins(0, 0) image_byte = BytesIO() fig.savefig(image_byte, format='png', bbox_inches='tight') # 把图像保存成二进制 img = base64.encodebytes(image_byte.getvalue()).decode() # 二进制图片流转base64编码 plt.close() return {'img': img}
criterion='mae', n_jobs=-1, bootstrap=True) #warm_start=False, #max_leaf_nodes = 30) rfr.fit(X_train, y_train) predictions = rfr.predict(X_test) #plt.scatter(y_test,predictions) print('MAE:', metrics.mean_absolute_error(y_test, predictions)) print('MSE:', metrics.mean_squared_error(y_test, predictions)) print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions))) print('MAPE:', mean_absolute_percentage_error(y_test, predictions)) #columns = net_profit_percent.columns #print (sorted(zip(map(lambda x: round(x, 4), rfr.feature_importances_), columns),reverse=True)) #subplots method of matplotlib fig, axes = plt.subplots(nrows=2, ncols=1) axes[0].scatter(y_test, predictions) plt.sca(axes[1]) #Use the pyplot interface to change just one subplot plt.xticks(range(X_train.shape[1]), X_train.columns, color='r') axes[1].bar(range(X_train.shape[1]), rfr.feature_importances_, color='b', align='center') #implement the profit_prediction algorihtm on pop skus second.drop('sku_id', axis=1, inplace=True) pop_predictions = rfr.predict(second)
def plot_params(self, pres_min, pres_max, param1, param2='z', delta=True, day_range=None, saveas=None, plottype='average'): """ Plot a given parameter's average value over a given depth range @params param1 - An ArgoProfile attribute, on the gen_attrlist param2 - An ArgoProfile attribute, on the gen_attrlist pres_min - Minimum pressure to plot from pres_max - Maximum pressure to plot from delta - Whether to plot absolute value or deviation from average day_range - Day range to plot (last day in list is top value for location/parameter plot) saveas - String file path to save this plot to, as a png file, if any (otherwise call plt.show()) """ # Plan not to display if saving, by turning interactive mode off (or on if planning to show) if saveas != None: plt.ioff() else: plt.ion() # Generate day range if needed. Format to sorted just in case. if day_range is None: day_range = self.any_match_days() day_range = sorted(day_range) # Generate pressure range pres_range = np.arange(pres_min, pres_max, 1.0) # PREPARE AXES f = plt.figure(figsize=(13, 8)) gs = gridspec.GridSpec(6, self.n_floats, figure=f) map_axes = [plt.subplot(gs[0:2, i]) for i in range(self.n_floats)] par_axes = [plt.subplot(gs[2:4, i]) for i in range(self.n_floats)] day_axis = plt.subplot(gs[4:6, :]) # Add axis labels and format shape to place nicer when saving without adjustments. for par_axis in par_axes: par_axis.set_xlabel(ap.attr_to_name(param1)) par_axis.set_ylabel(ap.attr_to_name(param2)) day_axis.set_xlabel("Date") day_axis.set_ylabel(str(param1) + " " + plottype) plt.tight_layout() plt.subplots_adjust(top=0.94, bottom=0.203, left=0.081, right=0.98, hspace=0.751, wspace=0.481) # AXES : map_axes : Float location on map with SLA # For each argo float for argofloat, map_axis, wmo_id in zip(self.floats, map_axes, self.wmo_ids): # a. Draw the map map = argofloat.aviso_map_plot(day_range[-1], map_axis) # b. Turn float interpolated lons and lats for this day into map coordinates x, y = map(argofloat.lon_interp(day_range), argofloat.lat_interp(day_range)) # c. Plot, emphasizing the latest position as a green dot map.plot(x, y, color='black', zorder=2) map.scatter(x[-1], y[-1], color='green', s=5, zorder=3) # d. Label axis map_axis.set_title(str(wmo_id)) # AXES : par_axes : Parameter vs. Depth for each profile # 1. Prepare a set of colors to emphasize most recent profile info and grey out old information cmap = cm.get_cmap('binary') norm = matplotlib.colors.PowerNorm(vmin=1, vmax=5, gamma=3.0, clip=True) colors = [ cmap(0.05 + 0.95 * norm(i + 1 - len(day_range) + 5)) for i in range(len(day_range)) ] # 2. Go through the days and... avg1 = [[] for d in day_range ] #prep to get averages w/ (day, float) indexing avg2 = [[] for d in day_range ] #prep to get averages w/ (day, float) indexing for i_d in range(len(day_range)): # a. Go through each synthetic profile and generate the desired parameters' values param1_values = [] param2_values = [] for synthprof in self.synth_profiles[day_range[i_d]]: param1_values.append( getattr(synthprof, param1 + '_interp')(pres_range)) param2_values.append( getattr(synthprof, param2 + '_interp')(pres_range)) # b. And iterate through the floats... for i_f in range(self.n_floats): # Either plot the values or the delta from average values, and add the average to the list if delta: #1. Get the mean values of the parameters first param1_means = getattr(self.floats[i_f].prof_avg, param1 + '_interp')(pres_range) param2_means = getattr(self.floats[i_f].prof_avg, param2 + '_interp')(pres_range) #2. Get the differential between this day and the average over time delta1_values = param1_values[i_f] - param1_means delta2_values = param2_values[i_f] - param2_means #3. Append the average to the list of averages avg1[i_d].append(np.nanmean(delta1_values)) avg2[i_d].append(np.nanmean(delta2_values)) #4. Plot on the corresponding parameter axis par_axes[i_f].plot(delta1_values[i_f], param2_values[i_f], color=colors[i_d]) else: #1. Add the not-differential average to the list of averages avg1[i_d].append(np.nanmean(param1_values[i_f])) avg2[i_d].append(np.nanmean(param2_values[i_f])) #2. Plot on corresponding parameter axis par_axes[i_f].plot(param1_values[i_f], param2_values[i_f], color=colors[i_d]) # 3. Grid the par_axis plots for par_axis in par_axes: par_axis.grid(True) # 4. Plot the average as a verticle line on the axis for par_axis, a in zip(par_axes, avg1[-1]): par_axis.axvline(a, color='red') # AXIS : day_axis : Parameter Average vs. Time # 1. Change average indexing from (day, float) to (float, day) avg1 = np.array(avg1).T # 2. Plot the average over time for each float plt.sca(day_axis) for i in range(self.n_floats): day_axis.plot(day_range, avg1[i], label=self.wmo_ids[i], linewidth=2) # 3. Plot the average between floats over time day_axis.plot(day_range, np.mean(avg1, axis=0), label='Average', linewidth=3, color='black') # 4. Add the grid and legend day_axis.grid(True) day_axis.legend(loc='lower right') # 5. Format date on x axis tick labels xticks = day_axis.get_xticks() form = [ap.format_jd(xt) for xt in xticks] # Convert into formatted dates day_axis.set_xticklabels(form, rotation=90) # Save and close if desired if saveas != None: f.savefig(saveas) plt.close(f) else: plt.show()
def plot_results(allresults, *, xy_fn=default_xy_fn, split_fn=default_split_fn, group_fn=default_split_fn, average_group=False, shaded_std=True, shaded_err=True, figsize=None, legend_outside=False, resample=0, smooth_step=1.0, tiling='vertical', xlabel=None, ylabel=None): ''' Plot multiple Results objects xy_fn: function Result -> x,y - function that converts results objects into tuple of x and y values. By default, x is cumsum of episode lengths, and y is episode rewards split_fn: function Result -> hashable - function that converts results objects into keys to split curves into sub-panels by. That is, the results r for which split_fn(r) is different will be put on different sub-panels. By default, the portion of r.dirname between last / and -<digits> is returned. The sub-panels are stacked vertically in the figure. group_fn: function Result -> hashable - function that converts results objects into keys to group curves by. That is, the results r for which group_fn(r) is the same will be put into the same group. Curves in the same group have the same color (if average_group is False), or averaged over (if average_group is True). The default value is the same as default value for split_fn average_group: bool - if True, will average the curves in the same group and plot the mean. Enables resampling (if resample = 0, will use 512 steps) shaded_std: bool - if True (default), the shaded region corresponding to standard deviation of the group of curves will be shown (only applicable if average_group = True) shaded_err: bool - if True (default), the shaded region corresponding to error in mean estimate of the group of curves (that is, standard deviation divided by square root of number of curves) will be shown (only applicable if average_group = True) figsize: tuple or None - size of the resulting figure (including sub-panels). By default, width is 6 and height is 6 times number of sub-panels. legend_outside: bool - if True, will place the legend outside of the sub-panels. resample: int - if not zero, size of the uniform grid in x direction to resample onto. Resampling is performed via symmetric EMA smoothing (see the docstring for symmetric_ema). Default is zero (no resampling). Note that if average_group is True, resampling is necessary; in that case, default value is 512. smooth_step: float - when resampling (i.e. when resample > 0 or average_group is True), use this EMA decay parameter (in units of the new grid step). See docstrings for decay_steps in symmetric_ema or one_sided_ema functions. ''' if split_fn is None: split_fn = lambda _: '' if group_fn is None: group_fn = lambda _: '' sk2r = defaultdict(list) # splitkey2results for result in allresults: splitkey = split_fn(result) sk2r[splitkey].append(result) assert len(sk2r) > 0 assert isinstance(resample, int), "0: don't resample. <integer>: that many samples" if tiling == 'vertical' or tiling is None: nrows = len(sk2r) ncols = 1 elif tiling == 'horizontal': ncols = len(sk2r) nrows = 1 elif tiling == 'symmetric': import math N = len(sk2r) largest_divisor = 1 for i in range(1, int(math.sqrt(N)) + 1): if N % i == 0: largest_divisor = i ncols = largest_divisor nrows = N // ncols figsize = figsize or (6 * ncols, 6 * nrows) f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize) groups = list(set(group_fn(result) for result in allresults)) default_samples = 512 if average_group: resample = resample or default_samples for (isplit, sk) in enumerate(sorted(sk2r.keys())): g2l = {} g2c = defaultdict(int) sresults = sk2r[sk] gresults = defaultdict(list) idx_row = isplit // ncols idx_col = isplit % ncols ax = axarr[idx_row][idx_col] for result in sresults: group = group_fn(result) g2c[group] += 1 x, y = xy_fn(result) if x is None: x = np.arange(len(y)) x, y = map(np.asarray, (x, y)) if average_group: gresults[group].append((x, y)) else: if resample: x, y, counts = symmetric_ema(x, y, x[0], x[-1], resample, decay_steps=smooth_step) l, = ax.plot(x, y, color=COLORS[groups.index(group) % len(COLORS)]) g2l[group] = l if average_group: for group in sorted(groups): xys = gresults[group] if not any(xys): continue color = COLORS[groups.index(group) % len(COLORS)] origxs = [xy[0] for xy in xys] minxlen = min(map(len, origxs)) def allequal(qs): return all((q == qs[0]).all() for q in qs[1:]) if resample: low = max(x[0] for x in origxs) high = min(x[-1] for x in origxs) usex = np.linspace(low, high, resample) ys = [] for (x, y) in xys: ys.append( symmetric_ema(x, y, low, high, resample, decay_steps=smooth_step)[1]) else: assert allequal([x[:minxlen] for x in origxs]),\ 'If you want to average unevenly sampled data, set resample=<number of samples you want>' usex = origxs[0] ys = [xy[1][:minxlen] for xy in xys] ymean = np.mean(ys, axis=0) ystd = np.std(ys, axis=0) ystderr = ystd / np.sqrt(len(ys)) l, = axarr[idx_row][idx_col].plot(usex, ymean, color=color) g2l[group] = l if shaded_err: ax.fill_between(usex, ymean - ystderr, ymean + ystderr, color=color, alpha=.4) if shaded_std: ax.fill_between(usex, ymean - ystd, ymean + ystd, color=color, alpha=.2) # https://matplotlib.org/users/legend_guide.html plt.tight_layout() if any(g2l.keys()): ax.legend(g2l.values(), ['%s (%i)' % (g, g2c[g]) for g in g2l] if average_group else g2l.keys(), loc=2 if legend_outside else None, bbox_to_anchor=(1, 1) if legend_outside else None) ax.set_title(sk) # add xlabels, but only to the bottom row if xlabel is not None: for ax in axarr[-1]: plt.sca(ax) plt.xlabel(xlabel) # add ylabels, but only to left column if ylabel is not None: for ax in axarr[:, 0]: plt.sca(ax) plt.ylabel(ylabel) return f, axarr
def applyoptions(md, data, options, fig, axgrid, gridindex): ''' APPLYOPTIONS - apply options to current plot 'plotobj' is the object returned by the specific plot call used to render the data. This object is used for adding a colorbar. Usage: applyoptions(md,data,options) See also: PLOTMODEL, PARSE_OPTIONS ''' # get handle to current figure and axes instance #fig = p.gcf() ax = axgrid[gridindex] # {{{ font fontsize = options.getfieldvalue('fontsize', 8) fontweight = options.getfieldvalue('fontweight', 'normal') fontfamily = options.getfieldvalue('fontfamily', 'sans-serif') font = { 'fontsize': fontsize, 'fontweight': fontweight, 'family': fontfamily } # }}} # {{{ title if options.exist('title'): title = options.getfieldvalue('title') if options.exist('titlefontsize'): titlefontsize = options.getfieldvalue('titlefontsize') else: titlefontsize = fontsize if options.exist('titlefontweight'): titlefontweight = options.getfieldvalue('titlefontweight') else: titlefontweight = fontweight #title font titlefont = font.copy() titlefont['size'] = titlefontsize titlefont['weight'] = titlefontweight ax.set_title(title, **titlefont) # }}} # {{{ xlabel, ylabel, zlabel if options.exist('labelfontsize'): labelfontsize = options.getfieldvalue('labelfontsize') else: labelfontsize = fontsize if options.exist('labelfontweight'): labelfontweight = options.getfieldvalue('labelfontweight') else: labelfontweight = fontweight #font dict for labels labelfont = font.copy() labelfont['fontsize'] = labelfontsize labelfont['fontweight'] = labelfontweight if options.exist('xlabel'): ax.set_xlabel(options.getfieldvalue('xlabel'), **labelfont) if options.exist('ylabel'): ax.set_ylabel(options.getfieldvalue('ylabel'), **labelfont) if options.exist('zlabel'): ax.set_zlabel(options.getfieldvalue('zlabel'), **labelfont) # }}} # {{{ xticks, yticks, zticks (tick locations) if options.exist('xticks'): if options.exist('xticklabels'): xticklabels = options.getfieldvalue('xticklabels') ax.set_xticks(options.getfieldvalue('xticks'), xticklabels) else: ax.set_xticks(options.getfieldvalue('xticks')) if options.exist('yticks'): if options.exist('yticklabels'): yticklabels = options.getfieldvalue('yticklabels') ax.set_yticks(options.getfieldvalue('yticks'), yticklabels) else: ax.set_yticks(options.getfieldvalue('yticks')) if options.exist('zticks'): if options.exist('zticklabels'): zticklabels = options.getfieldvalue('zticklabels') ax.set_zticks(options.getfieldvalue('zticks'), zticklabels) else: ax.set_zticks(options.getfieldvalue('zticks')) # }}} # {{{ xticklabels,yticklabels,zticklabels if options.getfieldvalue('ticklabels', 'off') == 'off' or options.getfieldvalue( 'ticklabels', 0) == 0: options.addfielddefault('xticklabels', []) options.addfielddefault('yticklabels', []) # TODO check if ax has a z-axis (e.g. is 3D) if options.exist('xticklabels'): xticklabels = options.getfieldvalue('xticklabels') ax.set_xticklabels(xticklabels) if options.exist('yticklabels'): yticklabels = options.getfieldvalue('yticklabels') ax.set_yticklabels(yticklabels) if options.exist('zticklabels'): zticklabels = options.getfieldvalue('zticklabels') ax.set_zticklabels(zticklabels) # }}} # {{{ ticklabel notation #ax.ticklabel_format(style='sci',scilimits=(0,0)) # }}} # {{{ ticklabelfontsize if options.exist('ticklabelfontsize'): for label in ax.get_xticklabels() + ax.get_yticklabels(): label.set_fontsize(options.getfieldvalue('ticklabelfontsize')) if int(md.mesh.dimension) == 3: for label in ax.get_zticklabels(): label.set_fontsize(options.getfieldvalue('ticklabelfontsize')) # }}} # {{{ view TOFIX #if int(md.mesh.dimension) == 3 and options.exist('layer'): # #options.getfieldvalue('view') ? # ax=fig.gca(projection='3d') #plt.show() # }}} # {{{ axis if options.exist('axis'): if options.getfieldvalue('axis', True) == 'off': ax.ticklabel_format(style='plain') p.setp(ax.get_xticklabels(), visible=False) p.setp(ax.get_yticklabels(), visible=False) # }}} # {{{ box if options.exist('box'): eval(options.getfieldvalue('box')) # }}} # {{{ xlim, ylim, zlim if options.exist('xlim'): ax.set_xlim(options.getfieldvalue('xlim')) if options.exist('ylim'): ax.set_ylim(options.getfieldvalue('ylim')) if options.exist('zlim'): ax.set_zlim(options.getfieldvalue('zlim')) # }}} # {{{ latlon TODO # }}} # {{{ Basinzoom TODO # }}} # {{{ ShowBasins TODO # }}} # {{{ clim if options.exist('clim'): lims = options.getfieldvalue('clim') assert len( lims) == 2, 'error, clim should be passed as a list of length 2' elif options.exist('caxis'): lims = options.getfieldvalue('caxis') assert len( lims) == 2, 'error, caxis should be passed as a list of length 2' options.addfielddefault('clim', lims) else: if len(data) > 0: lims = [data.min(), data.max()] else: lims = [0, 1] # }}} # {{{ shading TODO #if options.exist('shading'): # }}} # {{{ grid if options.exist('grid'): if 'on' in options.getfieldvalue('grid', 'on'): ax.grid() # }}} # {{{ colormap if options.exist('colornorm'): norm = options.getfieldvalue('colornorm') if options.exist('colormap'): cmap = options.getfieldvalue('colormap') cbar_extend = 0 if options.exist('cmap_set_over'): cbar_extend += 1 if options.exist('cmap_set_under'): cbar_extend += 2 # }}} # {{{ contours if options.exist('contourlevels'): plot_contour(md, data, options, ax) # }}} # {{{ wrapping TODO # }}} # {{{ colorbar if options.getfieldvalue('colorbar', 1) == 1: if cbar_extend == 0: extend = 'neither' elif cbar_extend == 1: extend = 'max' elif cbar_extend == 2: extend = 'min' elif cbar_extend == 3: extend = 'both' cb = mpl.colorbar.ColorbarBase(ax.cax, cmap=cmap, norm=norm, extend=extend) if options.exist('alpha'): cb.set_alpha(options.getfieldvalue('alpha')) if options.exist('colorbarnumticks'): cb.locator = MaxNLocator( nbins=options.getfieldvalue('colorbarnumticks', 5)) else: cb.locator = MaxNLocator(nbins=5) # default 5 ticks if options.exist('colorbartickspacing'): locs = np.arange(lims[0], lims[1] + 1, options.getfieldvalue('colorbartickspacing')) cb.set_ticks(locs) if options.exist('colorbarlines'): locs = np.arange(lims[0], lims[1] + 1, options.getfieldvalue('colorbarlines')) cb.add_lines(locs, ['k' for i in range(len(locs))], np.ones_like(locs)) if options.exist('colorbarlineatvalue'): locs = options.getfieldvalue('colorbarlineatvalue') colors = options.getfieldvalue('colorbarlineatvaluecolor', ['k' for i in range(len(locs))]) widths = options.getfieldvalue('colorbarlineatvaluewidth', np.ones_like(locs)) cb.add_lines(locs, colors, widths) if options.exist('colorbartitle'): if options.exist('colorbartitlepad'): cb.set_label( options.getfieldvalue('colorbartitle'), labelpad=options.getfieldvalue('colorbartitlepad'), fontsize=fontsize) else: cb.set_label(options.getfieldvalue('colorbartitle'), fontsize=fontsize) cb.ax.tick_params(labelsize=fontsize) cb.solids.set_rasterized(True) cb.update_ticks() cb.set_alpha(1) cb.draw_all() if options.exist('colorbarfontsize'): colorbarfontsize = options.getfieldvalue('colorbarfontsize') cb.ax.tick_params(labelsize=colorbarfontsize) # cb.set_ticks([0,-10]) # cb.set_ticklabels([-10,0,10]) if options.exist('colorbarticks'): colorbarticks = options.getfieldvalue('colorbarticks') cb.set_ticks(colorbarticks) plt.sca(ax) # return to original axes control # }}} # {{{ expdisp if options.exist('expdisp'): expdisp(ax, options) # }}} # {{{ area TODO # }}} # {{{ text if options.exist('text'): text = options.getfieldvalue('text') textx = options.getfieldvalue('textx') texty = options.getfieldvalue('texty') textcolor = options.getfieldvalue('textcolor') textweight = options.getfieldvalue('textweight') textrotation = options.getfieldvalue('textrotation') textfontsize = options.getfieldvalue('textfontsize') for label, x, y, size, color, weight, rotation in zip( text, textx, texty, textfontsize, textcolor, textweight, textrotation): ax.text(x, y, label, transform=ax.transAxes, fontsize=size, color=color, weight=weight, rotation=rotation) # }}} # {{{ north arrow TODO # }}} # {{{ scale ruler TODO # }}} # {{{ streamlines TOFIX if options.exist('streamlines'): plot_streamlines(md, options, ax)
def plotXsection(startpoints, endpoints, names, geo_model, lith, surflith, vscale=1, unitnames=None): '''Plots an approximate cross-section between the two specified points, using an elevation-cropped array (cells above the land surface should have nan values). Does not work well for N-S or nearly N-S xsections - use gempy built-in for that. startpoints: [[x1,y1],[x2,y2],...] float or [[col1,row1],[col1,row2],...] integer array of coordinates of starting points A endpoints: [[x1,y1],[x2,y2],...] float or [[col1,row1],[col1,row2],...] integer array of coordinates of ending points B names: [['A','B'],['C','D'],...] string array of names for starting and ending points grid_info: [xmin,xmax,xres,dx,ymin,ymax,yres,dy,zmin,zmax,zres,dz] model grid info (can get these using importDEM() function if model grid is same as DEM grid) lith: elevation-cropped array of lithologic unit indices of dimensions (nrow,ncol,nlay), i.e. (yres,xres,zres). Can use uncropped array, but will plot above land surface. surflith: array of lithologic unit values at the land surface, of dimensions (yres,xres) vscale: vertical exaggeration factor (y/x, defaults to 1) colors: dictionary of color & unit names to use (in order from youngest to oldest) OR 'gempy' to use default gempy colormap ''' #Get coordinate info from grid & create VTK cells info: xmin = geo_model.grid.regular_grid.extent[0] #min coordinate value (left) xmax = geo_model.grid.regular_grid.extent[1] #max coordinate value (right) xres = geo_model.grid.regular_grid.resolution[0] #number of pixels dx = (xmax - xmin) / xres #pixel width ymin = geo_model.grid.regular_grid.extent[2] ymax = geo_model.grid.regular_grid.extent[3] yres = geo_model.grid.regular_grid.resolution[1] dy = (ymax - ymin) / yres zmin = geo_model.grid.regular_grid.extent[4] zmax = geo_model.grid.regular_grid.extent[5] zres = geo_model.grid.regular_grid.resolution[2] dz = (zmax - zmin) / zres #Generate colormap: cmap = matplotlib.colors.ListedColormap( geo_model.surfaces.colors.colordict.values()) #set colormap norm = matplotlib.colors.Normalize( vmin=1, vmax=len(geo_model.surfaces.colors.colordict)) #set normalization #Plot geologic map once for reference: f1, ax1 = plt.subplots(1, 1, figsize=(10, 10)) #create empty figure plt.imshow(surflith, cmap=cmap, norm=norm) #plot geology (normalized to gempy color range) f2, ax2 = plt.subplots(len(startpoints), 1, figsize=( 15, 20)) #create figure and axes objects for subplots (one per xsection) for i in range(len(startpoints)): #loop over number of sections #Get starting coordinates: xA = startpoints[i][0] #get starting x coordinate yA = startpoints[i][1] #get starting y coordinate xB = endpoints[i][0] #get ending x coordinate yB = endpoints[i][1] #get ending y coordinate #Calculate corresponding row,col if type( xA ) != int: #if coordinates are NOT integers (i.e. not row,col numbers), convert them colA = (xA - xmin) // dx #col:x calculate column index c = (x1-x0)/dx rowA = yres - ( (yA - ymin) // dy ) #row:y calculate row index r = ymax - (y1-y0)/dy colB = (xB - xmin) // dx rowB = yres - ((yB - ymin) // dy) else: #if coordinates are already in row,col format colA = xA rowA = yA colB = xB rowB = yB #Calculate line equation between points A and B: m = (rowB - rowA) / (colB - colA ) #calculate slope m = (y2-y1)/(x2-x1) b = -m * colA + rowA #calculate intercept b = m*x1 + y1 (slope is neg here bc y axis is flipped) #Calculate true distance (not # of cells) between points A and B: #distance = ((xB-xA)**2 + (yB-yA)**2)**.5 #xsizes.append(distance*.001) #Get xy indices for cells intersected by the x-sec line, then get z values for those xy points: xvals = np.arange( colA, colB) #generate array of x values between the two points xvals = xvals.astype(int) #convert to integer yvals = m * xvals + b #calculate corresponding y values y = mx + b yvals = yvals.astype(int) #convert to integers to be able to slice #xsec = lith[yvals,xvals,:].T #select x-sec to plot and transpose to make it plot horizontally xsec = lith[ xvals, yvals, :].T #select x-sec to plot and transpose to make it plot horizontally #Plotting: #Add xsection lines to geologic map: plt.figure(f1.number) #make the map the active figure plt.plot([colA, colB], [rowA, rowB], 'k') #plot x-sec location line plt.annotate(names[i][0], xy=(colA, rowA), xytext=(colA - 4, rowA + 4)) #annotate start point plt.annotate(names[i][1], xy=(colB, rowB), xytext=(colB + 1, rowB - 1)) #annotate start point plt.ylim(bottom=yres, top=0) plt.xlim(left=0, right=xres) #Plot cross-sections in a new figure: #Set and get correct subplot axes: if len( startpoints ) == 1: #check if there are more than 1 subplots (for indexing purposes) plt.sca( ax2 ) #make current subplot axes active (automatically makes fig active too) cax = plt.gca() #get current axes object else: plt.sca(ax2[i]) cax = plt.gca() cax.imshow(xsec, origin="lower", cmap=cmap, norm=norm) #plot (with down=lower z indices) #cax.imshow(xsec, origin="lower", cmap=cmap) #plot (with down=lower z indices) cax.set_aspect(vscale * dz / dx) #apply vertical exaggeration cax.set_ylim(bottom=0, top=zres) #set y limit to zres cax.set_title(names[i][0] + names[i][1]) cax.set_anchor('W') #align left (West) #Set ticks to accurately reflect elevation (masl): locs = cax.get_yticks() #get tick locations nlabels = len(cax.get_yticklabels()) #get number of initial ticks labels = np.linspace(zmin, zmax, nlabels) #generate list of tick labels ticks = cax.set(yticks=locs, yticklabels=labels) #set tick locations and labels return f1, ax1, f2, ax2
def colorbar_to_axis(self, toaxis, clim, cmap, vmin=None, under='gray', vmax=None, over='red', title=None, ycb=10., fz_title=15, ticks='complete', fz_ticks=10, outline=False, orientation='vertical'): """Add a colorbar to a particular axis. Parameters ---------- toaxis : int Integer to specify the axis to which add the colorbar. clim : tuple/list A tuple of float/int describing the limit of the colorbar. cmap : str The colormap to use ('viridis', 'jet', 'Spectral'...) vmin : float | None Minimum threshold. See the under parameter for further details. under : string/tuple/list | 'gray' Every values bellow vmin will be set to the under color. vmax : float | None Maximum threshold. See the over parameter for further details. over : string/tuple/list | 'red' Every values over vmax will be set to the over color. title : string | None Title of the colorbar. ycb : float | 10. Distance between the colorbar and it title (if defined). fz_title : int/float | 15 The fontsize of colorbar title. ticks : string/int/float/np.ndarray | 'complete' Ticks of the colorbar. This parameter is only active if clim is defined. Use 'complete' to see only the minimum, maximum, vmin and vmax (if defined). Use 'minmax' to only see the maximum and minimum. If ticks is a float, an linear interpolation between the maximum and minimum will be used. Finally, if ticks is a NumPy array, it will be used as colorbar ticks directly. fz_ticks : int/float | 10 Fontsize of tick labels. outline : bool | False Specify if the box arround the colorbar have to be displayed. orientation : string | 'vertical' Colorbar orientation. Use either 'vertical' or 'horizontal'. See also -------- shared_colorbar : colorbar shared by multiple subplots. """ # ----------------- CHECKING ----------------- if orientation not in ['vertical', 'horizontal']: raise ValueError("Colorbar orientation must either be 'vertical' " "or 'horizontal'") # ----------------- CURRENT AXIS ----------------- cim = self._im[toaxis] plt.sca(self._ax[toaxis]) # ----------------- COLORMAP ----------------- # Manually create the colormap : cmap = self._customcmap(cmap, clim, vmin, under, vmax, over) cim.set_cmap(cmap) # ----------------- COLORBAR ----------------- cb = plt.colorbar(cim, shrink=0.7, pad=0.01, aspect=10, orientation=orientation) # # ----------------- CLIM ----------------- cim.set_clim(*clim) cb.set_clim(*clim) # ----------------- CBAR ----------------- self._cbar(cb, cmap, clim, vmin, under, vmax, over, title, ycb, fz_title, ticks, fz_ticks, outline, orientation, self._tcol)