def append(self, plot_cache): newpoints = [x[0] for x in plot_cache] self.latest.popleft() self.latest.popleft() self.latest.append(max(newpoints)) self.latest.append(min(newpoints)) for point, step, mtype, best_metric in plot_cache: if mtype in ('accept', 'acceptimprove'): self.y2s.append(point) self.x2s.append(step) elif mtype == "newbest": self.y3s.append(point) self.x3s.append(step) else: self.y1s.append(point) self.x1s.append(step) self.plt1.set_xdata(self.x1s) self.plt1.set_ydata(self.y1s) self.plt2.set_xdata(self.x2s) self.plt2.set_ydata(self.y2s) self.plt3.set_xdata(self.x3s) self.plt3.set_ydata(self.y3s) # import ipdb; ipdb.set_trace() plt.ylim([min(self.latest)*0.9, max(self.latest)]) plt.draw()
def plot_fit(self, size=None, tol=0.1, axis_on=True): n, d = self.D.shape if size: nrows, ncols = size else: sq = np.ceil(np.sqrt(n)) nrows = int(sq) ncols = int(sq) ymin = np.nanmin(self.D) ymax = np.nanmax(self.D) print 'ymin: {0}, ymax: {1}'.format(ymin, ymax) numplots = np.min([n, nrows * ncols]) plt.figure() for n in xrange(numplots): plt.subplot(nrows, ncols, n + 1) plt.ylim((ymin - tol, ymax + tol)) plt.plot(self.L[n, :] + self.S[n, :], 'r') plt.plot(self.L[n, :], 'b') if not axis_on: plt.axis('off')
def plot_response(data, plate_name, save_folder = 'Figures/'): """ """ if not os.path.isdir(save_folder): os.makedirs(save_folder) for block in data: # group = group_similar(data[block].keys()) names = data[block].keys() names.sort() # plt.figure(figsize=(16, 4 + len(names)/8), dpi=300) # for i, name in enumerate(names): a, b, c = get_index(group, name) color, pattern = color_shade_pattern(a, b, c, group) mean = data[block][name]['mean'][0] std = data[block][name]['std'][0] plt.barh([i], [mean], height=1.0, color=color, hatch=pattern) plt.errorbar([mean], [i+0.5], xerr=[std], ecolor = [0,0,0], linestyle = '') plt.yticks([i+0.5 for i in xrange(len(names))], names, size = 8) plt.title(plate_name) plt.ylim(0, len(names)) plt.xlabel('change') plt.tight_layout() plt.savefig(save_folder + 'response_' + str(block + 1)) # return None
def draw(cls, t_max, agents_proportions, eco_idx, parameters): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=2.0, label="Type-{} agents".format(agent_type)) plt.ylim([-0.1, 1.1]) plt.xlabel("$t$") plt.ylabel("Proportion of indirect exchanges") # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') plt.legend(loc='upper right', fontsize=12) print(parameters) plt.title( "Workforce: {}, {}, {}; displacement area: {}; vision area: {}; alpha: {}; tau: {}\n" .format( parameters["x0"], parameters["x1"], parameters["x2"], parameters["movement_area"], parameters["vision_area"], parameters["alpha"], parameters["tau"] ), fontsize=12) if not path.exists("../../figures"): mkdir("../../figures") plt.savefig("../../figures/figure_{}.pdf".format(eco_idx)) plt.show()
def example_filterbank(): from pylab import plt import numpy as np x = _create_impulse(2000) gfb = GammatoneFilterbank(density=1) analyse = gfb.analyze(x) imax, slopes = gfb.estimate_max_indices_and_slopes() fig, axs = plt.subplots(len(gfb.centerfrequencies), 1) for (band, state), imx, ax in zip(analyse, imax, axs): ax.plot(np.real(band)) ax.plot(np.imag(band)) ax.plot(np.abs(band)) ax.plot(imx, 0, 'o') ax.set_yticklabels([]) [ax.set_xticklabels([]) for ax in axs[:-1]] axs[0].set_title('Impulse responses of gammatone bands') fig, ax = plt.subplots() def plotfun(x, y): ax.semilogx(x, 20*np.log10(np.abs(y)**2)) gfb.freqz(nfft=2*4096, plotfun=plotfun) plt.grid(True) plt.title('Absolute spectra of gammatone bands.') plt.xlabel('Normalized Frequency (log)') plt.ylabel('Attenuation /dB(FS)') plt.axis('Tight') plt.ylim([-90, 1]) plt.show() return gfb
def plot(cls, data, figure_folder, msg="", suffix=""): fig, ax = plt.subplots() plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.2) x = np.arange(len(data[:])) ax.plot(x, data[:, 0], c="red", linewidth=2, label="agent 01") ax.plot(x, data[:, 1], c="blue", linewidth=2, label="agent 12") ax.plot(x, data[:, 2], c="green", linewidth=2, label="agent 20") plt.ylim([-0.01, 1.01]) plt.text(0, -0.23, "PARAMETERS. {}".format(msg)) ax.legend(fontsize=12, bbox_to_anchor=(1.1, 1.05)) # loc='upper center' ax.set_xlabel("$t$") ax.set_ylabel("Proportion of agents proceeding to indirect exchange") ax.set_title("Money emergence with a basal ganglia model") # Save fig if not exists(figure_folder): mkdir(figure_folder) fig_name = "{}/figure_{}.pdf".format(figure_folder, suffix.split(".p")[0]) plt.savefig(fig_name) plt.close()
def _plot(self): p = plt.pcolor(self.matrix, cmap=self.cmap, vmin=self.vmin, vmax=self.vmax) plt.colorbar(p) plt.xlim((0, self.matrix.shape[0])) plt.ylim((0, self.matrix.shape[1])) if self.labels is not None: plt.xticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize, rotation=90) plt.yticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize)
def plot(self, new_plot=False, xlim=None, ylim=None, title=None, figsize=None, xlabel=None, ylabel=None, fontsize=None, show_legend=True, grid=True): """ Plot data using matplotlib library. Use show() method for matplotlib to see result or :: %pylab inline in IPython to see plot as cell output. :param bool new_plot: create or not new figure :param xlim: x-axis range :param ylim: y-axis range :type xlim: None or tuple(x_min, x_max) :type ylim: None or tuple(y_min, y_max) :param title: title :type title: None or str :param figsize: figure size :type figsize: None or tuple(weight, height) :param xlabel: x-axis name :type xlabel: None or str :param ylabel: y-axis name :type ylabel: None or str :param fontsize: font size :type fontsize: None or int :param bool show_legend: show or not labels for plots :param bool grid: show grid or not """ xlabel = self.xlabel if xlabel is None else xlabel ylabel = self.ylabel if ylabel is None else ylabel figsize = self.figsize if figsize is None else figsize fontsize = self.fontsize if fontsize is None else fontsize self.fontsize_ = fontsize self.show_legend_ = show_legend title = self.title if title is None else title xlim = self.xlim if xlim is None else xlim ylim = self.ylim if ylim is None else ylim new_plot = self.new_plot or new_plot if new_plot: plt.figure(figsize=figsize) plt.xlabel(xlabel, fontsize=fontsize) plt.ylabel(ylabel, fontsize=fontsize) plt.title(title, fontsize=fontsize) plt.tick_params(axis='both', labelsize=fontsize) plt.grid(grid) if xlim is not None: plt.xlim(xlim) if ylim is not None: plt.ylim(ylim) self._plot() if show_legend: plt.legend(loc='best', scatterpoints=1)
def test_dip(self): xf = arange(0, 425) dips = self.fm.get_dip(xf) plt.plot(xf,dips) plt.grid('on') plt.gca().set_xticks(self.fm.Y_PC) plt.ylim([0, 30]) plt.gca().invert_yaxis() plt.savefig(join(self.outs_dir, '~y_fc_dips.png')) plt.close()
def plot(cls, data, msg=""): x = np.arange(len(data[:])) plt.plot(x, data[:, 0], c="red", linewidth=2) plt.plot(x, data[:, 1], c="blue", linewidth=2) plt.plot(x, data[:, 2], c="green", linewidth=2) plt.ylim([-0.01, 1.01]) plt.text(0, -0.12, "{}".format(msg)) plt.show()
def __call__(self,axis_on_or_off='off',use_lims=True,use_lims_ext=False): if 0: axis_on_or_off = axis_on_or_off.lower() if axis_on_or_off not in ['off','on']: raise ValueError(axis_on_or_off) if use_lims_ext: use_lims=False if use_lims_ext and use_lims: msg="use_lims={0} AND ".format(use_lims) msg+="use_lims_ext={0} ".format(use_lims_ext) msg+="but at most one of these can be True." raise ValueError(msg) Nx=self.Nx Ny=self.Ny # plt.axis(axis_on_or_off) plt.axis('scaled') # if use_lims: # plt.xlim([0,Nx]) # plt.ylim([0,Ny]) # if use_lims_ext: # plt.xlim([0,Nx+1]) # plt.ylim([0,Ny+1]) of.plt.axis_ij() return axis_on_or_off = axis_on_or_off.lower() if axis_on_or_off not in ['off','on']: raise ValueError(axis_on_or_off) if use_lims_ext: use_lims=False if use_lims_ext and use_lims: msg="use_lims={0} AND ".format(use_lims) msg+="use_lims_ext={0} ".format(use_lims_ext) msg+="but at most one of these can be True." raise ValueError(msg) Nx=self.Nx Ny=self.Ny plt.axis(axis_on_or_off) plt.axis('scaled') if use_lims: plt.xlim([0,Nx]) plt.ylim([0,Ny]) if use_lims_ext: plt.xlim([0,Nx+1]) plt.ylim([0,Ny+1]) of.plt.axis_ij()
def plot_smoothed_alpha_comparison(self,rmsval,suffix=''): plt.plot(self.f,self.alpha,'ko',label='data set') plt.plot(self.f,self.salpha,'c-',lw=2,label='smoothed angle $\phi$') plt.xlabel('frequency in Hz') plt.ylabel('angle $\phi$ in coordinates of circle') plt.legend() ylims=plt.axes().get_ylim() plt.yticks((arange(9)-4)*0.5*pi, ['$-2\pi$','$-3\pi/2$','$-\pi$','$-\pi/2$','$0$','$\pi/2$','$\pi$','$3\pi/2$','$2\pi$']) plt.ylim(ylims) plt.title('RMS offset from smooth curve: {:.4f}'.format(rmsval)) if self.show: plt.show() else: plt.savefig(join(self.sdc.plotpath,'salpha','c{}_salpha_on_{}_circle'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240) plt.close()
def draw(cls, t_max, agents_proportions, suffix): color_set = ["green", "blue", "red"] for agent_type in range(3): plt.plot(np.arange(t_max), agents_proportions[:, agent_type], color=color_set[agent_type], linewidth=1.0) plt.ylim([0, 1]) # plt.suptitle('Direct choices proportion per type of agents', fontsize=14, fontweight='bold') # plt.legend(loc='lower left', frameon=False) plt.savefig("figure_{}.pdf".format(suffix))
def drawAdoptionNetworkMPL(G, fnum=1, show=False, writeFile=None): """Draws the network to matplotlib, coloring the nodes based on adoption. Looks for the node attribute 'adopted'. If the attribute is True, colors the node a different color, showing adoption visually. This function assumes that the node attributes have been pre-populated. :param networkx.Graph G: Any NetworkX Graph object. :param int fnum: The matplotlib figure number. Defaults to 1. :param bool show: :param str writeFile: A filename/path to save the figure image. If not specified, no output file is written. """ Gclean = G.subgraph([n for n in G.nodes() if n not in nx.isolates(G)]) plt.figure(num=fnum, figsize=(6,6)) # clear figure plt.clf() # Blue ('b') node color for adopters, red ('r') for non-adopters. nodecolors = ['b' if Gclean.node[n]['adopted'] else 'r' \ for n in Gclean.nodes()] layout = nx.spring_layout(Gclean) nx.draw_networkx_nodes(Gclean, layout, node_size=80, nodelist=Gclean.nodes(), node_color=nodecolors) nx.draw_networkx_edges(Gclean, layout, alpha=0.5) # width=4 # TODO: Draw labels of Ii values. Maybe vary size of node. # TODO: Color edges blue based on influences from neighbors influenceEdges = [] for a in Gclean.nodes(): for n in Gclean.node[a]['influence']: influenceEdges.append((a,n)) if len(influenceEdges)>0: nx.draw_networkx_edges(Gclean, layout, alpha=0.5, width=5, edgelist=influenceEdges, edge_color=['b']*len(influenceEdges)) #some extra space around figure plt.xlim(-0.05,1.05) plt.ylim(-0.05,1.05) plt.axis('off') if writeFile != None: plt.savefig(writeFile) if show: plt.show()
def update_img((expected, output)): plt.cla() plt.ylim((vmin, vmin+vmax)) plt.xlim((vmin, vmin+vmax)) ax = fig.add_subplot(111) plt.plot([vmin, vmin+vmax], [vmin, vmin+vmax]) ax.grid(True) plt.xlabel("expected output") plt.ylabel("network output") plt.legend() expected = expected*vmax + vmin output = output*vmax + vmin #scat.set_offsets((expected, output)) scat = ax.scatter(expected, output) return scat
def plot_baseline(data, plate_name, save_folder = r'Figures/'): """ """ colors = ((0.2, 0.2, 0.2), (0.5, 0.5, 0.5), (0.7, 0.7, 0.7), (0.3, 0.3, 0.3)) names = data.keys() names.sort() fig, axs = plt.subplots(figsize=(8,3)) for index, name in enumerate(names): for value in data[name]['original_data']: plot_color = colors[index % len(colors)] if abs(value - data[name]['mean'][0]) > data[name]['std'][0] * 2.0: axs.plot([value], [index], 'ko', markerfacecolor = [1,1,1]) else: axs.plot([value], [index], 'ko', color = plot_color) axs.plot([data[name]['mean'][0] for _ in xrange(2)], [index-0.25, index+0.25], 'k-') axs.plot([data[name]['mean'][0] - data[name]['std'][0] for _ in xrange(2)], [index-0.25, index+0.25], 'k--') axs.plot([data[name]['mean'][0] + data[name]['std'][0] for _ in xrange(2)], [index-0.25, index+0.25], 'k--') plt.yticks([i for i in xrange(len(names))], names, size = 10) plt.title(plate_name) plt.ylim(-0.5,len(names)-0.5) plt.xlabel('Fluorescent intensity') plt.tight_layout() save_filename = save_folder + 'baseline_average' pdf = PdfPages(save_filename.split('.')[0] + '.pdf') pdf.savefig(fig) pdf.close() plt.savefig(save_filename) # return None
def __init__(self, width): # set plot to animated self.x1s = [0] self.y1s = [0] self.x2s = [0] self.y2s = [0] self.x3s = [0] self.y3s = [0] self.plt1, self.plt2, self.plt3 = plt.plot( self.x1s, self.y1s, 'rx', self.x2s, self.y2s, 'b.', self.x3s, self.y3s, 'gs', alpha=0.05, linewidth=3) self.latest = deque([0] * 20) self.plt3.set_alpha = 0.8 plt.ylim([0, 100]) plt.xlim([0, width]) plt.ion()
def _plot_base(dep, val, deplim_small, xlim_small, xlabel): plt.subplot(1,2,1) plt.plot(val, dep) plt.gca().invert_yaxis() plt.grid('on') plt.ylabel('depth/km') plt.xlabel(xlabel) locs, labels = plt.xticks() plt.setp(labels, rotation=-45) plt.subplot(1,2,2) plt.plot(val, dep) plt.gca().invert_yaxis() plt.grid('on') plt.ylim(deplim_small) plt.xlim(xlim_small) plt.xlabel(xlabel) locs, labels = plt.xticks() plt.setp(labels, rotation=-45)
def draw_partitioned_graph(G, partition_obj, layout=None, labels=None,layout_type='spring', node_size=70, node_alpha=0.7, cmap=plt.get_cmap('jet'), node_text_size=12, edge_color='blue', edge_alpha=0.5, edge_tickness=1, edge_text_pos=0.3, text_font='sans-serif'): # if a premade layout haven't been passed, create a new one if not layout: if graph_type == 'spring': layout=nx.spring_layout(G) elif graph_type == 'spectral': layout=nx.spectral_layout(G) elif graph_type == 'random': layout=nx.random_layout(G) else: layout=nx.shell_layout(G) # prepare the partition list noeds and colors list_nodes, node_color = partition_to_draw(partition_obj) # draw graph nx.draw_networkx_nodes(G,layout,list_nodes,node_size=node_size, alpha=node_alpha, node_color=node_color, cmap = cmap) nx.draw_networkx_edges(G,layout,width=edge_tickness, alpha=edge_alpha,edge_color=edge_color) #nx.draw_networkx_labels(G, layout,font_size=node_text_size, # font_family=text_font) if labels is None: labels = range(len(G)) edge_labels = dict(zip(G, labels)) #nx.draw_networkx_edge_labels(G, layout, edge_labels=edge_labels, # label_pos=edge_text_pos) # show graph plt.axis('off') plt.xlim(0,1) plt.ylim(0,1)
def plot_vel_decomposition(self, site, cmpt, loc=0, leg_fs=7, if_ylim=False ): y = self.plot_pred_vel_added(site, cmpt, label='total') y += self.plot_vel_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_vel_E_cumu_slip(site, cmpt, color='green') y += self.plot_vel_R_aslip(site, cmpt, color='black') plt.grid('on') if if_ylim: plt.ylim(calculate_lim(y)) plt.ylabel(r'mm/yr') plt.legend(loc=loc, prop={'size':leg_fs}) plt.gcf().autofmt_xdate() plt.title('Cumulative Disp.: {site} - {cmpt}'.format( site = get_site_true_name(site_id=site), cmpt = cmpt ))
def plot_cumu_disp_decomposition(self, site, cmpt, loc=2, leg_fs=7, if_ylim=False, added_label = None, ): self.plot_cumu_obs_linres(site, cmpt) y = self.plot_cumu_disp_pred_from_result_file(site, cmpt, label='pred.') y += self.plot_R_co(site, cmpt, style='-^', label='Rco', color='orange') y += self.plot_E_cumu_slip(site, cmpt, color='green') plt.grid('on') if if_ylim: plt.ylim(calculate_lim(y)) self.plot_cumu_disp_pred_added(site, cmpt, label=added_label) plt.ylabel(r'm') plt.legend(loc=loc, prop={'size':leg_fs}) plt.gcf().autofmt_xdate() plt.title('Cumulative Disp.: {site} - {cmpt}'.format( site = get_site_true_name(site_id=site), cmpt = cmpt ))
def plot(mot_results): bool_mot_results = np.asarray(mot_results) == 1 t_max = len(bool_mot_results) average_t = np.zeros(t_max) time_window = 10 for t in range(t_max): if t < time_window: average_t[t] = np.mean(bool_mot_results[:t+1]) else: average_t[t] = np.mean(bool_mot_results[t-10:t+1]) try: plt.plot(np.arange(t_max), average_t, linewidth=2) plt.ylim([-0.01, 1.01]) plt.show("figure.pdf") except: print("Could not show the figure but here are the results:") print(average_t)
label='obs.') plt.plot_date(days + vj.adjust_mjd_for_plot_date, y1 + y2, '-', lw=3, color='red', label='EXP1 + EXP2') plt.plot_date(days + vj.adjust_mjd_for_plot_date, y1, '-', color=r'green', label='EXP1') plt.plot_date(days + vj.adjust_mjd_for_plot_date, y2, '-', color=r'orange', label='EXP2') plt.ylim([0, 1]) plt.grid('on') plt.legend(loc=2, prop={'size': 6}) ax2 = plt.subplot(212, sharex=ax1) pplt = vj.inv.PredictedTimeSeriesPlotter( '../../inversions/inversion10/iter2/run7/analysis/pred_disp/~pred_disp.db') pplt.plot_post_disp_decomposition(site, cmpt) plt.title('') plt.ylim([0, 1]) plt.savefig('2EXPs_vs_pred_%s-%s.png' % (site, cmpt)) plt.savefig('2EXPs_vs_pred_%s-%s.pdf' % (site, cmpt)) plt.show()
from datetime import date import numpy as np from pylab import plt import viscojapan as vj site = 'J550' cmpt = 'e' tp = np.loadtxt('../../tsana/pre_fit/linres/{site}.{cmpt}.lres'.\ format(site=site, cmpt=cmpt)) days = tp[:,0] yres = tp[:,2] plt.plot_date(days + vj.adjust_mjd_for_plot_date, yres, 'x') plt.grid('on') plt.axvline(date(2011,3,11),color='r', ls='--') plt.ylim([-1, 7]) plt.xlim((date(2010,1,1), date(2015,1,1))) plt.gcf().autofmt_xdate() plt.ylabel('m') plt.title('%s - %s'%(site, cmpt)) plt.savefig('%s_%s.pdf'%(site, cmpt)) plt.show()
def getcutpoint(inputdatapath): """ 根据时间间隔找到分割点,生成最近一次的图像和全部综合图像并返回 """ totalmen = gettotalmem(getdatapath(inputdatapath)) inputdf = getdatadf(getdatapath(inputdatapath)) ds = inputdf['time'] - inputdf['time'].shift() deltads = ds[ds > getdelta()] outlst = list() for ix in deltads.index: ipos = list(inputdf.index).index(ix) # 处理内存占满(但未重启)的特殊情况 if inputdf.iloc[ipos - 1]['freeper'] == 0: continue print() print(ipos, ix, deltads[ix]) outlst.append(ipos) # bfdf = inputdf[inputdf.index >= (ix - deltads[ix] + pd.Timedelta(minutes=-5))] # tmpdf = bfdf[bfdf.index < (ix + pd.Timedelta(minutes=5))] # print(tmpdf) outlst.insert(0, 0) outlst.append(inputdf.shape[0]) plt.rcParams['font.sans-serif'] = 'SimHei' olen = len(outlst) if olen == 2: picheight = 1 elif olen == 3: picheight = 2 else: picheight = 3 plt.figure(figsize=(10, 10 * picheight)) imgpath = getdirmain() / 'img' / 'freemen.png' touchfilepath2depth(imgpath) # fig, ax1 = plt.subplots() # 针对数据集周期次数进行处理,主要是处理小于三次的情况 if len(outlst) > 3: plt.subplot(311) elif len(outlst) == 3: plt.subplot(211) # 最近数据集图形化输出 plt.ylabel(f'空闲内存百分比({totalmen}G)') cutdf = inputdf.iloc[outlst[-2]:outlst[-1]] plt.plot(cutdf.index, cutdf['freeper']) plt.ylim(0, 100) plt.title('最近一次运行') plt.annotate(cutdf.index[0].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[0], cutdf.iloc[0, 1]]) plt.annotate(cutdf.index[-1].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[-1], cutdf.iloc[-1, 1]]) # 针对单次(一般也是首次运行)数据集进行处理 if len(outlst) == 2: plt.savefig(imgpath) return str(imgpath) # 针对数据集周期次数进行处理,主要是处理小于三次的情况 if len(outlst) == 3: plt.subplot(212) elif len(outlst) > 3: plt.subplot(312) plt.ylabel(f'空闲内存百分比({totalmen}G)') plt.ylim(0, 100) plt.title('最近两次运行') twolst = outlst[-3:] for i in range(len(twolst) - 1): cutdf = inputdf.iloc[twolst[i]:twolst[i + 1]] plt.plot(cutdf.index, cutdf['freeper']) plt.annotate(cutdf.index[0].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[0], cutdf.iloc[0, 1]]) plt.annotate(cutdf.index[-1].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[-1], cutdf.iloc[-1, 1]]) # 综合(全部)数据集图形输出 # 针对仅有两次数据集进行处理 if len(outlst) == 3: plt.savefig(imgpath) return str(imgpath) else: plt.subplot(313) plt.ylabel(f'空闲内存百分比({totalmen}G)') plt.ylim(0, 100) plt.title('历次运行') for i in range(len(outlst) - 1): cutdf = inputdf.iloc[outlst[i]:outlst[i + 1]] plt.plot(cutdf.index, cutdf['freeper']) plt.annotate(cutdf.index[0].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[0], cutdf.iloc[0, 1]]) plt.annotate(cutdf.index[-1].strftime("%y-%m-%d %H:%M"), xy=[cutdf.index[-1], cutdf.iloc[-1, 1]]) plt.savefig(imgpath) return str(imgpath)
plt.plot(t_pred,y_pred,'-o') tp = np.loadtxt('../../../../../../tsana/sea_floor/cumu_post/%s.original'%site) t = tp[:,0] if cmpt == 'e': nth = 1 elif cmpt == 'n': nth = 2 elif cmpt == 'u': nth = 3 y = tp[:,nth] plt.plot(t,y,'x',color='red') y1 = np.amin(list(y_pred)+list(y)) y2 = np.amax(list(y_pred)+list(y)) dy = y2 - y1 y1 -= bd*dy y2 += bd*dy plt.ylim((y1,y2)) plt.grid('on') plt.xlabel('days after mainshock') plt.ylabel('displacement (m)') name = vj.sites_db.get_site_true_name(site) plt.title('%s-%s'%(name, cmpt)) plt.savefig('plots_seafloor/%s-%s.pdf'%(site,cmpt)) #plt.show() plt.close()
distance = np.sqrt( ( rn**2 ).sum( axis = 1 ) ) frac = ( distance <= 1.0 ).sum( ) / len( distance ) pi_mcs = frac * 4 #....... PLOT ............................. fig = plt.figure( figsize = ( 8, 8 ) ) ax = fig.add_subplot( 1, 1, 1 ) circ = plt.Circle( (0,0), radius = 1, edgecolor = 'g', lw = 2.0, facecolor = 'None' ) box = plt.Rectangle( (-1,-1), 2,2, edgecolor = 'b', alpha = 0.3 ) ax.add_patch( circ ) ax.add_patch( box ) plt.plot( rn[ :, 0], rn[:, 1 ], 'r.' ) plt.xlim( -1.1, 1.1) plt.ylim( -1.1, 1.1) # .............................................................. # ............ MonteCarlo function: NumPy....................... # .............................................................. def mcs_pi_np( n ): rn = [ ( rd.random() * 2 - 1, rd.random() * 2 - 1 ) for _ in range( n )] rn = np.array( rn ) distance = np.sqrt( ( rn**2 ).sum( axis = 1 ) ) frac = ( distance <= 1.0 ).sum( ) / n return frac * 4 # ................................................................ # ............ MonteCarlo function: for loop, no vectorization ... # ................................................................ def mcs_pi_py( n ):
tp = np.loadtxt( '../../../../../../tsana/sea_floor/cumu_post/%s.original' % site) t = tp[:, 0] if cmpt == 'e': nth = 1 elif cmpt == 'n': nth = 2 elif cmpt == 'u': nth = 3 y = tp[:, nth] plt.plot(t, y, 'x', color='red') y1 = np.amin(list(y_pred) + list(y)) y2 = np.amax(list(y_pred) + list(y)) dy = y2 - y1 y1 -= bd * dy y2 += bd * dy plt.ylim((y1, y2)) plt.grid('on') plt.xlabel('days after mainshock') plt.ylabel('displacement (m)') name = vj.sites_db.get_true_name_by_id(site) plt.title('%s-%s' % (name, cmpt)) plt.savefig('plots_seafloor/%s-%s.pdf' % (site, cmpt)) #plt.show() plt.close()
color='#d95f02', linestyle='--') # when using twinaxes, plt.legend() does not work lines, labels = ax1.get_legend_handles_labels() lines2, labels2 = ax2.get_legend_handles_labels() ax2.legend(lines + lines2, labels + labels2) ax1.set_xlabel('pert_scale svd') ax2.set_xlabel('pert_scale rand') ax1.axvline(best_pert_scale_svd, linestyle=':', color='#1b9e77') ax2.axvline(best_pert_scale_rand, linestyle=':', color='#7570b3') ax1.xaxis.labelpad = 0 ax2.xaxis.labelpad = 0 ax1.set_xscale('log') ax2.set_xscale('log') ax2.set_xlim(xmax=0.6) plt.ylim(ymax=1800) sns.despine() ax1.set_ylabel('rmse | spread [$m^2/s^2$]') plt.title(f'leadtime:{leadtime}h') plt.savefig( f'{plotdir}/era5_pert_scale_vs_skill_spread_leadtime{leadtime}.svg') fig, ax1 = plt.subplots(figsize=figsize) ax2 = plt.twiny(ax1) ax2.xaxis.set_ticks_position('bottom') ax2.xaxis.set_label_position('bottom') ax2.spines['bottom'].set_position(('outward', 40)) ax1.plot(sub_df['pert_scale'], sub_df['corr_svd'], label='svd', color='#1b9e77')
nn.Softmax th.Tensor.softmax plott = lambda *l, **kv: plt.plot( *map2(lambda t: npa(t) if isinstance(t, th.Tensor) else t, l), **kv) npoint = 500 xrange = -4, 3 #yrange = -2, 3 yrange = xrange xs = tht - np.linspace(*xrange, npoint) ys = tht - np.linspace(*yrange, npoint) plt.xlim(*xrange) plt.ylim(*yrange) xs.requires_grad = True t = 1 plott(xs, xs == 1000000.11, 'k') plott(xs == 1000000.11, ys, 'k') for it, t in enumerate([1, .5, 3][:2]): color = 'bgry'[it] prob = 1 / (1 + th.exp(-xs * t)) loss = -th.log(prob) allloss = loss.sum() allloss.backward()
for i in range(h): yc = ylim[1] - i * dy for j in range(m): xc = xlim[0] + j * dx dat[j * n] = xc dat[j * n + 1] = yc #print "first DNA: ",dat[0],dat[1]; #print "2nd DNA: ",dat[2],dat[3]; #print "last DNA: ",dat[2*m-2],dat[2*m-1]; r1 = tf.test_func(dat, f, ct.c_int(n), ct.c_int(m), ct.c_int(k)) rarr[i, :] = [f[j] for j in range(m)] plt.pcolor(x, y, flipud(rarr), vmin=np.min(rarr), vmax=np.max(rarr)) plt.colorbar() plt.xlim(xlim) plt.ylim(ylim) plt.xlabel(r'$x_1$') plt.ylabel(r'$x_2$') plt.title('CEC-2013 test function suite:\nno. {0}: {1}'.format(k, fnames[k])) plt.suptitle('evaluated using ctypes', x=0.02, y=0.02, ha='left', va='bottom', fontsize=9) #plt.show() plt.savefig('./pics/test_func_' + str(k).zfill(2) + '_using_ctypes_zoomlevel_2.png') plt.clf() plt.close('all')
pred = df_sub['prediction'] truth = df_sub['truth'] mse_test = metrics.mean_squared_error(truth, pred) rmse_test = np.sqrt(mse_test) print('mse on test data:', mse_test) print('rmse on test data', rmse_test) r2 = np.corrcoef(truth, pred)[0, 1]**2 plt.figure() sns.regplot(pred, truth, ci=ci) sns.despine() plt.ylabel('observed relative production loss [%]') plt.xlabel('predicted relative production loss [%]') plt.xlim((-5, 100)) plt.ylim((-5, 100)) plt.text(0.1, 0.9, 'R^2={:.2f}'.format(r2), transform=plt.gca().transAxes) plt.text(0.1, 0.85, 'RMSE={:.2f}'.format(rmse_test), transform=plt.gca().transAxes) plt.text(0.1, 0.8, 'N={:.2f}'.format(len(y_test)), transform=plt.gca().transAxes) plt.savefig('plots/'+target_var + '_vs_' + target_var + '_predicted_on_test_set_lead_hour' + \ str(lead_hour) + '_ensmean_'+name+'.svg') plt.figure() # two plit both mse and psersistenc_mse in one barplot, we need to rearrange # and melt the dataframe
def weatherstat(df, destguid=None): dfduplicates = df.groupby('date').apply( lambda d: tuple(d.index) if len(d.index) > 1 else None).dropna() log.info(dfduplicates) df.drop_duplicates(inplace=True) # 去重,去除可能重复的天气数据记录,原因可能是邮件重复发送等 # print(df.head(30)) df['date'] = df['date'].apply(lambda x: pd.to_datetime(x)) # 日期做索引,去重并重新排序 df.index = df['date'] df = df[~df.index.duplicated()] df.sort_index(inplace=True) # print(df) df.dropna(how='all', inplace=True) # 去掉空行,索引日期,后面索引值相同的行会被置空,需要去除 # print(len(df)) # df['gaowen'] = df['gaowen'].apply(lambda x: np.nan if str(x).isspace() else int(x)) #处理空字符串为空值的另外一骚 df['gaowen'] = df['gaowen'].apply(lambda x: int(x) if x else None) # 数字串转换成整数,如果空字符串则为空值 df['diwen'] = df['diwen'].apply(lambda x: int(x) if x else None) df['fengsu'] = df['fengsu'].apply(lambda x: int(x) if x else None) df['shidu'] = df['shidu'].apply(lambda x: int(x) if x else None) # df['gaowen'] = df['gaowen'].astype(int) # df['diwen'] = df['diwen'].astype(int) # df['fengsu'] = df['fengsu'].astype(int) # df['shidu'] = df['shidu'].astype(int) df.fillna(method='ffill', inplace=True) # 向下填充处理可能出现的空值,bfill是向上填充 df['wendu'] = (df['gaowen'] + df['diwen']) / 2 df['richang'] = df['sunoff'] - df['sunon'] df['richang'] = df['richang'].astype(int) df['wendu'] = df['wendu'].astype(int) # print(df.tail(30)) df_recent_year = df.iloc[-365:] # print(df_recent_year) # print(df[df.gaowen == df.iloc[-364:]['gaowen'].max()]) # df_before_year = df.iloc[:-364] plt.figure(figsize=(16, 20)) ax1 = plt.subplot2grid((4, 2), (0, 0), colspan=2, rowspan=2) ax1.plot(df['gaowen'], lw=0.3, label=u'日高温') ax1.plot(df['diwen'], lw=0.3, label=u'日低温') ax1.plot(df['wendu'], 'g', lw=0.7, label=u'日温度(高温低温平均)') quyangtianshu = 10 ax1.plot(df['wendu'].resample('%dD' % quyangtianshu).mean(), 'b', lw=1.2, label='日温度(每%d天平均)' % quyangtianshu) ax1.plot(df[df.fengsu > 5]['fengsu'], '*', label='风速(大于五级)') plt.legend(loc=2) # 起始统计日 kedu = df.iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--', lw=0.4) ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='Wheat') fsize = 8 txt = str(kedu['wendu']) ax1.annotate(txt, xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(-(len(txt) * fsize), +20), textcoords='offset points', fontsize=fsize, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 去年今日,如果数据不足一年,取今日 if len(df) >= 366: locqnjr = -365 else: locqnjr = -1 kedu = df.iloc[locqnjr] # kedu = df.iloc[-364] print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='Wheat') ax1.annotate(str(kedu['wendu']), xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(-5, +20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -35), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 今日 kedu = df.iloc[-1] # print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='BlueViolet') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-10, +20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) ax1.scatter([ kedu['date'], ], [kedu['wendu']], 50, color='BlueViolet') ax1.annotate(str(kedu['wendu']), xy=(kedu['date'], kedu['wendu']), xycoords='data', xytext=(10, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='BlueViolet') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-10, -20), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -35), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最近一年最高温 kedu = df_recent_year[df_recent_year.gaowen == df_recent_year.iloc[-364:] ['gaowen'].max()].iloc[0] # print(kedu) ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='Wheat') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最近一年最低温 kedu = df_recent_year[df_recent_year.diwen == df_recent_year.iloc[-364:] ['diwen'].min()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='Wheat') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最高温 kedu = df[df.gaowen == df['gaowen'].max()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['gaowen']], 50, color='Wheat') ax1.annotate(str(kedu['gaowen']), xy=(kedu['date'], kedu['gaowen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) # 最低温 kedu = df[df.diwen == df['diwen'].min()].iloc[0] ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--') ax1.scatter([ kedu['date'], ], [kedu['diwen']], 50, color='Wheat') ax1.annotate(str(kedu['diwen']), xy=(kedu['date'], kedu['diwen']), xycoords='data', xytext=(-20, +5), textcoords='offset points', arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='Purple')) dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day) ax1.annotate(dates, xy=(kedu['date'], 0), xycoords='data', xytext=(-10, -20), textcoords='offset points', fontsize=8, arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=0")) ax1.set_ylabel(u'(摄氏度℃)') ax1.grid(True) ax1.set_title(u'最高气温、最低气温和均值温度图') ax3 = plt.subplot2grid((4, 2), (2, 0), colspan=2, rowspan=2) # print(type(ax3)) ax3.plot(df_recent_year['shidu'], 'c.', lw=0.3, label=u'湿度') ax3.plot(df_recent_year['shidu'].resample('15D').mean(), 'g', lw=1.5) ax3.set_ylabel(u'(百分比%)') ax3.set_title(u'半月平均湿度图') img_wenshifeng_path = dirmainpath / "img" / 'weather' / 'wenshifeng.png' img_wenshifeng_path_str = str(img_wenshifeng_path) touchfilepath2depth(img_wenshifeng_path) plt.legend(loc='lower left') plt.savefig(img_wenshifeng_path_str) imglist = list() imglist.append(img_wenshifeng_path_str) plt.close() plt.figure(figsize=(16, 10)) fig, ax1 = plt.subplots() plt.plot(df['date'], df['sunon'], lw=0.8, label=u'日出') plt.plot(df['date'], df['sunoff'], lw=0.8, label=u'日落') ax = plt.gca() # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算 ax.yaxis.set_major_formatter( FuncFormatter(lambda x, pos: "%02d:%02d" % (int(x / 60), int(x % 60)))) # 主刻度文本用pi_formatter函数计算 plt.ylim((0, 24 * 60)) plt.yticks(np.linspace(0, 24 * 60, 25)) plt.xlabel(u'日期') plt.ylabel(u'时刻') plt.legend(loc=6) plt.title(u'日出日落时刻和白天时长图') plt.grid(True) ax2 = ax1.twinx() print(ax2) plt.plot(df_recent_year['date'], df_recent_year['richang'], 'r', lw=1.5, label=u'日长') ax = plt.gca() # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算 ax.yaxis.set_major_formatter( FuncFormatter(lambda x, pos: "%02d:%02d" % (int(x / 60), int(x % 60)))) # 主刻度文本用pi_formatter函数计算 # ax.set_xticklabels(rotation=45, horizontalalignment='right') plt.ylim((3 * 60, 12 * 60)) plt.yticks(np.linspace(3 * 60, 15 * 60, 13)) plt.ylabel(u'时分') plt.legend(loc=5) plt.grid(True) # plt.show() img_sunonoff_path = dirmainpath / 'img' / 'weather' / 'sunonoff.png' img_sunonoff_path_str = str(img_sunonoff_path) touchfilepath2depth(img_sunonoff_path) plt.savefig(img_sunonoff_path_str) imglist.append(img_sunonoff_path_str) plt.close() imglist2note(get_notestore(), imglist, destguid, '武汉天气图')
def histbin(self, var1, var2, force=False): fig_name = "{}/hist_median_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return if var1 == "customer_extra_view_choices" and var2 == "delta_position": x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) n_bin = 5 a = np.linspace(0, 10, n_bin + 1) b = np.zeros(n_bin) for i in range(n_bin): yo = list() for idx, xi in enumerate(x): if a[i] <= xi < a[i + 1]: yo.append(y[idx]) b[i] = np.median(yo) if len(yo) else 0 # ----- # fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) ax.bar(a[:-1] + (a[1] - a[0]) / 2, b, a[1] - a[0], color='grey') plt.savefig(fig_name) # --- # if self.display: plt.show() plt.close() # ---- # b = np.zeros(n_bin) c = np.zeros(n_bin) for i in range(n_bin): yo = list() for idx, xi in enumerate(x): if a[i] <= xi < a[i + 1]: yo.append(y[idx]) b[i] = np.mean(yo) if len(yo) else 0 c[i] = np.std(yo) if len(yo) else 0 # ----- # fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) ax.bar(a[:-1] + (a[1] - a[0]) / 2, b, a[1] - a[0], color='grey', yerr=c) plt.savefig("{}/hist_mean_{}_{}.pdf".format( self.fig_folder, var1, var2)) # --- # if self.display: plt.show() plt.close()
#flat转置为1维数组 #print("x1=\n",x1.flat) grid_hat = clf.predict(grid_test) print('grid_hat = \n', grid_hat) # 预测分类值 grid_hat = grid_hat.reshape(x1.shape) # 使之与输入的形状相同 mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False #绘制 #pcolormesh(x,y,z,cmap)这里参数代入x1,x2,grid_hat,cmap=cm_light绘制的是背景。 #scatter中edgecolors是指描绘点的边缘色彩,s指描绘点的大小,cmap指点的颜色。 #xlim指图的边界。 # cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF']) #绿,红,紫 cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b']) plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light) print(grid_hat) plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=50, cmap=cm_dark) # 样本 plt.scatter(x_test[:, 0], x_test[:, 1], s=120, facecolors='none', zorder=10) # 圈中测试集样本 plt.xlabel(u'花萼长度', fontsize=13) plt.ylabel(u'花萼宽度', fontsize=13) plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) plt.title(u'鸢尾花SVM二特征分类', fontsize=15) # plt.grid() plt.show()
from pylab import plt import viscojapan as vj from viscojapan.tsana.post_fit.post import fit_post site = 'J550' cmpt = 'e' ylim = (-0.1, 1) pplt = vj.inv.PredictedTimeSeriesPlotter( partition_file = 'deformation_partition.h5', result_file = 'nrough_06_naslip_11.h5' ) pplt.plot_post_disp_decomposition(site, cmpt, marker_for_obs='.') plt.ylim(ylim) def ajust_xaxis_tick_labels(ax): for tick in ax.xaxis.get_major_ticks(): tick.label.set_fontsize(8) # specify integer or one of preset strings, e.g. #tick.label.set_fontsize('x-small') tick.label.set_rotation('vertical') ajust_xaxis_tick_labels(plt.gca()) plt.savefig('model_prediction_%s-%s.png'%(site, cmpt)) plt.savefig('model_prediction_%s-%s.pdf'%(site, cmpt)) plt.show()
def run(*args): import pkg_resources if args[0] == 'test': TEST_PATH = pkg_resources.resource_filename('confine', 'TEST/') dir_in = TEST_PATH + 'test.csv' f = 'test' lcc_min = 50 lcc_max = 350 else: f = args[0] dir_in = str(raw_input("Where the file is located in? ")) print "You entered: ------", str(f), ' ------' lcc_min = int( raw_input( "Enter the minimum size of LCC, we recommend a number between 30 and 50: " )) lcc_max = int( raw_input( "Enter the maximum size of LCC, we recommend a number between 300 and 500: " )) print '.....loading data.....' import os import pickle import time start_time = time.time() NET_PATH = pkg_resources.resource_filename('confine', 'NET/') id_to_sym = pickle.load(open(NET_PATH + 'id_to_sym_human.p', 'r')) G = pickle.load(open(NET_PATH + 'PPI_2015_raw.p', 'r')) file = open(dir_in, "r") initial_data = file.read().splitlines() file.close() threshold = 0.05 gene = [] pval = [] for row in initial_data: n = row.strip().split(',') p = float(n[1].strip()) g = int(n[0].strip()) if p <= threshold: gene.append(g) pval.append(p) print 'Number of genes with P.val<0.05: ', len(gene) print '.....Identifying disease module.....' data = zip(gene, pval) #--------------------- from func import CONFINE as conf result = conf(data, G, lcc_min, lcc_max) z_list = result[0] pval_cut_list = result[1] sig_Cluster_LCC = result[2] z_score = z_list[result[3]] p_val_cut = pval_cut_list[result[3]] print '--------------------' print 'LCC size: ', len(sig_Cluster_LCC.nodes()) print 'Z-score: ', z_score print 'P.val cut-off: ', p_val_cut print '--------------------' directory_name = f + '_' + str(time.time()) if not os.path.exists(directory_name): os.makedirs(directory_name) b = open(directory_name + '/LCC_' + f + '.txt', "w") for node in sig_Cluster_LCC.nodes(): try: print >> b, str(id_to_sym[int(node)]) + ',' + str(int(node)) except KeyError: print >> b, ' ' + ',' + str(int(node)) b.close() from pylab import plt, matplotlib fig = plt.figure(figsize=(10, 10)) ax = fig.add_subplot(111) #----------------------------------------------------plotting-------- ax.plot(pval_cut_list, z_list, 'o', color='saddlebrown', markersize=4) plt.axvline(x=p_val_cut, color='r', linestyle='--') ax.set_xlabel('P.value cut-off', fontsize=25, fontweight='bold', labelpad=18) ax.set_ylabel('Z-Score', fontsize=25, fontweight='bold', labelpad=18) ax.set_title('LCC' + ' = ' + str(len(sig_Cluster_LCC.nodes())) + ' ,' + ' Z-Score' + ' = ' + str("{0:.3f}".format(round(z_score, 4))), fontsize=20, fontweight='bold') ax.grid(True) plt.ylim(min(z_list) - min(z_list) / 5, max(z_list) + max(z_list) / 3) plt.savefig(directory_name + '/' + f + '.png', dpi=150, bbox_inches='tight') plt.close() print("--- %s seconds ---" % (time.time() - start_time))
s='lowermantle \n$ \\eta = 1 \\times 10^{21}$', fontsize=12) ax1.set_ylabel('depth (km)') # plot ax2 - shear and bulk modulus axhspan_for_viscosity(ax2) plt.setp(ax2.get_yticklabels(), visible=False) ax2.plot(shear, dep, '--', label='shear modulus', dashes=(3,3), lw=2) ax2.plot(bulk, dep, label='bulk modulus', lw=2) plt.setp(ax2.get_xticklabels(), rotation=90) ax2.set_xlabel('Shear/bulk modulus (GPa)') ax2.legend(prop={'size':8}) ax2.set_xlim([0, 460]) # plot ax3 - density axhspan_for_viscosity(ax3) plt.setp(ax3.get_yticklabels(), visible=False) ax3.plot(den, dep, lw=2) ax3.set_xlim([2.2, 5]) plt.setp(ax3.get_xticklabels(), rotation=90) ax3.set_xlabel(r'Density $(g/cm^3)$') fig.subplots_adjust(wspace=.05) plt.ylim([-1000,0]) plt.savefig('earth_model.pdf') plt.savefig('earth_model.png') plt.show()
lines_shape = (18, 512) # The initial points lines_old_x=hx[0].reshape(lines_shape ).copy() lines_old_y=hy[0].reshape(lines_shape ).copy() # The final points lines_new_x=hx[-1].reshape(lines_shape ).copy() lines_new_y=hy[-1].reshape(lines_shape ).copy() c = 'r' fig = plt.figure() plt.subplot(121) for line_x,line_y in zip(lines_old_x,lines_old_y): plt.plot(line_x,line_y,c) plt.axis('scaled') q=100 plt.xlim(0-q,512+q) plt.ylim(0-q,512+q) plt.gca().invert_yaxis() c = 'b' plt.subplot(122) for line_x,line_y in zip(lines_new_x,lines_new_y): plt.plot(line_x,line_y,c) plt.axis('scaled') q=500 plt.xlim(0-q,512+q) plt.ylim(0-q,512+q) plt.gca().invert_yaxis() pylab.show()
np.sqrt(res_mse_ensmean_svd[ltime]).squeeze())[0, 1]) corrs_rand.append( np.corrcoef( np.sqrt(res_ensvar_rand[ltime]).squeeze(), np.sqrt(res_mse_ensmean_rand[ltime]).squeeze())[0, 1]) corrs_netens.append( np.corrcoef( np.sqrt(res_ensvar_netens[ltime]).squeeze(), np.sqrt(res_mse_netens[ltime]).squeeze())[0, 1]) plt.figure() plt.plot(leadtimes[1:], corrs_svd, label='svd', color='#1b9e77') plt.plot(leadtimes[1:], corrs_netens, label='netens', color='#d95f02') plt.plot(leadtimes[1:], corrs_rand, label='rand', color='#7570b3') plt.legend() plt.ylim((-0.2, 1)) plt.xlabel('leadtime [MTU]') plt.ylabel('correlation') sns.despine() plt.title(f'n_ens:{n_ens}') plt.savefig( f'{outdir}/leadtime_vs_error_spread_corr_netens_{param_string}_{test_startyear}-{test_endyear}_{svd_params}.svg' ) plt.close('all') df = pd.DataFrame({ 'leadtime': leadtimes, 'rmse_ctrl': np.sqrt(np.mean(res_mse_ctrl, 1)).squeeze(),
plt.suptitle(f'GEFS reforecasts') plt.subplot(3, 2, 3) plt.plot(sub['leadtime'], sub['crps'] * 9.81, color='black') plt.ylabel('crps [$m^2/s^2$]') plt.xlabel('leadtime [h]') plt.xlabel('leadtime [h]') sns.despine() plt.subplot(3, 2, 5) plt.plot(sub['leadtime'][1:], sub['corr'][1:], label='svd', color='black') plt.xlabel('leadtime [h]') plt.ylabel('spread-error correlation') sns.despine() plt.ylim((0, 0.9)) plt.xlim((0, 120)) ## n_ens vs rmse / corr/ crps sub_df = df # here we omit leadtime 0 (because we dont need and it makes the plots confusing, especially for corr) sub_df = sub_df[sub_df['leadtime'] != 0] plt.subplot(3, 2, 2) sns.lineplot('n_ens', 'rmse_ensmean', data=sub_df, hue='leadtime') sns.lineplot('n_ens', 'spread', data=sub_df, hue='leadtime', style=True, dashes=[(2, 2)], legend=False)
def runAnalysis( caseDir , backbone , timeFactor ): # Retrieve info on the peptide resNames = backbone.resnames() # Go through each residue connection for i in range( 1, len(resNames) ): # User info print "Plotting dihedrals for residue: "+str(i) # Paths for the two files psiPath = caseDir+"/analysis/data/psi_"+str(i) phiPath = caseDir+"/analysis/data/phi_"+str(i) # Common Plot command myPlot.plotData( caseDir+"/analysis/plots" , "Dihedral Angles. Res ID: "+str(i)+", Residue name: "+str(resNames[i]), ["$\Psi_"+str(i)+"$","$\Phi_"+str(i)+"$"], [ psiPath , phiPath ] , "Angle (Degrees)", xFactor = timeFactor, scatter = True , skipLines = 1, legendFrame = 1, legendAlpha = 1 ) # Create a Ramachandran plot ############################ # User info print "Creating a Ramachandran plot for residue: "+str(i) # Get the components components = [] for path in [ phiPath, psiPath ]: components.append([]) index = len(components)-1 with open(path, "r") as fi: lines = fi.readlines() for line in lines: temp = line.split() try: components[ index ].append( float( temp[1] ) ) except ValueError: print "Reading Header: ",line # Set to numpy np_arrays = [ np.array( component ) for component in components ] # Do the plotting title = "Ramachandran Plot. Res ID: "+str(i)+", Residue name: "+str(resNames[i]) pp = PdfPages( caseDir+"/analysis/plots/"+title+".pdf" ) fig = plt.figure() #figsize=(8,6) ax = fig.gca() ax.set_xlabel("$\Phi_"+str(i)+"$", fontsize=12) ax.set_ylabel("$\Psi_"+str(i)+"$", fontsize=12) # Create the histogram without plotting, so we can set the units properly boltzman = 0.0019872041 temperature = 300 H, xedges, yedges = np.histogram2d(np_arrays[1], np_arrays[0], bins=100 ) H_normalized = H/len(np_arrays[0]) H = -1 * boltzman * temperature * (np.log( H_normalized )-np.log(np.max(H_normalized))) # Now plot the 2d histogram img = ax.imshow(H, interpolation='nearest', origin='lower',extent=[yedges[0], yedges[-1],xedges[0], xedges[-1]] , rasterized=True ) colorbar = plt.colorbar(img, ax=ax) colorbar.set_label("Kcal / mol") # For normal histogram plot #plt.hist2d(np_arrays[0], np_arrays[1], bins=100) #plt.colorbar() plt.ylim([-180,180]) plt.xlim([-180,180]) plt.title( title ) plt.savefig(pp, format="pdf",dpi=150) pp.close()
_x[0] = np.linspace(m,M,100) _v = A.dot(_x).flatten() plt.plot(_x[0],_v) if 1: # plt.figure() plt.figure(1);plt.clf() plt.subplot(231) plt.plot(interval,src.cpu) plt.title('src') plt.subplot(232) # plt.plot(interval[1:],np.diff(src)/(interval[1]-interval[0])) dx=interval[1]-interval[0] plt.plot(interval[1:],np.diff(src.cpu.ravel())/dx) plt.title(" d/dx src") plt.ylim(0,.5) plt.subplot(233) plt.plot(np.linspace(cpa_space.XMINS[0],cpa_space.XMAXS[0], interval.size),v_dense.cpu.ravel()) plt.ylim(-1,1) plt.title('velocity') plt.subplot(234) plt.plot(interval,transformed.cpu) plt.ylim(0,1) plt.title('transformed') plt.subplot(235) # plt.plot(interval[1:],np.diff(transformed)/(interval[1]-interval[0])) dx=interval[1]-interval[0] plt.plot(interval[1:],np.diff(transformed.cpu.ravel())/dx) plt.ylim(0,5) plt.title('d/dx transformed')
bins = np.arange(-100, 100, 0.05) # fixed bin size plt.xlim([min(S[:, 0]) - 0.05, max(S[:, 0]) + 0.05]) plt.hist(S[:, 0], bins=bins, alpha=0.5) plt.title('RPCA Sparse Set Distribution (Fixed bin size)') plt.xlabel('Response Variable Only (bin size = 0.0.5)') plt.ylabel('Count') plt.show() plt.plot(S[:, 0]) plt.title('RPCA Sparse ($S_0$) Set of Fuel Demand') plt.xlabel('Observation') plt.ylabel('Scaled Response Variable') plt.ylim(0, 1.0) plt.show() plt.plot(L[:, 0]) plt.plot(L[:, 2] + 1) plt.plot(L[:, 3] + 2) plt.plot(L[:, 4] + 3) plt.plot(L[:, 5] + 4) plt.plot(L[:, 6] + 5) plt.plot(L[:, 7] + 6) plt.title('RPCA Low Rank ($L_0$) Set of Fuel Demand') plt.xlabel('Observation') plt.ylabel('Scaled Response Variable') plt.ylim(0, 7.0) plt.show()
plt.plot(n_samples_5_ami_scl, weihted_5_ami_scl, color=cnames['red'], marker='s', linestyle='dashed', label='5 taxonomies SCL') plt.plot(n_samples_5_ami_tca, weihted_5_ami_tca, color=cnames['green'], marker='s', linestyle='dashed', label='5 taxonomies TCA') plt.gca().yaxis.grid(True) plt.xlim(0, 7000) plt.ylim(0, 1.) plt.legend() plt.show() ########################################### font_prop = font_manager.FontProperties(size=12) xlabel('number of sampled data', fontproperties=font_prop) ylabel('accuracy', fontproperties=font_prop) title( 'accuracies for DoA methods with training on AMI and testing on the ICSI', fontproperties=font_prop) #plt.plot(n_samples_3_icsi_scl, weihted_3_icsi_scl, color=cnames['blue'], marker='s', linestyle='dashed', label='3 taxonomies SCL') plt.plot(n_samples_3_icsi_tca,
linestyle='--', zorder=0) plt.plot(sub_drop['leadtime'], sub_drop['spread_drop'], color=colors[2], linestyle='--') plt.plot(sub_netens['leadtime'], sub_netens['spread_netens'], color=colors[3], linestyle='--') if iplot == 0: plt.legend(loc='upper left', fontsize=14) plt.ylabel('rmse (solid) \n spread (dashed) [$m^2/s^2$]') sns.despine() plt.ylim(ymax=2000) plt.xlim((0, 120)) plt.title(f'selected on {optimization_var}') plt.subplot(3, 3, 4 + iplot) plt.plot(sub_svd['leadtime'], sub_svd['crps_svd'], label='svd', color=colors[0]) plt.plot(sub_rand['leadtime'], sub_rand['crps_rand'], label='rand', color=colors[1], zorder=1) plt.plot(sub_drop['leadtime'], sub_drop['crps_drop'],
plt.plot([0.119,0.119],[0,18.402],color='k',linestyle='--',linewidth=1, dashes = (10,10)) #Plot the W1 5 sigma line in this color space W1,=plt.plot(col,ch2prime, color='k',linestyle='-.',linewidth=1, dashes = [8,4,2,4]) #SpIES 5sigma line (CH2) plt.axhline(22.0, linestyle='--',linewidth=1, color='b', dashes = (10,10),label=r'SpIES 5$\sigma$') plt.xlabel(r'[3.6]$-$[4.5] Color') plt.ylabel('[4.5]') first_legend = plt.legend([WISE,W1],['Assef et al. 2013 limits',r'WISE W1 5$\sigma$'],loc=1) ax = plt.gca().add_artist(first_legend) plt.legend(loc=2,markerscale=2, scatterpoints=1) fig.set_size_inches(10.0,10.0) ax2.minorticks_on() ax2.yaxis.set_major_locator(majorLocator) ax2.yaxis.set_major_formatter(majorFormatter) ax2.yaxis.set_minor_locator(minorLocator) #ax2.yaxis.set_minor_formatter(majorFormatter) label = ax2.get_yticks() plt.yticks(label,rotation=90) plt.xlim(-4,4) plt.ylim(13,23) plt.gca().invert_yaxis() #plt.savefig('Group_Plotting2.pdf') plt.show()
def plot_slip_overview(slip, output_file, if_x_log=False, xlim=[0, 1344], ylim = [0,100], yticks = [20, 40, 60], xticks = [1, 10, 100, 1000], xticklabels = [r'$10^0$', r'$10^1$', r'$10^2$', r'$10^3$'], rotation = 45, fontsize = 10, ): num_subflts_strike = slip.num_subflt_along_strike num_subflts_dip = slip.num_subflt_along_dip epochs = slip.get_epochs() fig, axes = plt.subplots(num_subflts_dip, num_subflts_strike, sharex=True, sharey=True) for ii in range(num_subflts_dip): for jj in range(num_subflts_strike): ax = axes[ii][jj] slip_subflt = slip.get_cumu_slip_at_subfault(ii,jj) plt.sca(ax) plt.fill_between(x=epochs, y1=slip_subflt, y2=0, color='r') if if_x_log: ax.set_xscale('log') plt.xlim(xlim) plt.ylim(ylim) plt.grid('on') plt.box('on') plt.tick_params(axis='both',which='both', bottom='off', top='off', left='off', right='off', labelbottom='off', labeltop='off', labelleft='off', labelright='off') fig.subplots_adjust(hspace=0, wspace=0) for ax in axes[-1,::2]: plt.sca(ax) plt.tick_params(axis='x',which='major', bottom='on', top='off', left='off', right='off', labelbottom='on', labeltop='off', labelleft='off', labelright='off') ax.set_xticks(xticks) ax.set_xticklabels(xticklabels, rotation=rotation, fontsize=fontsize) plt.xlabel('day') for ax in axes[0,1::2]: plt.sca(ax) plt.tick_params(axis='x',which='major', bottom='off', top='on', left='off', right='off', labelbottom='off', labeltop='on', labelleft='off', labelright='off') ax.set_xticks(xticks) ax.set_xticklabels(xticklabels, rotation=rotation, fontsize=fontsize) plt.xlabel('day') for ax in axes[::2,0]: plt.sca(ax) plt.tick_params(axis='y',which='major', bottom='off', top='off', left='on', right='off', labelbottom='off', labeltop='off', labelleft='on', labelright='off') ax.set_yticks(yticks) #ax.set_yticklabels(range(0,100,20)) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(10) tick.label.set_rotation('horizontal') plt.ylabel('slip/m') for ax in axes[::2,-1]: plt.sca(ax) plt.tick_params(axis='y',which='major', bottom='off', top='off', left='off', right='on', labelbottom='off', labeltop='off', labelleft='off', labelright='on') ax.set_yticks(yticks) #ax.set_yticklabels(range(0,100,20)) for tick in ax.yaxis.get_major_ticks(): tick.label.set_fontsize(10) tick.label.set_rotation('horizontal') plt.ylabel('slip/m') ax.yaxis.set_label_position("right") fig.set_size_inches(33,10) plt.savefig(output_file) plt.close()
x2 = [x - 1.0 for x in x1] xticks(x1, nucleotides) plt.bar(x2, [item[0] for item in counts_tfidf], width=1.0, color="#87CEEB", label="w.r.t abstractive resume") plt.bar(x1, [item[1] for item in counts_tfidf], width=1.0, color="#F4A460", label="w.r.t extractive resume") plt.gca().yaxis.grid(True) plt.xlim(0, 105) plt.ylim(0, 1.2) plt.legend() plt.subplot(2, 1, 2) plt.title( 'NMF topic modelling results vs abstractive and extractive summaries', fontproperties=font_prop) plt.xlabel('scenario-based meetings', fontproperties=font_prop) plt.ylabel('accuracy', fontproperties=font_prop) plt.xticks(x1, nucleotides) plt.bar(x2, [item[0] for item in counts_nmf], width=1.0, color="#87CEEB",