def hexbin_plot(self, var1, var2, force=False): fig_name = "{}/hexbin_{}_{}.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": print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) 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)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig(fig_name) if self.display: plt.show() plt.close()
def plot_comfort(fingers_org=range(1, 6, 1), fingers_dst=range(1, 6, 1), jumps=range(-12, 13, 1)): import seaborn from mpl_toolkits.mplot3d import Axes3D from pylab import plt xs, ys, zs, cs = calculate_comforts(fingers_org, fingers_dst, jumps) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(xs, ys, zs, c=cs) ax.set_zlabel("Interval (half steps)", fontsize=15) ax.set_zlim(jumps[0], jumps[-1]) # ax.set_zticks(jumps) plt.xticks(fingers_org) plt.xlim(fingers_org[0], fingers_org[-1]) plt.xlabel("From finger", fontsize=15) plt.yticks(fingers_dst) plt.ylim(fingers_dst[0], fingers_dst[-1]) plt.ylabel("To finger", fontsize=15) plt.title("Difficulty of finger passages", fontsize=25) plt.savefig('./figures/image.png', figsize=(16, 12), dpi=300) plt.show()
def hexbin_plot(self, var1, var2): print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) 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)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig("{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() 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 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 savefig(self, fname): # Graph using the parameters plt.xlim(-1000, max(self.incomes) * 1.05) # make it a little bigger than needed plt.ylim(-5, 105) plt.legend(loc='lower center', fontsize=9) plt.xticks(rotation=20) plt.axes().get_xaxis().set_major_formatter( mp.ticker.FuncFormatter(lambda x, p: format(int(x), ','))) plt.savefig(fname)
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 plot2piFft(self, func, Fs, L): ''' Fs is the sampling freq. L is length of signal list. This plot is for a func that has period of 2pi. If you found the time domain wave is not very accurate, that is because you set too small Fs, which leads to to big step Ts. ''' base_freq = 1.0/(2*np.pi) #频域横坐标除以基频,即以基频为单位,此处的基频为 2*pi rad/s Ts = 1.0/Fs t = [el*Ts for el in range(0,L)] x = [func(el) for el in t] # https://www.ritchievink.com/blog/2017/04/23/understanding-the-fourier-transform-by-example/ # 小明给的代码: # sampleF = Fs # print('小明:') # for f, Y in zip( # np.arange(0, len(x)*sampleF,1) * 1/len(x) * sampleF, # np.log10(np.abs(np.fft.fft(x) / len(x))) # ): # print('\t', f, Y) L_4pi = int(4*np.pi / Ts) +1 # 画前两个周期的 self.fig_plot2piFft = plt.figure(7) plt.subplot(211) plt.plot(t[:L_4pi], x[:L_4pi]) #title('Signal in Time Domain') #xlabel('Time / s') #ylabel('x(t)') plt.title('Winding Function') plt.xlabel('Angular location along air gap [mech. rad.]') plt.ylabel('Current Linkage by unit current [Ampere]') NFFT = 2**nextpow2(L) print('NFFT =', NFFT, '= 2^%g' % (nextpow2(L)), '>= L =', L) y = fft(x,NFFT) # y is a COMPLEX defined in numpy Y = [2 * el.__abs__() / L for el in y] # /L for spectrum aplitude consistent with actual signal. 2* for single-sided. abs for amplitude. f = Fs/2.0/base_freq*linspace(0,1,int(NFFT/2+1)) # unit is base_freq Hz #f = Fs/2.0*linspace(0,1,NFFT/2+1) # unit is Hz plt.subplot(212) plt.plot(f, Y[0:int(NFFT/2+1)]) plt.title('Single-Sided Amplitude Spectrum of x(t)') plt.xlabel('Frequency divided by base_freq [base freq * Hz]') #plt.ylabel('|Y(f)|') plt.ylabel('Amplitude [1]') plt.xlim([0,50])
def displayRetireWMonthsandRates(monthlies, rates, terms): plt.figure('retire both') plt.clf() plt.xlim(30 * 12, 40 * 12) # focusing only on the last 10 years of investment for monthly in monthlies: for rate in rates: xvals, yvals = retire(monthly, rate, terms) plt.plot(xvals, yvals, label='retire with ' + str(monthly) + ":" + str(int(rate * 100))) plt.legend(loc='upper left')
def scatter_plot(self, var1, var2, range_var, linear_regression, force=False): fig_name = "{}/scatterplot_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return print("Doing scatter plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) plt.scatter(x=x, y=y, c=self.stats.data["transportation_cost"], s=10, cmap=cm.plasma) plt.xlim(range_var[var1]) plt.ylim(range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) if linear_regression: slope, intercept, r_value, p_value, std_err = linregress(x, y) plt.plot(x, intercept + x * slope, c="black", lw=2) with open("{}/stats.txt".format(self.fig_folder), "a", encoding='utf-8') as f: to_write = "*****\n" + \ "{} against {}\n".format(self.format_label(var2), self.format_label(var1)) + \ "p value: {}\n".format(p_value) + \ "intercept: {}\n".format(intercept) + \ "slope: {}\n".format(slope) + \ "r value: {}\n".format(r_value) + \ "\n" f.write(to_write) plt.savefig(fig_name) if self.display: plt.show() plt.close()
def _plot(self): (binsX, binsY) = (self.bins, self.bins) if isinstance(self.bins, int) else self.bins X, Y = self.data H, ex, ey = numpy.histogram2d(X, Y, bins=(binsX, binsY)) x_center = numpy.diff(ex) / 2 + ex[0:-1] x_digit = numpy.digitize(X, ex) y_center = numpy.empty(binsY) y_std = numpy.empty(binsY) for i in range(binsX): y_pop = Y[numpy.where(x_digit == i + 1)[0]] y_center[i] = numpy.mean(y_pop) y_std[i] = numpy.std(y_pop) plt.errorbar(x_center, y_center, y_std) plt.xlim(ex[0], ex[-1])
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 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 __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 _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 pos_firmA_over_pos_firmB(file_name, folder=None): if folder is None: folder = "data/figures" os.makedirs(folder, exist_ok=True) bkp = backup.RunBackup.load(file_name=file_name) pos = bkp.positions[-1000:] fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05) ax.axvline(0.5, color="white", linewidth=0.5, linestyle="--") ax.axhline(0.5, color="white", linewidth=0.5, linestyle="--") plt.xlim(-1, bkp.parameters.n_positions) plt.ylim(-1, bkp.parameters.n_positions) plt.xticks( range(0, bkp.parameters.n_positions + 1, round(bkp.parameters.n_positions / 5))) plt.yticks( range(0, bkp.parameters.n_positions + 1, round(bkp.parameters.n_positions / 5))) plt.xlabel("Position A") plt.ylabel("Position B") plt.text(0.005, 0.005, file_name, transform=fig.transFigure, fontsize='x-small', color='0.5') plt.title("$r={:.2f}$".format(bkp.field_of_view / 2)) ax.set_aspect(1) plt.tight_layout() plt.savefig("{}/{}_evo_positions.pdf".format(folder, file_name)) plt.show()
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 freqz(sosmat, nsamples=44100, sample_rate=44100, plot=True): """Plots Frequency response of sosmat.""" from pylab import np, plt, fft, fftfreq x = np.zeros(nsamples) x[nsamples/2] = 0.999 y, states = sosfilter_double_c(x, sosmat) Y = fft(y) f = fftfreq(len(x), 1.0/sample_rate) if plot: plt.grid(True) plt.axis([0, sample_rate / 2, -100, 5]) L = 20*np.log10(np.abs(Y[:len(x)/2]) + 1e-17) plt.semilogx(f[:len(x)/2], L, lw=0.5) plt.hold(True) plt.title('freqz sos filter') plt.xlabel('Frequency / Hz') plt.ylabel('Damping /dB(FS)') plt.xlim((10, sample_rate/2)) plt.hold(False) return x, y, f, Y
def freqz(sosmat, nsamples=44100, sample_rate=44100, plot=True): """Plots Frequency response of sosmat.""" from pylab import np, plt, fft, fftfreq x = np.zeros(nsamples) x[int(nsamples/2)] = 0.999 y, states = sosfilter_double_c(x, sosmat) Y = fft(y) f = fftfreq(len(x), 1.0/sample_rate) if plot: plt.grid(True) plt.axis([0, sample_rate / 2, -100, 5]) L = 20*np.log10(np.abs(Y[:int(len(x)/2)]) + 1e-17) plt.semilogx(f[:int(len(x)/2)], L, lw=0.5) plt.hold(True) plt.title(u'freqz sos filter') plt.xlabel('Frequency / Hz') plt.ylabel(u'Damping /dB(FS)') plt.xlim((10, sample_rate/2)) plt.hold(False) return x, y, f, Y
def draw(weightvect): """ function that draw the graph so the line that separate the points from the special point """ axeofx = [-1, 2] axeofy = calculate(axeofx, weightvect) plt.xlim(-1, 2) plt.ylim(-1, 2) plt.plot(axeofx, axeofy, color="black") pointx = [0, 0, 1] pointy = [0, 1, 0] plt.scatter(pointx, pointy, color="blue") plt.scatter(1, 1, color="red") plt.ylabel("x2") plt.xlabel("x1") title1 = "Weight Vector : " + str(weightvect) plt.title(title1, backgroundcolor ="green",color="black") plt.pause(0.2) plt.cla() return 0
def displayRetireWMonthsandRates2(monthlies, rates, terms): plt.figure('retire better') plt.clf() plt.xlim(30 * 12, 40 * 12) monthLabels = ['r', 'b', 'g', 'k'] rateLabels = ['-', 'o', '^'] for i in range(len(monthlies)): monthly = monthlies[i] monthLabel = monthLabels[i % len( monthLabels )] # using remainder to pick new label for each new month choice for j in range(len(rates)): rate = rates[j] rateLable = rateLabels[j % len( rateLabels)] # if more months, cycles back to the beginning xvals, yvals = retire(monthly, rate, terms) plt.plot(xvals, yvals, monthLabel + rateLable, label='retire: ' + str(monthly) + " : " + str(int(rate * 100))) plt.legend(loc="upper left")
def plot_L_curve( files, nlin_pars=['log10_He_', 'log10_visM_', 'rake'], nlin_pars_ylabels=[r'$log_{10}(He)$', r'$log_{10}(visM)$', 'rake'], ): nreses = collect_from_result_files(files, 'residual_norm_weighted') nroughs = collect_from_result_files(files, 'roughening_norm') num_subplots = 1 + len(nlin_pars) x1 = amin(nreses) x2 = amax(nreses) dx = x2 - x1 xlim = (x1 - dx * 0.02, x2 + dx * 0.2) xticks = range(int(x1), int(x2), 5) plt.subplot(num_subplots, 1, 1) plt.loglog(nreses, nroughs, 'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter( matplotlib.ticker.ScalarFormatter()) plt.ylabel('roughening') plt.xlabel('Residual Norm') plt.grid('on') nth = 2 for par, par_label in zip(nlin_pars, nlin_pars_ylabels): y = collect_from_result_files(files, par) plt.subplot(num_subplots, 1, nth) plt.semilogx(nreses, y, 'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter( matplotlib.ticker.ScalarFormatter()) plt.ylabel(par_label) plt.xlabel('Residual Norm') plt.grid('on') nth += 1
def scatter_plot(self, var1, var2, range_var, linear_regression): print("Doing scatter plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) plt.scatter(x=x, y=y, color="black", s=10) plt.xlim(range_var[var1]) plt.ylim(range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) if linear_regression: slope, intercept, r_value, p_value, std_err = linregress(x, y) plt.plot(x, intercept + x * slope, c="black", lw=2) with open("{}/stats.txt".format(self.fig_folder), "a", encoding='utf-8') as f: to_write = "*****\n" + \ "{} against {}\n".format(self.format_label(var2), self.format_label(var1)) + \ "p value: {}\n".format(p_value) + \ "intercept: {}\n".format(intercept) + \ "slope: {}\n".format(slope) + \ "r value: {}\n".format(r_value) + \ "\n" f.write(to_write) plt.savefig("{}/scatterplot_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() plt.close()
def pos_firmA_over_pos_firmB(backup, fig_name): os.makedirs(os.path.dirname(fig_name), exist_ok=True) position_max = backup.parameters.n_positions - 1 pos = backup.positions[-1000:] / position_max fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05, zorder=10) ax.axvline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1) ax.axhline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1) plt.xlim(0, 1) plt.ylim(0, 1) plt.xticks((0, 0.5, 1)) plt.yticks((0, 0.5, 1)) plt.xlabel("Position $a$") plt.ylabel("Position $b$") for tick in ax.get_xticklabels(): tick.set_fontsize("small") for tick in ax.get_yticklabels(): tick.set_fontsize("small") plt.title("$r={:.2f}$".format(backup.parameters.r)) ax.set_aspect(1) # Cut margins plt.tight_layout() # Save fig plt.savefig(fig_name) plt.close()
def plot_L_curve(files, nlin_pars = ['log10_He_','log10_visM_','rake'], nlin_pars_ylabels = [r'$log_{10}(He)$', r'$log_{10}(visM)$', 'rake'], ): nreses = collect_from_result_files(files, 'residual_norm_weighted') nroughs = collect_from_result_files(files, 'roughening_norm') num_subplots = 1 + len(nlin_pars) x1 = amin(nreses) x2 = amax(nreses) dx = x2 - x1 xlim = (x1-dx*0.02, x2+dx*0.2) xticks = range(int(x1), int(x2),5) plt.subplot(num_subplots,1,1) plt.loglog(nreses, nroughs,'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.ylabel('roughening') plt.xlabel('Residual Norm') plt.grid('on') nth = 2 for par, par_label in zip(nlin_pars, nlin_pars_ylabels): y = collect_from_result_files(files, par) plt.subplot(num_subplots,1,nth) plt.semilogx(nreses, y,'o-') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.gca().get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) plt.ylabel(par_label) plt.xlabel('Residual Norm') plt.grid('on') nth += 1
def plot(x, y, y_std, x_label, y_label, fig_title, fig_folder, fig_name, comment=None): plt.figure(figsize=(10, 10)) plt.plot(x, y, c='b', lw=2) plt.plot(x, y + y_std, c='b', lw=.5) plt.plot(x, y - y_std, c='b', lw=.5) plt.fill_between(x, y + y_std, y - y_std, color='b', alpha=.1) plt.xlabel("\n{}".format(x_label), fontsize=12) plt.ylabel("{}\n".format(y_label), fontsize=12) if comment: plt.title("{}\n({})\n".format(fig_title, comment)) else: plt.title("{}\n".format(fig_title)) # if comment: # # plt.text(x=min(x) + (max(x) - min(x)) * 0.5, y=min(y) + (max(y) - min(y)) * 0.5, # s="{}".format(comment)) plt.xlim(min(x), max(x)) plt.ylim(-0.001, 0.1) if not path.exists(fig_folder): mkdir(fig_folder) plt.savefig("{}/{}".format(fig_folder, fig_name)) plt.close()
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()
import h5py from pylab import plt nreses =[] rakes = [] for ano in range(30): with h5py.File('outs/ano_%02d.h5'%ano,'r') as fid: nres = fid['misfit/norm_weighted'][...] nreses.append(nres) rake = fid['nlin_pars/rake'][...] rakes.append(rake) plt.semilogx(nreses, rakes,'o') plt.ylabel('rake') plt.xlabel('weighted residual norm') plt.xlim([0.7, 4]) plt.savefig('plots/rake_residual.png') plt.show()
def mkplots(self): # run to make plots of the resulting posteriors. Modified from marginal_plots.py # from pymultinest. Produces basename+marg.pdf and basename+marge.png files prefix = self.basename parameters = json.load(file(prefix + 'params.json')) n_params = len(parameters) a = pymultinest.Analyzer(n_params = n_params, outputfiles_basename = prefix) s = a.get_stats() p = pymultinest.PlotMarginal(a) try: values = a.get_equal_weighted_posterior() except IOError as e: print 'Unable to open: %s' % e return assert n_params == len(s['marginals']) modes = s['modes'] dim2 = os.environ.get('D', '1' if n_params > 20 else '2') == '2' nbins = 100 if n_params < 3 else 20 if dim2: plt.figure(figsize=(5.1*n_params, 5*n_params)) for i in range(n_params): plt.subplot(n_params, n_params, i + 1) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x,w,patches = oldax.hist(values[:,i], bins=nbins, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05*(ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose([[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) #newax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) #plt.close() for j in range(i): plt.subplot(n_params, n_params, n_params * (j + 1) + i + 1) p.plot_conditional(i, j, bins=20, cmap = plt.cm.gray_r) for m in modes: plt.errorbar(x=m['mean'][i], y=m['mean'][j], xerr=m['sigma'][i], yerr=m['sigma'][j]) ax = plt.gca() if j == i-1: plt.xlabel(parameters[i]) plt.ylabel(parameters[j]) [l.set_rotation(45) for l in ax.get_xticklabels()] else: ax.set_xticklabels([]) ax.set_yticklabels([]) plt.xlim([m['mean'][i]-5*m['sigma'][i],m['mean'][i]+5*m['sigma'][i]]) plt.ylim([m['mean'][j]-5*m['sigma'][j],m['mean'][j]+5*m['sigma'][j]]) #plt.savefig('cond_%s_%s.pdf' % (params[i], params[j]), bbox_tight=True) #plt.close() plt.tight_layout() plt.savefig(prefix + 'marg.pdf') plt.savefig(prefix + 'marg.png') plt.close() else: from matplotlib.backends.backend_pdf import PdfPages print '1dimensional only. Set the D environment variable D=2 to force' print '2d marginal plots.' pp = PdfPages(prefix + 'marg1d.pdf') for i in range(n_params): plt.figure(figsize=(5, 5)) plt.xlabel(parameters[i]) m = s['marginals'][i] plt.xlim(m['5sigma']) oldax = plt.gca() x,w,patches = oldax.hist(values[:,i], bins=20, edgecolor='grey', color='grey', histtype='stepfilled', alpha=0.2) oldax.set_ylim(0, x.max()) newax = plt.gcf().add_axes(oldax.get_position(), sharex=oldax, frameon=False) p.plot_marginal(i, ls='-', color='blue', linewidth=3) newax.set_ylim(0, 1) ylim = newax.get_ylim() y = ylim[0] + 0.05*(ylim[1] - ylim[0]) center = m['median'] low1, high1 = m['1sigma'] print center, low1, high1 newax.errorbar(x=center, y=y, xerr=np.transpose([[center - low1, high1 - center]]), color='blue', linewidth=2, marker='s') oldax.set_yticks([]) newax.set_ylabel("Probability") ylim = oldax.get_ylim() newax.set_xlim(m['5sigma']) oldax.set_xlim(m['5sigma']) plt.savefig(pp, format='pdf', bbox_inches='tight') plt.close() pp.close()
def freqz(ofb, length_sec=6, ffilt=False, plot=True): """Computes the IR and FRF of a digital filter. Parameters ---------- ofb : FractionalOctaveFilterbank object length_sec : scalar Length of the impulse response test signal. ffilt : bool Backard forward filtering. Effectiv order is doubled then. plot : bool Create Plots or not. Returns ------- x : ndarray Impulse test signal. y : ndarray Impules responses signal of the filters. f : ndarray Frequency vector for the FRF. Y : Frequency response (FRF) of the summed filters. """ from pylab import np, plt, fft, fftfreq x = np.zeros(length_sec*ofb.sample_rate) x[length_sec*ofb.sample_rate/2] = 0.9999 if not ffilt: y, states = ofb.filter_mimo_c(x) y = y[:, :, 0] else: y, states = ofb.filter(x, ffilt=ffilt) s = np.zeros(len(x)) for i in range(y.shape[1]): s += y[:, i] X = fft(y[:, i]) # sampled frequency response f = fftfreq(len(x), 1.0/ofb.sample_rate) if plot: fig = plt.figure('freqz filter bank') plt.grid(True) plt.axis([0, ofb.sample_rate / 2, -100, 5]) L = 20*np.log10(np.abs(X[:len(x)/2]) + 1e-17) plt.semilogx(f[:len(x)/2], L, lw=0.5) plt.hold(True) Y = fft(s) if plot: plt.title('freqz() Filter Bank') plt.xlabel('Frequency / Hz') plt.ylabel('Damping /dB(FS)') plt.xlim((10, ofb.sample_rate/2)) plt.hold(False) plt.figure('sum') L = 20*np.log10(np.abs(Y[:len(x)/2]) + 1e-17) plt.semilogx(f[:len(x)/2], L, lw=0.5) level_input = 10*np.log10(np.sum(x**2)) level_output = 10*np.log10(np.sum(s**2)) plt.axis([5, ofb.sample_rate/1.8, -50, 5]) plt.grid(True) plt.title('Sum of filter bands') plt.xlabel('Frequency / Hz') plt.ylabel('Damping /dB(FS)') print('sum level', level_output, level_input) return x, y, f, Y
import numpy as np from pylab import plt import viscojapan as vj reader = vj.ReadEarthModelFile('earth.modelBURG-SUM_40km') dep = np.arange(2.01,100) shear = reader.get_shear_modulus(dep)/1e9 tp = np.loadtxt('prem.model') shear_prem = tp[:,5] dep_prem = 6371-tp[:,0] plt.plot(dep, shear) plt.plot(dep_prem, shear_prem) plt.xlim([0,150]) plt.show()
def blanking(self, semi_data, int_data, nug, R): # def blanking(self, semi_data, int_data, prediction, nug, R): # ============================================================================= # Defining and Initializing Variables # ============================================================================= predictor = defaultdict(list) mindistance = defaultdict(list) color = defaultdict(list) frequency = [] mean = [] std = [] lag = [] classed_resi = [] classed_dist = [] col2del = [] covariance = sv.Semivariogram(semi_data).isotropy(nug) # input("Press Enter string to continue") # ============================================================================= # 150m = max. radius for Elevation (Z) in 2001 # 350m = max. radius for Elevation (Z) in 2011 # 250m = max. radius for Synthetic "Profile A" Ice thickness and Profile A&B # 350m = max. radius for Synthetic "Profile B" Ice thickness # ============================================================================= # ============================================================================= # R = maximum radius between the two closest data points in the dataset. # It is obtained by direct observation of the distribution of the dataset. # ============================================================================= # R = 350 C = 10 r0 = R / 100 sep = np.linspace(R / C, R, C) blanking_radii = np.hstack((0, r0, sep)) # blanking_radii = np.hstack((0, r0)) # # ============================================================================= # # Estimating the interpolated value for the entire grid of points # # ============================================================================= # inter = [] # dist_min = [] # for k in range(len(prediction)): # inter.append(kg.Kriging().ordinary(covariance, semi_data, prediction[k, :2], 0, 0)) # dist_min.append(np.min(cdist(int_data[:, :2], prediction[k, :2][None]))) # print(str(k) + ' ' + str(len(prediction))) # inter = np.hstack(inter) # dist_min = np.hstack(dist_min) # krige_mindist = pd.DataFrame(np.column_stack((prediction, inter, dist_min))) # w_krige_mindist = pd.ExcelWriter(str(time.localtime()[0]) + str(time.localtime()[1]) + str(time.localtime()[2])+'_' + # str(time.localtime()[3]) + str(time.localtime()[4]) + str(time.localtime()[5]) + # '_Prediction.xlsx', engine='xlsxwriter') # krige_mindist.to_excel(w_krige_mindist, sheet_name='Prediction') # w_krige_mindist.save() # ============================================================================= # Blanking data inside defined radius prior to kriging to obtain interpolation # with its error # ============================================================================= for i in range(len(blanking_radii)): krige = [] min_dist = [] for j in range(len(int_data)): unblanked = semi_data[( (semi_data[:, :2] - int_data[j, :2])**2).sum(1) > blanking_radii[i]**2] min_dist.append( np.min(cdist(unblanked[:, :2], int_data[j, :2][None]))) krige.append( np.hstack(kg.Kriging().ordinary(covariance, unblanked, int_data[j, :2]))) print(str(i) + ' ' + str(j)) predictor[i] = np.hstack(krige) mindistance[i] = np.hstack(min_dist) del krige del min_dist mindistance = pd.DataFrame(mindistance) predictor = pd.DataFrame(predictor) color = pd.DataFrame(predictor) predictor = predictor.T.drop_duplicates().T mindistance = mindistance.T.drop_duplicates().T color = color.T.drop_duplicates().T predictor = predictor.apply(pd.Series.drop_duplicates, axis=1) mindistance = mindistance.apply(pd.Series.drop_duplicates, axis=1) color = color.apply(pd.Series.drop_duplicates, axis=1) # ============================================================================= # Get the interpolator error # ============================================================================= error = (np.array(predictor).transpose() - int_data[:, 2].transpose()).transpose() # ============================================================================= # Scatter plot of minimum distance between points versus its interpolator error # ============================================================================= mindistance = np.array(mindistance) color = np.array(color) # color = np.random.rand(len(predictor.columns)) for i in range(len(predictor.columns)): for k in range(len(predictor)): color[k, i] = blanking_radii[i] plt.scatter(mindistance, error, c=color) plt.xlim(0, 450) plt.ylim(-250, 300) plt.savefig('Scatter Plot.pdf', fmt='pdf', dpi=200) plt.show() for i in range(len(predictor.columns)): plt.scatter(mindistance[:, i], error[:, i]) plt.xlim(0, 450) plt.ylim(-250, 300) plt.savefig('Scatter-' + str(blanking_radii[i]) + '.pdf', fmt='pdf', dpi=200) plt.show() vecresi = np.array(error).ravel() vecdist = np.array(mindistance).ravel() sep = np.linspace(R / C, R, C) lags = (sep[1:] + sep[:-1]) / 2 lags = np.hstack((0, r0, R / (2 * C), lags, 2 * lags[-1] - lags[-2])) count = -1 for ilag in lags[:-1]: count = count + 1 with warnings.catch_warnings(): warnings.simplefilter("ignore", category=RuntimeWarning) frequency.append( np.sum((vecdist[:] >= ilag) & (vecdist[:] < lags[count + 1]))) classed_resi.append(vecresi[(vecdist[:] >= ilag) & (vecdist[:] < lags[count + 1])]) classed_dist.append( np.average(vecdist[(vecdist[:] >= ilag) & (vecdist[:] < lags[count + 1])])) mean.append( np.average(vecresi[(vecdist[:] >= ilag) & (vecdist[:] < lags[count + 1])])) std.append( np.std(vecresi[(vecdist[:] >= ilag) & (vecdist[:] < lags[count + 1])])) classed_error = pd.DataFrame(classed_resi).transpose() lag = np.hstack((0, r0, sep)) iclassed_error = pd.DataFrame(classed_resi).transpose() count = -1 for i in range(len(classed_error.columns)): count = count + 1 if np.count_nonzero(~np.isnan(np.array(classed_error)[:, i])) < 100: col2del.append(count) iclassed_error.drop(i, axis=1, inplace=True) lag = np.delete(lag, col2del) mean = np.delete(mean, col2del) std = np.delete(std, col2del) classed_dist = np.delete(classed_dist, col2del) frequency = np.delete(frequency, col2del) # ============================================================================= # Export Interpolator Error grouped in classes as excel file # ============================================================================= error = pd.DataFrame(error) w_err_mindist = pd.ExcelWriter( str(time.localtime()[0]) + str(time.localtime()[1]) + str(time.localtime()[2]) + '_' + str(time.localtime()[3]) + str(time.localtime()[4]) + str(time.localtime()[5]) + '_Error.xlsx', engine='xlsxwriter') error.to_excel(w_err_mindist, sheet_name='Error') # ============================================================================= # Export Minimum Distance grouped in classes as excel file # ============================================================================= mindistance = pd.DataFrame(mindistance) mindistance.to_excel(w_err_mindist, sheet_name='Minimum Distance') w_err_mindist.save() # ============================================================================= # Scatter plot of the error gropued in classes defined by the vector "lag" # ============================================================================= iclassed_error = np.array(iclassed_error) plt.scatter(np.tile(classed_dist, len(iclassed_error)), iclassed_error.flatten()) plt.plot(classed_dist, mean, 'o', c='k') plt.plot(classed_dist, std, 'o', c='r') plt.xlabel('Distance') plt.ylabel('Error') plt.title('DBF and DEF') # plt.savefig(str(time.localtime()[0]) + str(time.localtime()[1]) + str(time.localtime()[2])+'_' + # str(time.localtime()[3]) + str(time.localtime()[4]) + str(time.localtime()[5]) + # '_Validation.png', fmt='png', dpi=200) plt.show() # weight = np.sqrt(std) ** 2 weight = ((frequency * classed_dist) / np.sum(frequency * classed_dist)) # weight = np.sqrt(blanking_radii) ** 2 # ============================================================================= # Least square fit function to obtain coefficient of the indeterminate # ============================================================================= paramsMean = curve_fit(fnc.fitfunction, classed_dist, mean, sigma=weight, bounds=((-np.inf, -np.inf, -np.inf, 0), (np.inf, np.inf, np.inf, 0.000001))) paramsStD = curve_fit(fnc.fitfunction, classed_dist, std, sigma=weight, bounds=((-np.inf, -np.inf, -np.inf, 0), (np.inf, np.inf, np.inf, nug))) [m1, m2, m3, m4] = paramsMean[0] [s1, s2, s3, s4] = paramsStD[0] classed_dist = np.hstack((0, classed_dist)) mean = np.hstack((0, mean)) std = np.hstack((s4, std)) frequency = np.hstack((0, frequency)) x_int = np.linspace(np.min(classed_dist), np.max(classed_dist), 200) f_mean = np.poly1d(paramsMean[0]) f_std = np.poly1d(paramsStD[0]) mean_int = f_mean(x_int) std_int = f_std(x_int) _, ax = plt.subplots() plt.plot(x_int, mean_int) plt.plot(x_int, std_int) # plt.plot(classed_dist, mean, 'o', c='k', label=str(m1) + "x^3 " + str(m2) + "x^2 ")# + str(m3) + "x") plt.plot(classed_dist, mean, 'o', c='k', label=str(m1) + "x^3 " + str(m2) + "x^2 " + str(m3) + "x") plt.plot(classed_dist, std, 'x', c='r', label=str(s1) + "x^3 " + str(s2) + "x^2 " + str(s3) + "x " + str(s4)) for i, txt in enumerate(frequency): ax.annotate(txt, (classed_dist[i], mean[i])) ax.annotate(txt, (classed_dist[i], std[i])) plt.legend(loc=2, fontsize='xx-small', borderaxespad=0.) plt.savefig('Validation Fit.pdf', fmt='pdf', dpi=200) plt.show()
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 update_graph(times, nums): plt.xlim(max(times) - datetime.timedelta(minutes=15), max(times)) plt.ylim(0, max(nums) * 1.2) plt.plot_date(times, nums, "ro") plt.draw()
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()
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()
import h5py from pylab import plt nreses = [] rakes = [] for ano in range(30): with h5py.File('outs/ano_%02d.h5' % ano, 'r') as fid: nres = fid['misfit/norm_weighted'][...] nreses.append(nres) rake = fid['nlin_pars/rake'][...] rakes.append(rake) plt.semilogx(nreses, rakes, 'o') plt.ylabel('rake') plt.xlabel('weighted residual norm') plt.xlim([0.7, 4]) plt.savefig('plots/rake_residual.png') plt.show()
m = fid['m'][...] visMs.append(m[-3]) Hes.append(m[-2]) rakes.append(m[-1]) nrough = fid['regularization/roughening/norm'][...] nroughs.append(nrough) xlim = (7, 22) xlim = None xticks = range(7,22) plt.subplot(411) plt.semilogx(nreses, visMs,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.grid('on') plt.ylabel('log10(visM/(Pa.s))') plt.subplot(412) plt.semilogx(nreses, Hes,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks) plt.grid('on') plt.ylabel('He/km') plt.subplot(413) plt.semilogx(nreses, rakes,'o') plt.xlim(xlim) plt.gca().set_xticks(xticks)
def freqz(ofb, length_sec=6, ffilt=False, plot=True): """Computes the IR and FRF of a digital filter. Parameters ---------- ofb : FractionalOctaveFilterbank object length_sec : scalar Length of the impulse response test signal. ffilt : bool Backard forward filtering. Effectiv order is doubled then. plot : bool Create Plots or not. Returns ------- x : ndarray Impulse test signal. y : ndarray Impules responses signal of the filters. f : ndarray Frequency vector for the FRF. Y : Frequency response (FRF) of the summed filters. """ from pylab import np, plt, fft, fftfreq x = np.zeros(length_sec * ofb.sample_rate) x[int(length_sec * ofb.sample_rate / 2)] = 0.9999 if not ffilt: y, states = ofb.filter_mimo_c(x) y = y[:, :, 0] else: y, states = ofb.filter(x, ffilt=ffilt) s = np.zeros(len(x)) len_x_2 = int(len(x) / 2) for i in range(y.shape[1]): s += y[:, i] X = fft(y[:, i]) # sampled frequency response f = fftfreq(len(x), 1.0 / ofb.sample_rate) if plot: fig = plt.figure('freqz filter bank') plt.grid(True) plt.axis([0, ofb.sample_rate / 2, -100, 5]) L = 20 * np.log10(np.abs(X[:len_x_2]) + 1e-17) plt.semilogx(f[:len_x_2], L, lw=0.5) Y = fft(s) if plot: plt.title(u'freqz() Filter Bank') plt.xlabel('Frequency / Hz') plt.ylabel(u'Damping /dB(FS)') plt.xlim((10, ofb.sample_rate / 2)) plt.figure('sum') L = 20 * np.log10(np.abs(Y[:len_x_2]) + 1e-17) plt.semilogx(f[:len_x_2], L, lw=0.5) level_input = 10 * np.log10(np.sum(x**2)) level_output = 10 * np.log10(np.sum(s**2)) plt.axis([5, ofb.sample_rate / 1.8, -50, 5]) plt.grid(True) plt.title('Sum of filter bands') plt.xlabel('Frequency / Hz') plt.ylabel(u'Damping /dB(FS)') print('sum level', level_output, level_input) return x, y, f, Y
sns.set_palette('colorblind') data = pickle.load(open('tlm_test_result.pkl', 'rb')) pert_scales = data['pert_scales'] # loop over forecasts plt.figure() for tlm, finitdiff in zip(data['tlm'], data['finitdiff']): plt.plot(pert_scales, tlm, label='tlm', alpha=0.1, color='black') plt.plot(pert_scales, finitdiff, label='NN', alpha=0.1, color='red') plt.xlabel('$\sigma$') plt.ylabel("$y'$") plt.savefig('plots/tlm_test_singlelines.png') plt.xlim((-0.25, 0.25)) plt.ylim((-0.004, 0.004)) plt.savefig('plots/tlm_test_singlelines_closeup.png') plt.show() # compute mean response over all forecasts tlm = np.mean(np.array(data['tlm']), axis=0) finitdiff = np.mean(np.array(data['finitdiff']), axis=0) plt.figure(figsize=(12, 4)) plt.subplot(121) plt.plot(pert_scales, tlm, label='TLM') plt.plot(pert_scales, finitdiff, label='NN') plt.xlabel('$\sigma$') plt.ylabel("$y'$")
import h5py from pylab import plt def collect_results(outs_files, key): outs = [] for file in outs_files: with h5py.File(file, 'r') as fid: out = fid[key][...] outs.append(out) return outs files = sorted(glob.glob('../outs/ano_??.h5')) nrough1 = collect_results(files, 'regularization/roughening/norm') nres1 = collect_results(files, 'misfit/norm_weighted') files = sorted(glob.glob('../../run0/outs/ano_??.h5')) nrough0 = collect_results(files, 'regularization/roughening/norm') nres0 = collect_results(files, 'misfit/norm_weighted') plt.loglog(nres0, nrough0, '.', label='Result0') plt.loglog(nres1, nrough1, '.', label='Result1') plt.grid('on') plt.xlabel('norm of weighted residual') plt.ylabel('norm of solution roughness') plt.xlim([.7,5]) plt.legend() plt.savefig('compare_misfit.png') plt.show()
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, 7 + 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'], label='drop',
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()
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()
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()