def Ftry(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # ltx = ekxx * cotd + ekyx * sotd + cb * lK - cot * Rg lty = ekxy * cotd + ekyy * sotd - sot * Rg ltz = ekxz * cotd + sb * lK # return (ltx * ekyx + lty * ekyy + ltz * ekyz) / (ltx * ekzx + lty * ekzy + ltz * ekzz)
def rotate_by_pivot(p, pivot, angle): result = [None] * 2 result[0] = (p[0] - pivot[0]) * np.cos(angle) + ( p[1] - pivot[1]) * np.sin(angle) + pivot[0] result[1] = (p[0] - pivot[0]) * -np.sin(angle) + ( p[1] - pivot[1]) * np.cos(angle) + pivot[1] return result
def ek(alpha, betas): ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) return np.array([[ca * cbs, -sbs, sa * cbs], [ca * sbs, cbs, sa * sbs], [-sa, 0, ca]])
def ltfree(lK, Rg, alpha, beta, delta, omegat): ltx = np.cos(alpha) * np.cos(np.radians(omegat) + delta) + np.cos( beta) * lK - np.cos(np.radians(omegat)) * Rg lty = np.sin(np.radians(omegat) + delta) - np.sin(np.radians(omegat)) * Rg ltz = -np.sin(alpha) * np.cos(np.radians(omegat) + delta) + np.sin(beta) * lK return np.sqrt(ltx * ltx + lty * lty + ltz * ltz)
def Mgrz(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # eaxx = ekxx * cotd + ekyx * sotd eaxy = ekxy * cotd + ekyy * sotd eaxz = ekxz * cotd # ltx = eaxx + cb * lK - cot * Rg lty = eaxy - sot * Rg ltz = eaxz + sb * lK # vcz = cot * lty - sot * ltx # return vcz / (ltx * ekzx + lty * ekzy + ltz * ekzz)
def rotate_arrow_and_text(BarNo): alpha = p*(BarNo-1)*alpha_c / 180 * np.pi x = x_ori* np.cos(alpha) + y_ori*-np.sin(alpha) y = x_ori* np.sin(alpha) + y_ori* np.cos(alpha) pu.pyx_arrow((x,y)) pu.pyx_text(( np.sign(x)*(abs(x)-1), np.sign(y)*(abs(y)-1) ), rf'${BarNo}$', scale=1.5) print(f'alpha={alpha/np.pi*180}')
def rA(lK, R, alpha, betas, beta, delta, omegat): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cotd = np.cos(np.radians(omegat) + delta) sotd = np.sin(np.radians(omegat) + delta) rAx = (ca * cbs * cotd - sbs * sotd) * R + cb * lK rAy = (ca * sbs * cotd + cbs * sotd) * R rAz = (-sa * cotd) * R + sb * lK return np.array([rAx, rAy, rAz])
def cosgammag(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) ltx = np.cos(alpha)*cotd + cb*lK - cot*Rg lty = sotd - sot*Rg ltz = -np.sin(alpha)*cotd + sb*lK Rwx = cot*Rg Rwy = sot*Rg Rwz = 0 lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) return (Rwx*lty-Rwy*ltx)/(lt*Rg)
def rB(R, omegat): cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) rBx = cot * R rBy = sot * R rBz = 0 return np.array([rBx, rBy, rBz])
def f2(t): val = psi_mu_min*np.sin(2*np.pi*(1/x) *(t-pt1[0])) if abs(val) > ell_limit: list_of_saturated_time.append(t) return np.sign(val)*ell_limit else: return val
def rK(lK, beta): cb = np.cos(beta) sb = np.sin(beta) rKx = cb * lK rKy = 0 rKz = sb * lK return np.array([rKx, rKy, rKz])
def cosgammak(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) Rax = ca*cotd Ray = sotd Raz = -sa*cotd ltx = Rax + cb*lK - cot*Rg lty = Ray - sot*Rg ltz = Raz + sb*lK lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) return (sa*(Ray*ltz-Raz*lty) + ca*(Rax*lty-Ray*ltx))/lt
def vt(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) ltx = ca*cotd + cb*lK - cot*Rg lty = sotd - sot*Rg ltz = -sa*cotd + sb*lK lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) dltxdt = -ca*sotd + sot*Rg dltydt = cotd - cot*Rg dltzdt = sa*sotd return (ltx*dltxdt+lty*dltydt+ltz*dltzdt)/lt
def GenSinPeak(self, x, begin, end, amp): """Generates a sinusoid peak between 'begin' and 'end' on the [x] axis with the amplitude 'amp'""" index = range(len(x)) y = [0] * len(x) for i in index: if (x[i] < end) and (x[i] > begin): y[i] = amp * np.sin(x[i] * np.pi / (end - begin) - np.pi * begin / (end - begin)) return y
def f2(t): val = psi_mu_min * np.sin(2 * np.pi * (freq2 / x) * (t - pt1[0])) if abs(val) > ell_limit: if t < pt2[0]: list_of_saturated_time_before_t2.append(t) else: list_of_saturated_time_after_t2.append(t) return np.sign(val) * ell_limit else: return val
def wall(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] midpoint = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] midpoint = parms[0] x0 = midpoint - thick / 2.0 th = np.deg2rad(theta) p = w * np.cos(th) Atot = w * w / np.sin(2.0 * th) out = [] for x in xvals: if (x <= (x0 - p)): val = ib #elif ((x0-p) >= (x-thick)) and ((x0+p) >= x): #Gauge volume is smaller than thickness if ((x > (x0 - p)) and (x0 - p > (x - thick)) and (x <= x0)): val = ib + i0 * ((x - (x0 - p)) * ((x - (x0 - p)) * np.tan(th))) / Atot elif ((x > x0) and (x <= x0 + p) and (x0 - p > (x - thick))): val = ib + i0 * (Atot - (x0 + p - x) * (x0 + p - x) * np.tan(th)) / Atot elif (x0 - p > (x - thick) and (x0 + p < x)): #Gauge volume is smaller than thickness val = ib + i0 elif (x0 - p < (x - thick) and (x0 + p > x)): #Gauge volume is larger than thickness A2 = (x0 + p - x) * (x0 + p - x) * np.tan(th) A3 = ((x - thick) - (x0 - p)) * (((x - thick) - (x0 - p)) * np.tan(th)) val = ib + i0 * (Atot - A2 - A3) / Atot elif ((x0 - p) < (x - thick)) and ((x0 + p) < x) and (x0 >= (x - thick)): A3 = ((x - thick) - (x0 - p)) * (((x - thick) - (x0 - p)) * np.tan(th)) val = ib + i0 * (Atot - A3) / Atot elif (x0 < (x - thick) and ((x0 + p >= (x - thick)))): A2 = (x0 + p - (x - thick)) * ((x0 + p) - (x - thick)) * np.tan(th) val = ib + i0 * (A2 / Atot) elif (x0 + p < (x - thick)): val = ib #elif ((x0-p) < (x-thick)) and ((x0+p) >= x): #Gauge volume is larger than thickness # A2 = (x0+p-x)*(x0+p-x)*np.tan(th) # A3 = ((x-thick)-(x0-p))*(((x-thick)-(x0-p))*np.tan(th)) # val = ib + i0*(Atot-A2-A3)/Atot out = out + [val] return np.array(out)
def arc(r0, R, e, n, phi0, phi): e1 = e / np.sqrt(np.sum(e * e)) # normalize en = n / np.sqrt(np.sum(n * n)) # normalize ip = np.argmax(phi > phi0) # find end index e2 = np.cross(en, e1) cp = np.cos(np.radians(phi[:ip])) sp = np.sin(np.radians(phi[:ip])) # r = cp*e1+sp*e2 r = np.zeros((3, ip)) r[0, :] = r0[0] + R * (cp * e1[0] + sp * e2[0]) r[1, :] = r0[1] + R * (cp * e1[1] + sp * e2[1]) r[2, :] = r0[2] + R * (cp * e1[2] + sp * e2[2]) return r
def Marz(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # eaxx = ekxx * cotd + ekyx * sotd eaxy = ekxy * cotd + ekyy * sotd eaxz = ekxz * cotd # ltx = eaxx + cb * lK - cot * Rg lty = eaxy - sot * Rg ltz = eaxz + sb * lK # vcx = eaxy * ltz - eaxz * lty vcy = eaxz * ltx - eaxx * ltz vcz = eaxx * lty - eaxy * ltx # d = ltx * ekzx + lty * ekzy + ltz * ekzz # return np.where(d > dmin, (vcx * ekzx + vcy * ekzy + vcz * ekzz) / d, 0.0)
def transformCoords(self, points, deg_addTheta=0): # This function takes in an nx2 array of coordinates of the form # [x,y] and returns rotated and translated coordinates. The # translation and rotation are described by obj.anchor_xy and # obj.theta. The optional "addTheta" argument adds an # additional angle of "addTheta" to the obj.theta attribute. transCoords = [] for point in points: # 旋转方向和eMach是反一下的,我是Park变换。 cosT = np.cos(self.theta + (deg_addTheta * np.pi / 180)) sinT = np.sin(self.theta + (deg_addTheta * np.pi / 180)) transCoords.append([ points[0] * cosT + points[1] * sinT, points[0] * -sinT + points[1] * cosT ]) return np.array(transCoords)
def transmission(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] #x0=p[0]; i0=p[1]; w=p[2]; theta=p[3]; ib=p[4]; thick=p[5]; u=p[6]; #th = np.deg2rad(theta) #p0 = w * np.cos(th)/np.cos(2*th) #out = [] #for x in xvals: # if x < (x0 - p0): # val = ib # elif ((x>=x0-p0) and (x < x0)): # val = i0*(x-x0+p0)*(x-x0+p0)+ib # elif ((x>=x0) and (x<(x0+p0))): # val = i0*(2.0*p0*p0-(x0+p0-x)*(x0+p0-x))+ib # elif (x>=x0+p0): # val=2*i0*p0*p0 +ib # out = out + [val] x0 = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] th = np.deg2rad(theta) p = w * np.cos(th) Atot = w * w / np.sin(2.0 * th) out = [] for x in xvals: if (x <= (x0 - p)): val = ib elif ((x > (x0 - p)) and (x <= x0)): val = ib + i0 * ((x - (x0 - p)) * ((x - (x0 - p)) * np.tan(th))) / Atot elif ((x > x0) and (x <= x0 + p)): val = ib + i0 * (Atot - (x0 + p - x) * (x0 + p - x) * np.tan(th)) / Atot elif (x > x0 + p): val = ib + i0 out = out + [val] return np.array(out)
def gammak2(lK, Rg, beta, delta, omegat): alpha = 0.5 * np.pi - beta cb = np.cos(beta) sb = np.sin(beta) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat) + delta) sotd = np.sin(np.radians(omegat) + delta) cotdt = np.cos(np.radians(omegat + 90) + delta) sotdt = np.sin(np.radians(omegat + 90) + delta) ltx = np.cos(alpha) * cotd + cb * lK - cot * Rg lty = sotd - sot * Rg ltz = -np.sin(alpha) * cotd + sb * lK eyrx = np.cos(alpha) * cotdt eyry = sotdt eyrz = -np.sin(alpha) * cotdt lt = np.sqrt(ltx * ltx + lty * lty + ltz * ltz) return np.degrees(np.arccos((eyrx * ltx + eyry * lty + eyrz * ltz) / lt))
ann.set_text('???') def hide_annotations(self): for ann in self.annotations: ann.set_visible(False) # --------------------------------------------------------------------- t = np.linspace(0.0, 1.0, 11) s1 = t s2 = -t s3 = t**2 s4 = -(t**2) s5 = np.sin(t * 2 * np.pi) s6 = np.cos(t * 2 * np.pi) fig = figure() ax1 = fig.add_subplot(321) ax1.plot(t, s1) ax1.scatter(t, s1) ax2 = fig.add_subplot(322) ax2.plot(t, s2) ax2.scatter(t, s2) ax3 = fig.add_subplot(323) ax3.plot(t, s3) ax3.scatter(t, s3)
from pylab import plt, np, xlim, ylim, figure, plot, xticks, yticks, subplot, show x = np.arange(-5.0, 5.0, 0.02) y1 = np.sin(x) plt.figure(1) plt.subplot(211) plt.plot(x, y1) plt.subplot(212) # 设置x轴范围 xlim(-2.5, 2.5) # 设置y轴范围 ylim(-1, 1) plt.plot(x, y1) # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show() '===========================================' plt.figure(1) # 第一张图 plt.subplot(211) # 第一张图中的第一张子图 plt.plot([1, 2, 3]) plt.subplot(212) # 第一张图中的第二张子图 plt.plot([4, 5, 6]) plt.figure(2) # 第二张图
def f1(t): return psi_mu_max * np.sin(2 * np.pi * (freq1 / x) * t) - BIAS
from matplotlib.widgets import MultiCursor from pylab import figure, show, np t = np.arange(0.0, 2.0, 0.01) s1 = np.sin(2*np.pi*t) s2 = np.sin(4*np.pi*t) fig = figure() ax1 = fig.add_subplot(211) ax1.plot(t, s1) ax2 = fig.add_subplot(212, sharex=ax1) ax2.plot(t, s2) multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1, horizOn=False, vertOn=True) show()
def rotate(_, x, y): return np.cos(_) * x + np.sin(_) * y, -np.sin(_) * x + np.cos(_) * y
def my_3d_plot_non_dominated_fronts(pop, paretoPoints, fea_config_dict, az=180, comp=[0, 1, 2], plot_option=1): """ Plots solutions to the DTLZ problems in three dimensions. The Pareto Front is also visualized if the problem id is 2,3 or 4. Args: pop (:class:`~pygmo.population`): population of solutions to a dtlz problem az (``float``): angle of view on which the 3d-plot is created comp (``list``): indexes the fitness dimension for x,y and z axis in that order Returns: ``matplotlib.axes.Axes``: the current ``matplotlib.axes.Axes`` instance on the current figure Raises: ValueError: if *pop* does not contain a DTLZ problem (veryfied by its name only) or if *comp* is not of length 3 Examples: >>> import pygmo as pg >>> udp = pg.dtlz(prob_id = 1, fdim =3, dim = 5) >>> pop = pg.population(udp, 40) >>> udp.plot(pop) # doctest: +SKIP """ from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np # from pylab import mpl # mpl.rcParams['font.family'] = ['Times New Roman'] # mpl.rcParams['font.size'] = 16.0 # if (pop.problem.get_name()[:-1] != "DTLZ"): # raise(ValueError, "The problem seems not to be from the DTLZ suite") if (len(comp) != 3): raise ( ValueError, "The kwarg *comp* needs to contain exactly 3 elements (ids for the x,y and z axis)" ) # Create a new figure fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111, projection='3d') # plot the points fits = np.transpose(pop.get_f()) try: pass # ax.plot(fits[comp[0]], fits[comp[1]], fits[comp[2]], 'ro') except IndexError: print('Error. Please choose correct fitness dimensions for printing!') if False: # Plot pareto front for dtlz 1 if plot_option == 1: # (pop.problem.get_name()[-1] in ["1"]): X, Y = np.meshgrid(np.linspace(0, 0.5, 100), np.linspace(0, 0.5, 100)) Z = -X - Y + 0.5 # remove points not in the simplex for i in range(100): for j in range(100): if X[i, j] < 0 or Y[i, j] < 0 or Z[i, j] < 0: Z[i, j] = float('nan') ax.set_xlim(0, 1.) ax.set_ylim(0, 1.) ax.set_zlim(0, 1.) ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) plt.plot([0, 0.5], [0.5, 0], [0, 0]) # Plot pareto fronts for dtlz 2,3,4 if plot_option == 2: # (pop.problem.get_name()[-1] in ["2", "3", "4"]): # plot the wireframe of the known optimal pareto front thetas = np.linspace(0, (np.pi / 2.0), 30) # gammas = np.linspace(-np.pi / 4, np.pi / 4, 30) gammas = np.linspace(0, (np.pi / 2.0), 30) x_frame = np.outer(np.cos(thetas), np.cos(gammas)) y_frame = np.outer(np.cos(thetas), np.sin(gammas)) z_frame = np.outer(np.sin(thetas), np.ones(np.size(gammas))) ax.set_autoscalex_on(False) ax.set_autoscaley_on(False) ax.set_autoscalez_on(False) ax.set_xlim(0, 1.8) ax.set_ylim(0, 1.8) ax.set_zlim(0, 1.8) ax.plot_wireframe(x_frame, y_frame, z_frame) # https://stackoverflow.com/questions/37000488/how-to-plot-multi-objectives-pareto-frontier-with-deap-in-python # def simple_cull(inputPoints, dominates): # paretoPoints = set() # candidateRowNr = 0 # dominatedPoints = set() # while True: # candidateRow = inputPoints[candidateRowNr] # inputPoints.remove(candidateRow) # rowNr = 0 # nonDominated = True # while len(inputPoints) != 0 and rowNr < len(inputPoints): # row = inputPoints[rowNr] # if dominates(candidateRow, row): # # If it is worse on all features remove the row from the array # inputPoints.remove(row) # dominatedPoints.add(tuple(row)) # elif dominates(row, candidateRow): # nonDominated = False # dominatedPoints.add(tuple(candidateRow)) # rowNr += 1 # else: # rowNr += 1 # if nonDominated: # # add the non-dominated point to the Pareto frontier # paretoPoints.add(tuple(candidateRow)) # if len(inputPoints) == 0: # break # return paretoPoints, dominatedPoints # def dominates(row, candidateRow): # return sum([row[x] >= candidateRow[x] for x in range(len(row))]) == len(row) # import random # print(inputPoints) # inputPoints = [[random.randint(70,100) for i in range(3)] for j in range(500)] # print(inputPoints) # quit() # inputPoints = [(x,y,z) for x,y,z in zip(fits[comp[0]], fits[comp[1]], fits[comp[2]])] # paretoPoints, dominatedPoints = simple_cull(inputPoints, dominates) x = [coords[0] / 1000 for coords in paretoPoints] y = [coords[1] for coords in paretoPoints] z = [coords[2] for coords in paretoPoints] # from surface_fitting import surface_fitting # surface_fitting(x,y,z) # quit() if False: pass else: import pandas as pd from pylab import cm print(dir(cm)) df = pd.DataFrame({'x': x, 'y': y, 'z': z}) # 只有plot_trisurf这一个函数,输入是三个以为序列的,其他都要meshgrid得到二维数组的(即ndim=2的数组) # # https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html # surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.magma, linewidth=0.1, edgecolor='none') # surf = ax.plot_trisurf(x, y, z, cmap='viridis', edgecolor='none') # surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.magma, linewidth=0.1) surf = ax.plot_trisurf(df.x, df.y * 100, df.z, cmap=cm.Spectral, linewidth=0.1) with open('./%s_PF_points.txt' % (fea_config_dict['run_folder'][:-1]), 'w') as f: f.write('TRV,eta,OC\n') f.writelines([ '%g,%g,%g\n' % (a, b, c) for a, b, c in zip(df.x, df.y * 100, df.z) ]) # quit() fig.colorbar(surf, shrink=0.5, aspect=5) ax.set_xlabel(' \n$\\rm -TRV$ [$\\rm kNm/m^3$]') ax.set_ylabel(' \n$-\\eta$ [%]') ax.set_yticks(np.arange(-96, -93.5, 0.5)) ax.set_zlabel(r'$O_C$ [1]') # Try to export data from plot_trisurf # https://github.com/WoLpH/numpy-stl/issues/19 # print(surf.get_vector()) # plt.savefig('./plots/avgErrs_vs_C_andgamma_type_%s.png'%(k)) # plt.show() # # rotate the axes and update # for angle in range(0, 360): # ax.view_init(30, angle) # plt.draw() # plt.pause(.001) ax.view_init(azim=245, elev=15) # fig.tight_layout() # Y730 # fig.savefig(r'C:\Users\horyc\Desktop/3D-plot.png', dpi=300, layout='tight') # ax.view_init(azim=az) # ax.set_xlim(0, 1.) # ax.set_ylim(0, 1.) # ax.set_zlim(0, 10.) return ax
def iPark(P, theta): return [ P[0] * np.cos(theta) + P[1] * -np.sin(theta), P[0] * np.sin(theta) + P[1] * np.cos(theta) ]
def reflection1(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] x0 = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] th = np.deg2rad(theta) p = w * np.sin(th) Atot = w * w / np.sin(2.0 * th) out = [] ni = 5 irng = np.array(range(1, ni + 1)) for x in xvals: if x < (x0 - p): val = ib elif ((x0 - p) < x and x0 > x): l1 = x - (x0 - p) nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1 = l1 / float(nrleft) dl = irngleft * dl1 triA = dl * dl / np.tan(th) #triangle areas secA = [triA[0] ] + [triA[i] - triA[i - 1] for i in range(1, nrleft)] #section areas secA = np.array(secA) m1 = np.linspace( x0 - p + dl1 / 2.0, x - dl1 / 2.0, nrleft ) #section midpoint position - path length calculated from this plen = np.abs(2 * m1 / np.sin(th)) val = ib + np.sum(i0 * secA * np.exp(-u * plen)) elif (x0 <= x) and (x0 + p >= x): l1 = p nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1left = l1 / float(nrleft) dlleft = irngleft * dl1left triAleft = dlleft * dlleft / np.tan(th) #triangle areas secAleft = [triAleft[0]] + [ triAleft[i] - triAleft[i - 1] for i in range(1, nrleft) ] #section areas secAleft = np.array(secAleft) m1left = np.linspace(x0 - p + dl1left / 2.0, x0 - dl1left / 2.0, nrleft) + (x - x0) plenleft = np.abs(2 * m1left / np.sin(th)) valleft = np.sum(i0 * secAleft * np.exp(-u * plenleft)) l2 = x - x0 nrright = int(np.ceil(x - x0 / (2 * p) * ni)) if nrright < 1: nrright = 1 irngright = np.array(range(1, nrright + 1)) dl1right = l2 / float(nrright) dlright = p - np.append(0.0, dl1right * irngright) triAright = dlright * dlright / np.tan(th) secAright = [ triAright[i] - triAright[i + 1] for i in range(nrright) ] secAright = np.array(secAright) m1right = np.linspace(x - x0 - dl1right / 2.0, dl1right / 2.0, nrright) plenright = np.abs(2 * m1right / np.sin(th)) valright = np.sum(i0 * secAright * np.exp(-u * plenright)) val = ib + valleft + valright elif (x > x0 + p): l1 = p #nrleft = int(np.ceil(l1/(x-(x0-p))*ni)); nrleft = int(np.ceil(l1 / (2 * p) * ni)) if nrleft < 1: nrleft = 1 irngleft = np.array(range(1, nrleft + 1)) dl1left = l1 / float(nrleft) dlleft = irngleft * dl1left triAleft = dlleft * dlleft / np.tan(th) #triangle areas secAleft = [triAleft[0]] + [ triAleft[i] - triAleft[i - 1] for i in range(1, nrleft) ] #section areas secAleft = np.array(secAleft) m1left = np.linspace(x0 - p + dl1left / 2.0, x0 - dl1left / 2.0, nrleft) + (x - x0) plenleft = np.abs(2 * m1left / np.sin(th)) valleft = np.sum(i0 * secAleft * np.exp(-u * plenleft)) l2 = p nrright = int(np.ceil(l2 / (2 * p) * ni)) if nrright < 1: nrright = 1 irngright = np.array(range(1, nrright + 1)) dl1right = l2 / float(nrright) dlright = p - np.append(0.0, dl1right * irngright) triAright = dlright * dlright / np.tan(th) secAright = [ triAright[i] - triAright[i + 1] for i in range(nrright) ] secAright = np.array(secAright) m1right = np.linspace(dlright[0] - dl1right / 2.0, dlright[-1] + dl1right / 2.0, nrright) + (x - (x0 + p)) plenright = np.abs(2 * m1right / np.sin(th)) valright = np.sum(i0 * secAright * np.exp(-u * plenright)) val = ib + valleft + valright out = out + [val] return np.array(out)
def reflection(xvals, p): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] x0 = p[0] i0 = p[1] w = p[2] theta = p[3] ib = p[4] thick = p[5] u = p[6] th = np.deg2rad(theta) p0 = w * np.sin(th) / np.sin(2 * th) out = [] for x in xvals: if x < (x0 - p0): val = ib elif ((x >= (x0 - p0)) and (x < x0)): val = i0 * ((x - x0 + p0) / u) - i0 * (np.sin(th) / (2 * u * u)) * (1 - np.exp( (-2 * u / np.sin(th)) * (x - x0 + p0))) + ib elif ((x >= x0) and (x < (x0 + p0))): val = i0 * (np.sin(th) / (2 * u * u)) * (np.exp( (-2 * u / np.sin(th)) * (x - x0 + p0)) - 2 * np.exp( (-2 * u / np.sin(th)) * (x - x0)) + 1) + i0 * ((x0 + p0 - x) / u) + ib elif (x >= (x0 + p0)): val = i0 * np.sin(th) / ( 2 * u * u) * (np.exp(-2 * u * (x - x0 + p0) / np.sin(th)) - 2 * np.exp(-2 * u * (x - x0) / np.sin(th)) + np.exp(-2 * u * (x - x0 - p0) / np.sin(th))) + ib out = out + [val] return np.array(out)
ann.set_text('???') def hide_annotations(self): for ann in self.annotations: ann.set_visible(False) # --------------------------------------------------------------------- t = np.linspace(0.0, 1.0, 11) s1 = t s2 = -t s3 = t**2 s4 = -(t**2) s5 = np.sin(t*2*np.pi) s6 = np.cos(t*2*np.pi) fig = figure() ax1 = fig.add_subplot(321) ax1.plot(t, s1) ax1.scatter(t, s1) ax2 = fig.add_subplot(322) ax2.plot(t, s2) ax2.scatter(t, s2) ax3 = fig.add_subplot(323) ax3.plot(t, s3) ax3.scatter(t, s3)
from __future__ import print_function from pylab import figure, show, np Ntests = 3 t = np.arange(0.0, 1.0, 0.05) s = np.sin(2 * np.pi * t) # scatter creates a RegPolyCollection fig = figure() ax = fig.add_subplot(Ntests, 1, 1) N = 100 x, y = 0.9 * np.random.rand(2, N) area = np.pi * (10 * np.random.rand(N)) ** 2 # 0 to 10 point radiuses ax.scatter(x, y, s=area, marker='^', c='r', label='scatter') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 2) ax.vlines(t, [0], np.sin(2 * np.pi * t), label='vlines') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 3) ax.plot(t, s, 'b-', lw=2, label='a line') ax.legend() fig.savefig('legend_unit') show()
from pylab import figure, show, np Ntests = 3 t = np.arange(0.0, 1.0, 0.05) s = np.sin(2 * np.pi * t) # scatter creates a RegPolyCollection fig = figure() ax = fig.add_subplot(Ntests, 1, 1) N = 100 x, y = 0.9 * np.random.rand(2, N) area = np.pi * (10 * np.random.rand(N))**2 # 0 to 10 point radiuses ax.scatter(x, y, s=area, marker='^', c='r', label='scatter') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 2) ax.vlines(t, [0], np.sin(2 * np.pi * t), label='vlines') ax.legend() # vlines creates a LineCollection ax = fig.add_subplot(Ntests, 1, 3) ax.plot(t, s, 'b-', lw=2, label='a line') ax.legend() fig.savefig('legend_unit') show()