def line_fit(x, y, xlabel, ylabel, param_units, param_names=['m', 'q'], dy=None, dx=None, err_fit=None, title=''): p0 = [1., 1.] opt, pcov = curve_fit(functions.line, x, y, sigma=err_fit) param_errors = numpy.sqrt(numpy.diagonal(pcov)[0]) res = y - functions.line(x, *opt) chi2 = (res**2) / (err_fit**2) chi2 = chi2.sum() ndof = len(x) - len(opt) plt.figure() plt.subplot(2, 1, 1) plt.errorbar(x, y, yerr=dy, xerr=dx, fmt='.') legend = fit_legend(opt, param_errors, param_names, param_units, chi2, ndof) x_new = numpy.linspace(0., 300., 1000) plt.plot(x_new, functions.line(x_new, *opt), 'r', label=legend) set_plot(xlabel, ylabel, title=title) plt.subplot(2, 1, 2) plt.errorbar(x, res, yerr=err_fit, fmt='.') set_plot(xlabel, "residui", title='') return opt, pcov
def eq_w_numerical(self,bkg0,bkg1,output_plot=True): '''Given the continuum level as a line between two points, computes the equivalent width by adding up the flux.''' m = (bkg1[1] - bkg0[1])/(bkg1[0] - bkg0[0]) b = bkg0[1] - m * bkg0[0] ind = np.where((self.wls[0] > bkg0[0]) & (self.wls[0] < bkg1[0])) print("Wavelengths used", self.wls[0][ind]) height = 0.5 * (bkg0[1] + bkg1[1]) width = self.wls[0][ind][-1] - self.wls[0][ind][0] total_flux = np.trapz(self.fls[0][ind],self.wls[0][ind]) continuum_flux = height * width print("Total Flux", total_flux) print("Continuum Flux", continuum_flux) print("Total - Continuum", total_flux - continuum_flux) line = lambda x: functions.line(x,[m,b]) if output_plot: plt.plot(self.wls[0][ind],self.fls[0][ind]) plt.plot(self.wls[0][ind],line(self.wls[0][ind]),ls="-") plt.show() return (total_flux - continuum_flux)/ height
import numpy as np import matplotlib.pyplot as mpl # import time as t from functions import line, exact, sden_kink, var_phi from const import * mpl.close("all") # We initialize the field (phi) with all values to zero varf = np.zeros(n) # Arrays with initial information about the simulation f = line(xmax) # Here we define the exact solution fexact = exact(xmax) # Discrete integral of the energy in all the system. energy = 0.0 for i in range(n): energy = energy + sden_kink(i, f) for loop in range(loops): # Calculation of variations for j in range(1, n - 1): varf[j] = var_phi(j, f) # Implementation of variations
ax4.set_title("Power spectrum") ax4.set_yscale("log") ax4.set_xscale("log") fig4 = plt.figure(4, figsize=(10, 7)) ax5 = fig4.add_subplot(2, 1, 1) ax6 = fig4.add_subplot(2, 1, 2) fig5 = plt.figure(5, figsize=(10, 7)) ax7 = fig5.add_subplot(2, 1, 1) ax8 = fig5.add_subplot(2, 1, 2) ax7.set_title("volitility test") #plots ax.plot(t, y, color="r") ax.plot(t, f.line(np.array(t), *linepara)) ax2.plot(t2, y2) values, bounds, patches = ax3.hist(brownianhist, 100) gausspara = f.fitgauss(f.bincenters(bounds), values) ax3.plot(f.bincenters(bounds), f.gauss(f.bincenters(bounds), *gausspara)) ax4.plot(f1, s1, "o", markerfacecolor="None") ax4.plot(f1[:2000], f.logline(f1, *linepara2)[:2000]) ax5.plot(t3, noise) ax6.plot(f3, s3, "o", markerfacecolor="None") ax6.set_yscale("log") ax6.set_xscale("log") ax7.plot(t2, y2, color="black") ax7.plot(t5, avg, color="blue") ax7.plot(t6, avg2, color="green") ax8.plot(t4, vol, color="red")
def sub(x, y): for i in range(0, 200, 20): if functions.line(i, 15, i + 10, 5, x, y) == 0: pygame.gfxdraw.pixel(gameDisplay, i, i + 10, alwan.black)
def drow_bord(): black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) gameDisplay = pygame.display.set_mode((200, 155)) gameDisplay.fill(white) b = 5 a = 0 while (a < 300): l = [] m = [] n = [] p = [] for i in range(0, 11): l.append([2 * 10 * i, 10 + a + b]) m.append([2 * i * 10 + 10, 0 + a + b]) pygame.draw.line(gameDisplay, red, tuple(l[i]), tuple(m[i]), 1) n.append([2 * 10 * i, 20 + a + b]) p.append([2 * i * 10 + 10, 30 + a + b]) pygame.draw.line(gameDisplay, red, tuple(n[i]), tuple(p[i]), 1) for i in range(0, len(m) - 1): pygame.draw.line(gameDisplay, red, tuple(m[i]), tuple(l[i + 1]), 1) for i in range(0, len(p) - 1): pygame.draw.line(gameDisplay, red, tuple(p[i]), tuple(n[i + 1]), 1) for i in range(0, 11): pygame.draw.line(gameDisplay, red, (i * 20, 10 + a + b), (i * 20, 20 + a + b), 1) a = a + 40 print(300 - a) for i in range(0, 11): pygame.draw.line(gameDisplay, red, (i * 20 + 10, 0), (i * 20 + 10, 5), 1) for i in range(0, 11): for j in range(0, 11): pygame.draw.line(gameDisplay, red, (i * 20 + 10, 35 + 40 * j), (i * 20 + 10, 45 + 40 * j), 1) for i in range(200): for j in range(155): if functions.line(0, 15, 10, 5, i, j) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, black) for x in range(10, 200, 20): for i in range(200): for j in range(155): if functions.line(x, 5, x + 10, 15, i, j) == 0 and functions.line( x + 10, 15, x + 20, 5, i, j) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, black) for i in range(200): for j in range(155): if functions.line(0, 145, 10, 155, i, j) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, black) for x in range(10, 200, 20): for i in range(200): for j in range(155): if functions.line(x, 155, x + 10, 145, i, j) == 1 and functions.line( x + 10, 145, x + 20, 155, i, j) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, black) """ for z in range(0, 50, 10): for i in range(200): for j in range(155): if functions.line(z, z + 25, z + 10, z + 35, i, j) == 1 and functions.vert(z+10 , i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) """ for i in range(200): for j in range(155): if functions.line(0, 25, 10, 35, i, j) == 1 and functions.vert( 10, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(10, 45, 20, 55, i, j) == 1 and functions.vert( 20, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(20, 65, 30, 75, i, j) == 1 and functions.vert( 30, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(30, 85, 40, 95, i, j) == 1 and functions.vert( 40, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(40, 105, 50, 115, i, j) == 1 and functions.vert( 50, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(50, 125, 60, 135, i, j) == 1 and functions.vert( 60, i) == 1: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(200, 135, 190, 125, i, j) == 0 and functions.vert(190, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(190, 115, 180, 105, i, j) == 0 and functions.vert(180, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(180, 95, 170, 85, i, j) == 0 and functions.vert( 170, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(170, 75, 160, 65, i, j) == 0 and functions.vert( 160, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(160, 55, 150, 45, i, j) == 0 and functions.vert( 150, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red) for i in range(200): for j in range(155): if functions.line(150, 35, 140, 25, i, j) == 0 and functions.vert( 140, i) == 0: pygame.gfxdraw.pixel(gameDisplay, i, j, red)
fit_function=functions.costant, p0=None, x_min=x_min, x_max=x_max) opt_line, pcov_line = plot_functions.do_fit(bins_center, asimmetry, asimmetry_err, param_names=['m', 'costant'], param_units=['MHz', ''], fit_function=functions.line, p0=None, x_min=x_min, x_max=x_max) plt.subplot(2, 1, 2) residui = (asimmetry - functions.line(bins_center, *opt_line)) / asimmetry_err plot_functions.scatter_plot(bins_center, residui, 'dt [$\mu$s]', 'Residui ', dy=asimmetry_err / asimmetry_err, title='') #likelihood test: mask = (bins_center > x_min) asimmetry = asimmetry[mask] asimmetry_err = asimmetry_err[mask] bins_center = bins_center[mask] l_likelihood_wave = functions.gauss_log_likelihood(bins_center, asimmetry, asimmetry_err, functions.wave,
def make_f_theta_grid(delTheta=0.025, delF=[0.04, 0.01]): """ Using parameters found in f_theta_line.py, make a parallelogram grid of points in (Theta, F) space for use in cylindrical equilibrium modeling. In this function x means Theta and y means F. """ import matplotlib.mlab as mm import numpy as np import functions as fu reload(fu) # Specify the boundaries of the grid. # Original, simple case for 2012-02-17 AM or early PM. # Parallelogram case #def make_f_theta_grid(delTheta=0.025, delF=0.025): # [x2, y2] = [1.0, 0.0] # [x3, y3] = [2.25, 0.0] # [x0, y0] = [2.5, -2.0] # [x1, y1] = [3.75, -2.0] # Nx = int((x1 - x0) / delTheta + 1) # Ny = int((y2 - y0) / delF + 1) # x = np.linspace(x0, x1, Nx) # y = np.linspace(y0, y2, Ny) # X, Y = np.meshgrid(x, y) # a, b = fu.line([x0, y0], [x2, y2]) # for i in range(Ny): # X[i] = X[i] - (x0 - (Y[i] - b) / a) # return X, Y # Updated case from 2012-02-17 late PM or later. # Trapezoid case until 2012-10-12 # called larger_theta_f_space # with (delTheta=0.025, delF=[0.04, 0.01]): #[x2, y2] = [1.25, 0.125] #[x3, y3] = [1.625, 0.125] #[x0, y0] = [2.375, -2.0] #[x1, y1] = [3.75, -2.0] # Trapezoid case until 2012-10-14 # called smaller_theta_f_space # with delTheta=0.05, delF=[0.05, 0.01] #[x2, y2] = [1.375, 0.0] #[x3, y3] = [1.675, 0.0] #[x0, y0] = [2.25, -1.75] #[x1, y1] = [3.75, -1.75] # Trapezoid case after 2012-10-13 (not used yet): # called larger_theta_f_space, like pre-2012-10-12, # with (delTheta=0.025, delF=[0.04, 0.01]): # MST [x2, y2] = [1.25, 0.125] [x3, y3] = [1.625, 0.125] [x0, y0] = [2.375, -2.0] [x1, y1] = [3.75, -2.0] # RELAX [x2, y2] = [0.75, 0.5] [x3, y3] = [1.25, 0.5] [x0, y0] = [2.75, -1.5] [x1, y1] = [4.25, -1.5] # Get the desired delTheta resolution at middle altitude: Nx = int(((x1 + x3) - (x0 + x2)) / 2.0 / delTheta + 1) # Old way is to use regular y, i.e. constant diff(y). #Ny = int((y2 - y0) / delF + 1) #y = np.array([[i] for i in np.linspace(y0, y2, Ny)]) #Y = np.tile(y, Nx) # New way is to use y with a varying diff(y). # Get the desired delF resolution as a function of f. # Note the sum delF[0] + delF[1] should be an integer divisor of # 2(y2 - y0). Ny = int(2.0 * (y2 - y0) / (delF[0] + delF[1])) + 2 delFuse = np.append( 0.0, delF[0] + (delF[1] - delF[0]) / (Ny - 2.0) * np.arange(Ny - 1)) y = y0 + np.cumsum(delFuse) X = np.zeros([Ny, Nx]) Y = X.copy() aleft, bleft = fu.line([x0, y0], [x2, y2]) aright, bright = fu.line([x1, y1], [x3, y3]) Y = np.tile(np.array([y]).T, Nx) for i in range(Ny): X[i] = np.linspace((y[i] - bleft) / aleft, (y[i] - bright) / aright, Nx) return X, Y
plt.close("all") plt.figure('Fig1') plt.suptitle('Lineas Rectas') # Ponemos un título #Podemos usar subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) #Para ajustar los espacios de las figuras pequeñas plt.subplots_adjust(wspace = 0.5) ##Vamos hacer 3 gráficas en una sóla figura plt.subplot(1,3,1) plt.title('Linea 1') plt.xlabel('x[m]') # Ponemos etiqueta al eje x plt.ylabel('y[m]') # Ponemos etiqueta al eje y #Aquí vemos como cambiar el marcador, su tamaño, su color de contorno (edge) y el color de relleno (face) plt.plot(x,f2d.line(x,1,5),linestyle="",marker="o",markersize=5,markeredgecolor="blue",markerfacecolor="red") plt.subplot(1,3,2) plt.title('Linea 2') plt.xlabel('x[m]') # Ponemos etiqueta al eje x plt.ylabel('y[m]') # Ponemos etiqueta al eje y #Aquí vemos como cambiar el tipo de línea y su grosor plt.plot(x,f2d.line(x,-10,-4), color="blue",linestyle="--",linewidth=5) plt.subplot(1,3,3) plt.title('Linea 3') plt.xlabel('x[m]') # Ponemos etiqueta al eje x plt.ylabel('y[m]') # Ponemos etiqueta al eje y #Aquí vemos como cambiar tanto líneas como marcadores.