def plot_contours(obj, top_bottom=True): '''A function that plots the BRF as an azimuthal projection with contours over the TOC and soil. Input: rt_layers object, top_bottom - True if only TOC plot, False if both TOC and soil. Output: contour plot of brf. ''' sun = ((np.pi - obj.sun0[0]) * np.cos(obj.sun0[1] + np.pi), \ (np.pi - obj.sun0[0]) * np.sin(obj.sun0[1] + np.pi)) theta = obj.views[:, 0] x = np.cos(obj.views[:, 1]) * theta y = np.sin(obj.views[:, 1]) * theta z = obj.I_top_bottom #* -obj.mu_s if top_bottom == True: if np.max > 1.: maxz = np.max(z) else: maxz = 1. else: maxz = np.max(z[:obj.n / 2]) minz = 0. #np.min(z) space = np.linspace(minz, maxz, 11) x = x[:obj.n / 2] y = y[:obj.n / 2] zt = z[:obj.n / 2] zb = z[obj.n / 2:] fig = plt.figure() if top_bottom == True: plt.subplot(121) plt.plot(sun[0], sun[1], 'ro') triang = tri.Triangulation(x, y) plt.gca().set_aspect('equal') plt.tricontourf(triang, zt, space, vmax=maxz, vmin=minz) plt.title('TOC BRF') plt.ylabel('Y') plt.xlabel('X') if top_bottom == True: plt.subplot(122) plt.plot(sun[0], sun[1], 'ro') plt.gca().set_aspect('equal') plt.tricontourf(triang, zb, space, vmax=maxz, vmin=minz) plt.title('Soil Absorption') plt.ylabel('Y') plt.xlabel('X') s = obj.__repr__() if top_bottom == True: cbaxes = fig.add_axes([0.11, 0.1, 0.85, 0.05]) plt.suptitle(s, x=0.5, y=0.93) plt.colorbar(orientation='horizontal', ticks=space,\ cax = cbaxes, format='%.3f') else: plt.suptitle(s, x=0.5, y=0.13) plt.colorbar(orientation='horizontal', ticks=space,\ format='%.3f') #plt.tight_layout() plt.show()
def plot_contours(obj, top_bottom=True): '''A function that plots the BRF as an azimuthal projection with contours over the TOC and soil. Input: rt_layers object, top_bottom - True if only TOC plot, False if both TOC and soil. Output: contour plot of brf. ''' sun = ((np.pi - obj.sun0[0]) * np.cos(obj.sun0[1] + np.pi), \ (np.pi - obj.sun0[0]) * np.sin(obj.sun0[1] + np.pi)) theta = obj.views[:,0] x = np.cos(obj.views[:,1]) * theta y = np.sin(obj.views[:,1]) * theta z = obj.I_top_bottom # * -obj.mu_s if top_bottom == True: if np.max > 1.: maxz = np.max(z) else: maxz = 1. else: maxz = np.max(z[:obj.n/2]) minz = 0. #np.min(z) space = np.linspace(minz, maxz, 11) x = x[:obj.n/2] y = y[:obj.n/2] zt = z[:obj.n/2] zb = z[obj.n/2:] fig = plt.figure() if top_bottom == True: plt.subplot(121) plt.plot(sun[0], sun[1], 'ro') triang = tri.Triangulation(x, y) plt.gca().set_aspect('equal') plt.tricontourf(triang, zt, space, vmax=maxz, vmin=minz) plt.title('TOC BRF') plt.ylabel('Y') plt.xlabel('X') if top_bottom == True: plt.subplot(122) plt.plot(sun[0], sun[1], 'ro') plt.gca().set_aspect('equal') plt.tricontourf(triang, zb, space, vmax=maxz, vmin=minz) plt.title('Soil Absorption') plt.ylabel('Y') plt.xlabel('X') s = obj.__repr__() if top_bottom == True: cbaxes = fig.add_axes([0.11,0.1,0.85,0.05]) plt.suptitle(s,x=0.5,y=0.93) plt.colorbar(orientation='horizontal', ticks=space,\ cax = cbaxes, format='%.3f') else: plt.suptitle(s,x=0.5,y=0.13) plt.colorbar(orientation='horizontal', ticks=space,\ format='%.3f') #plt.tight_layout() plt.show()
def triplot(x,y,z,r=0.001,title = 'band'): # z = c-c.min() # z /= z.max() triang = tri.Triangulation(x,y) xmid = x[triang.triangles].var(axis=1) ymid = y[triang.triangles].var(axis=1) mask = sp.where(xmid*xmid + ymid*ymid > r*r, 1, 0) triang.set_mask(mask) pl.figure() pl.gca().set_aspect('equal') pl.tricontourf(triang, z) pl.colorbar() V = sp.arange(-10,10,dtype=sp.double)/10*z.max() pl.tricontour(triang, z,V)#, colors='k') pl.title(title)
def triplot(x, y, z, r=0.001, title='band'): # z = c-c.min() # z /= z.max() triang = tri.Triangulation(x, y) xmid = x[triang.triangles].var(axis=1) ymid = y[triang.triangles].var(axis=1) mask = sp.where(xmid * xmid + ymid * ymid > r * r, 1, 0) triang.set_mask(mask) pl.figure() pl.gca().set_aspect('equal') pl.tricontourf(triang, z) pl.colorbar() V = sp.arange(-10, 10, dtype=sp.double) / 10 * z.max() pl.tricontour(triang, z, V) #, colors='k') pl.title(title)
vvv = np.zeros(len(p)) ppp = np.zeros(len(p)) # speed sound = np.sqrt(gamma * pp / dd) veloc = np.sqrt(uu**2 + vv**2) / sound # normalized to local Mach speed for i in range(len(p)): ind0 = np.where(tri[:, 0] == i) ind1 = np.where(tri[:, 1] == i) ind2 = np.where(tri[:, 2] == i) vvv[i] = np.mean(np.concatenate((veloc[ind0], veloc[ind1], veloc[ind2]))) ppp[i] = np.mean(np.concatenate((pp[ind0], pp[ind1], pp[ind2]))) plt.figure() plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, vvv, 30) plt.colorbar() plt.title('SPEED [IN MACH UNITS]') plt.xlabel('x') plt.ylabel('y') plt.show() # subsonic region #v_reg = np.zeros(len(vvv)) v_reg = vvv inda = np.where(vvv <= 1) indb = np.where(vvv > 1) #v_reg[inda] = -1 v_reg[indb] = 2 plt.figure()
import numpy as np import matplotlib.pylab as pl x = np.loadtxt('./data/x.txt') y = np.loadtxt('./data/y.txt') p = np.loadtxt('./data/p.txt') print(sum(p)) pl.figure(figsize=(10., 10.), dpi=1000) pl.xlim((300, 500)) pl.ylim((250, 450)) pl.tricontourf(x, y, p, 300, cmap='hot') #pl.tricontour(x,y,p, 300,cmap='hot') #pl.scatter(x,y,c=p, cmap = 'hot') pl.colorbar() pl.savefig('TW.png')
def __call__(self): x1, x2, y1, y2, t = self.x1, self.x2, self.y1, self.y2, self.t p, tri = self.p, self.tri pp, dd, uu, vv = self.pp, self.dd, self.uu, self.vv Ibrief = self.Ibrief atm_units = 0 # False if np.max(pp) > 1.0e5: # change from Pascal units to 1 atm units atm_units = 1 # True p_1atm = 1.01325e5 pp = pp / p_1atm # cell location (middle of the triangle) cell = (1.0 / 3.0) * (p[tri[:, 0]] + p[tri[:, 1]] + p[tri[:, 2]]) # tricontour uses the pressure and density at the nodes, instead # of the calculated value of pp and dd in the cells, so we have # to assign a value to pressure and density to the nodes. # Sometimes, a few nodes do not appear in the triangulation # The pressure and density on these nodes will be defined as # the mean value of the pressure and density arrays. For normal # size meshes, this will not affect the visual plot ppp = np.zeros(len(p)) ddd = np.zeros(len(p)) pp_mean = np.mean(pp) dd_mean = np.mean(dd) for i in range(len(p)): ind0 = np.where(tri[:, 0] == i) ind1 = np.where(tri[:, 1] == i) ind2 = np.where(tri[:, 2] == i) aa = list(np.concatenate((ind0[0], ind1[0], ind2[0]))) if len(aa): # not empty ppp[i] = np.mean(pp[aa]) ddd[i] = np.mean(dd[aa]) else: # node not in tri ppp[i] = pp_mean ddd[i] = dd_mean if Ibrief == 1: plt.figure() plt.subplot(2, 2, 1) plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ppp, 256) plt.colorbar() if atm_units == 0: plt.title('PRESSURE') else: plt.title('PRESSURE [ATM]') plt.ylabel('y') plt.subplot(2, 2, 2) plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ddd, 256) plt.colorbar() plt.title('DENSITY') plt.xlabel('x') plt.subplot(2, 2, 3) # -------------------------------- # u, v # -------------------------------- xx = cell[:, 0] yy = cell[:, 1] uu_tri = uu[0:len(tri)] vv_tri = vv[0:len(tri)] # generate 100 points for (u,v) plot if len(xx) * len(yy) <= 100 or len(xx) < 10 or len(yy) < 10: plt.quiver(xx, yy, uu_tri, vv_tri) plt.axis('equal') plt.xlabel('velocity field') plt.show() else: mxx = np.zeros(100) myy = np.zeros(100) muu = np.zeros(100) mvv = np.zeros(100) ddx = 0.1 * (x2 - x1) ddy = 0.1 * (y2 - y1) n = 0 for i in range(10): for j in range(10): ind = (xx > x1 + ddx * i) & ( xx <= x1 + ddx * (i + 1)) & (yy > y1 + ddy * j) & (yy <= y1 + ddy * (j + 1)) if np.any(ind == True): mxx[n] = np.mean(xx[np.where(ind == True)]) myy[n] = np.mean(yy[np.where(ind == True)]) muu[n] = np.mean(uu_tri[np.where(ind == True)]) mvv[n] = np.mean(vv_tri[np.where(ind == True)]) else: mxx[n] = x1 + 0.5 * ddx * (2 * i + 1) myy[n] = y1 + 0.5 * ddy * (2 * j + 1) muu[n] = 0.0 mvv[n] = 0.0 n += 1 plt.quiver(mxx, myy, muu, mvv) plt.axis('equal') plt.xlabel('velocity field') ind = np.argmin(pp) pmin = pp[ind] # cell location (middle of the triangle) pmin_cell = (1.0 / 3.0) * (p[tri[ind, 0]] + p[tri[ind, 1]] + p[tri[ind, 2]]) ind = np.argmax(pp) pmax = pp[ind] # cell location (middle of the triangle) pmax_cell = (1.0 / 3.0) * (p[tri[ind, 0]] + p[tri[ind, 1]] + p[tri[ind, 2]]) ind = np.argmin(dd) dmin = dd[ind] # cell location (middle of the triangle) dmin_cell = (1.0 / 3.0) * (p[tri[ind, 0]] + p[tri[ind, 1]] + p[tri[ind, 2]]) ind = np.argmax(dd) dmax = dd[ind] # cell location (middle of the triangle) dmax_cell = (1.0 / 3.0) * (p[tri[ind, 0]] + p[tri[ind, 1]] + p[tri[ind, 2]]) plt.text( 1.2 * x2, 0.8 * y2, 'max p = %g @ (%+5.2f,%+5.2f)' % (pmax, pmax_cell[0], pmax_cell[1])) plt.text( 1.2 * x2, 0.6 * y2, 'min p = %g @ (%+5.2f,%+5.2f)' % (pmin, pmin_cell[0], pmin_cell[1])) plt.text( 1.2 * x2, 0.4 * y2, 'max d = %g @ (%+5.2f,%+5.2f)' % (dmax, dmax_cell[0], dmax_cell[1])) plt.text( 1.2 * x2, 0.2 * y2, 'min d = %g @ (%+5.2f,%+5.2f)' % (dmin, dmin_cell[0], dmin_cell[1])) plt.text(1.2 * x2, 0.0, 'simulation time t = %6.4f' % (t)) plt.show() else: # normal detailed separate plots # ------------------------------ # pressure # ------------------------------ # method 1: almost continuous contour plot plt.figure() plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ppp, 256) plt.colorbar() if atm_units == 0: plt.title('PRESSURE') else: plt.title('PRESSURE [ATM]') plt.xlabel('x') plt.ylabel('y') plt.show() # method 2: 30 contours plt.figure() plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ppp, 30) plt.colorbar() if atm_units == 0: plt.title('PRESSURE') else: plt.title('PRESSURE [ATM]') plt.xlabel('x') plt.ylabel('y') plt.show() # method 3: just the contour lines plt.figure() plt.gca().set_aspect('equal') plt.tricontour(p[:, 0], p[:, 1], tri, ppp, 30) plt.colorbar() if atm_units == 0: plt.title('PRESSURE') else: plt.title('PRESSURE [ATM]') plt.xlabel('x') plt.ylabel('y') plt.show() # -------------------- # density # -------------------- # method 1: almost continuous contour plot plt.figure() plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ddd, 256) plt.colorbar() plt.title('DENSITY') plt.xlabel('x') plt.ylabel('y') plt.show() # method 2: 30 contours plt.figure() plt.gca().set_aspect('equal') plt.tricontourf(p[:, 0], p[:, 1], tri, ddd, 30) plt.colorbar() plt.title('DENSITY') plt.xlabel('x') plt.ylabel('y') plt.show() # method 3: just the contour lines plt.figure() plt.gca().set_aspect('equal') plt.tricontour(p[:, 0], p[:, 1], tri, ddd, 30) plt.colorbar() plt.title('DENSITY') plt.xlabel('x') plt.ylabel('y') # -------------------------------- # u, v # -------------------------------- xx = cell[:, 0] yy = cell[:, 1] uu_tri = uu[0:len(tri)] vv_tri = vv[0:len(tri)] # generate 100 points for (u,v) plot if len(xx) * len(yy) <= 100 or len(xx) < 10 or len(yy) < 10: plt.figure() plt.quiver(xx, yy, uu_tri, vv_tri) plt.title('VELOCITY FIELD') plt.axis('equal') plt.xlabel('pixel # in x') plt.ylabel('pixel # in y') plt.show() else: mxx = np.zeros(100) myy = np.zeros(100) muu = np.zeros(100) mvv = np.zeros(100) ddx = 0.1 * (x2 - x1) ddy = 0.1 * (y2 - y1) n = 0 for i in range(10): for j in range(10): ind = (xx > x1 + ddx * i) & ( xx <= x1 + ddx * (i + 1)) & (yy > y1 + ddy * j) & (yy <= y1 + ddy * (j + 1)) if np.any(ind == True): mxx[n] = np.mean(xx[np.where(ind == True)]) myy[n] = np.mean(yy[np.where(ind == True)]) muu[n] = np.mean(uu_tri[np.where(ind == True)]) mvv[n] = np.mean(vv_tri[np.where(ind == True)]) else: mxx[n] = x1 + 0.5 * ddx * (2 * i + 1) myy[n] = y1 + 0.5 * ddy * (2 * j + 1) muu[n] = 0.0 mvv[n] = 0.0 n += 1 plt.figure() plt.quiver(mxx, myy, muu, mvv) plt.title('VELOCITY FIELD') plt.axis('equal') plt.xlabel('pixel # in x') plt.ylabel('pixel # in y') plt.show() return 0
w = np.sqrt(eigenvalues[posmin]) period = 2.*np.pi/w wavelength = np.sqrt(C2)*period eta = eigenvectors[:, posmin] print 'wavelength = ', wavelength for i in np.arange(ID.shape[0]): if ID[i] != 0: Modo1[i, 0] = eta[ID[i]-1,0] else: Modo1[i, 0] = eta_g x = xyz[:, 0] y = xyz[:, 1] triangles = IEN-1 plt.tricontourf(x, y, triangles, Modo1[:, 0]) Tri3.Plot2DQuadGeometry(xyz,IEN,nodeId=True,elemId=False) plt.colorbar() plt.show() ######## Narrow Bay ####### CBdirichlet = np.arange(ndx+1, (ndx+1)*(ndy+2), ndy+1) ID = np.zeros(xyz.shape[0], dtype=int) aux1 = 1 for i in np.arange(ID.shape[0]): aux2 = 1 for j in np.arange(CBdirichlet.shape[0]): if i+1 == CBdirichlet[j]: aux2 = aux1*0 else:
def createContour(self, data, value=None, col=None): """ calculation of a contour deom value : value[0] : min [1] : max, [2] nb contours, [3] decimals, [4] : 'lin', log' or 'fix', if [4]:fix, then [5] is the series of contours""" X, Y, Z = data #print 'visu controu',value,col self.cnv.collections = self.cnv.collections[:3] self.cnv.artists = [] V = 11 Zmin = amin(amin(Z)) Zmax = amax(amax(Z * (Z < 1e5))) if Zmax == Zmin: # test min=max -> pas de contour onMessage(self.gui, ' values all equal to ' + str(Zmin)) return if value == None or len(value) < 4: value = [Zmin, Zmax, (Zmax - Zmin) / 10., 2, 'auto', []] # adapt the number and values of the contours val2 = [float(a) for a in value[:3]] if value[4] == 'log': # cas echelle log n = int((log10(val2[1]) - log10(max(val2[0], 1e-4))) / val2[2]) + 1 V = logspace(log10(max(val2[0], 1e-4)), log10(val2[1]), n) elif (value[4] == 'fix') and (value[5] != None): # fixes par l'utilisateur V = value[5] * 1 V.append(V[-1] * 2.) n = len(V) elif value[4] == 'lin': # cas echelle lineaire n = int((val2[1] - val2[0]) / val2[2]) + 1 V = linspace(val2[0], val2[1], n) else: # cas automatique n = 11 V = linspace(Zmin, Zmax, n) # ONE DIMENSIONAL if self.mesh == False: r, c = shape(X) if r == 1: X = concatenate([X, X]) Y = concatenate([Y - Y * .45, Y + Y * .45]) Z = concatenate([Z, Z]) Z2 = ma.masked_where(Z.copy() > 1e5, Z.copy()) #print value,n,V # definir les couleurs des contours if col == None or type(col) < type( 5): # or (col==[(0,0,0),(0,0,0),(0,0,0),10]): cmap = mpl.cm.jet col = [(0, 0, 255), (0, 255, 0), (255, 0, 0), 10] else: r, g, b = [], [], [] lim=((0.,1.,0.,0.),(.1,1.,0.,0.),(.25,.8,0.,0.),(.35,0.,.8,0.),(.45,0.,1.,0.),\ (.55,0.,1.,0.),(.65,0.,.8,0.),(.75,0.,0.,.8),(.9,0.,0.,1.),(1.,0.,0.,1.)) for i in range(len(lim)): c1 = lim[i][1] * col[0][0] / 255. + lim[i][2] * col[1][ 0] / 255. + lim[i][3] * col[2][0] / 255. r.append((lim[i][0], c1, c1)) c2 = lim[i][1] * col[0][1] / 255. + lim[i][2] * col[1][ 1] / 255. + lim[i][3] * col[2][1] / 255. g.append((lim[i][0], c2, c2)) c3 = lim[i][1] * col[0][2] / 255. + lim[i][2] * col[1][ 2] / 255. + lim[i][3] * col[2][2] / 255. b.append((lim[i][0], c3, c3)) cdict = {'red': r, 'green': g, 'blue': b} cmap = mpl.colors.LinearSegmentedColormap('my_colormap', cdict, 256) if self.mesh == False: cf = pl.contourf(pl.array(X), pl.array(Y), Z2, V, cmap=cmap) c = pl.contour(pl.array(X), pl.array(Y), Z2, V, cmap=cmap) else: cf = pl.tricontourf(self.Triangles, Z2, V, cmap=cmap) c = pl.tricontour(self.Triangles, Z2, V, cmap=cmap) #print col[3] for c0 in cf.collections: c0.set_alpha(int(col[3]) / 100.) #print cl if value == None: fmt = '%1.3f' else: fmt = '%1.' + str(value[3]) + 'f' cl = pl.clabel(c, color='black', fontsize=9, fmt=fmt) self.Contour = c self.ContourF = cf self.ContourLabel = cl self.Contour.data = data self.redraw()