def test_streamlines(driving): import matplotlib.pyplot as plt from streamplot import streamplot Fx, Fy = driving.field x = np.linspace(0.0, 1.0, Fx.shape[0]) y = np.linspace(0.0, 1.0, Fy.shape[1]) F = (Fx**2 + Fy**2)**0.5 streamplot(x, y, Fx, Fy, density=1, color=F, linewidth=5*F/F.max()) plt.show()
def plot_stream(x, y, u): pl.figure() pl.gca().set_aspect('equal') M = pl.sqrt(u['X'][:, :]**2 + u['Y'][:, :]**2) lw = 5 * M / M.max() streamplot(x, y, u['X'], u['Y'], color=M, linewidth=lw) pl.xlabel('x') pl.ylabel('y') #pl.colorbar() pl.show()
def streamlines(u1,u2,d,x0=None,y0=None,nmax=600,density=1,fig=None,color='b',linewidth=1,arrowsize=1): '''plots streamlines from a vector field. Use density=[densx,densy] to control how close streamlines are allowed to get.''' if fig==None: ax=plt.gca() else: ax=fig.ax xrange=[ax.get_xlim()[0],ax.get_xlim()[1]] yrange=[ax.get_ylim()[0],ax.get_ylim()[1]] if nmax == 600 and fig != None: nmax = fig.dpi * max([fig.fig_w,fig.fig_h]) # Aspect ratio: r=(xrange[1]-xrange[0])/(yrange[1]-yrange[0]) if r<1: ny=nmax nx=int(r*nmax) else: nx=nmax ny=int(nmax/r) nregrid = [nx,ny] print nregrid CC=d.getCenterPoints() tmp0=np.complex(0,nregrid[0]) tmp1=np.complex(0,nregrid[1]) x=np.linspace(xrange[0],xrange[1],nregrid[0]) y=np.linspace(yrange[0],yrange[1],nregrid[1]) grid_x, grid_y = np.mgrid[xrange[0]:xrange[1]:tmp0, yrange[0]:yrange[1]:tmp1] u = griddata(CC, u1, (grid_x, grid_y), method='linear') v = griddata(CC, u2, (grid_x, grid_y), method='linear') uisnan=np.isnan(u) visnan=np.isnan(v) un = np.empty(np.shape(u)) vn = np.empty(np.shape(v)) un[uisnan] = griddata(CC, u1, (grid_x[uisnan], grid_y[uisnan]), method='nearest') vn[visnan] = griddata(CC, u2, (grid_x[visnan], grid_y[visnan]), method='nearest') u[uisnan]=un[uisnan] v[visnan]=vn[visnan] if (x0 != None and y0!= None): for myx in zip(x0,y0): streamplot(x, y, u.transpose(), v.transpose(), x_0=myx[0], y_0=myx[1], density=density, linewidth=linewidth, INTEGRATOR='RK4', color=color, arrowsize=arrowsize) else: streamplot(x, y, u.transpose(), v.transpose(), density=density, linewidth=linewidth, INTEGRATOR='RK4', color=color, arrowsize=arrowsize)
def stream_plot(self, fname=None, k=0): if not isinstance(self.field, VectorField): raise TypeError('Not a vector field') fig = plt.figure() x = self.grid.get_axis(0).get_points() y = self.grid.get_axis(1).get_points() # x, y = np.meshgrid(x, y) u, v, w = self.field.get_field(k) # streamplot(x, y, u, v, density=1, INTEGRATOR='RK4', color='b') streamplot(x, y, v, u, density=1, INTEGRATOR='RK4', color=u) if fname is not None: plt.savefig(fname) else: plt.show()
def streamplot(self, density=2): f = plt.figure() if not MATPLOTLIB_STREAMPLOT: streamplot( self._x, self._x, self.ui, self.vi, color=self.vmag,density=(density, density), INTEGRATOR='RK4', linewidth=5*self.vmag/self.vmag.max() ) else: streamplot( self._x, self._x, self.ui, self.vi, density=(density, density), linewidth=5*self.vmag/self.vmag.max(), color=self.vmag) plt.show()
def streams(ax, x, y, u, v, *args, **kwargs): """Make a streamplot on a non-uniform grid. It works by interpolating onto a uniform grid before calling the usual streamplot function. Arguments beyond the basic ones will be passed along to streamplot.""" if x.ndim == 1 and y.ndim == 1: X, Y = np.meshgrid(x,y) else: assert x.ndim == 2 assert y.ndim == 2 x_unif = np.linspace(x.min(), x.max(), len(x)) y_unif = np.linspace(y.min(), y.max(), len(y)) X_unif, Y_unif = np.meshgrid(x_unif,y_unif) px = X.flatten() py = Y.flatten() pu = u.flatten() pv = v.flatten() gu = griddata(zip(px,py), pu, (X_unif,Y_unif)) gv = griddata(zip(px,py), pv, (X_unif,Y_unif)) return streamplot(ax, x_unif, y_unif, gu, gv, *args, **kwargs)
def show_frame(fname, show=True): h5f = h5py.File(fname) vx = h5f['prim'][...,2] vy = h5f['prim'][...,3] Bx = h5f['prim'][...,5] By = h5f['prim'][...,6] X = np.linspace(-1, 1, Bx.shape[0]) Y = np.linspace(-1, 1, Bx.shape[1]) kwargs = dict(arrowsize=1, density=1, linewidth=1) streamplot.streamplot(X, Y, vx.T, vy.T, color='b', **kwargs) streamplot.streamplot(X, Y, Bx.T, By.T, color='r', **kwargs) plt.axis('equal') plt.xlim(-1,1) plt.ylim(-1,1) if show: plt.show() else: plt.savefig(fname.replace('.h5', '.png')) plt.clf()
def streamlines(u1, u2, d, x0=None, y0=None, nmax=600, density=1, fig=None, color='b', linewidth=1, arrowsize=1): '''plots streamlines from a vector field. Use density=[densx,densy] to control how close streamlines are allowed to get.''' if fig == None: ax = plt.gca() else: ax = fig.ax xrange = [ax.get_xlim()[0], ax.get_xlim()[1]] yrange = [ax.get_ylim()[0], ax.get_ylim()[1]] if nmax == 600 and fig != None: nmax = fig.dpi * max([fig.fig_w, fig.fig_h]) # Aspect ratio: r = (xrange[1] - xrange[0]) / (yrange[1] - yrange[0]) if r < 1: ny = nmax nx = int(r * nmax) else: nx = nmax ny = int(nmax / r) nregrid = [nx, ny] print nregrid CC = d.getCenterPoints() tmp0 = np.complex(0, nregrid[0]) tmp1 = np.complex(0, nregrid[1]) x = np.linspace(xrange[0], xrange[1], nregrid[0]) y = np.linspace(yrange[0], yrange[1], nregrid[1]) grid_x, grid_y = np.mgrid[xrange[0]:xrange[1]:tmp0, yrange[0]:yrange[1]:tmp1] u = griddata(CC, u1, (grid_x, grid_y), method='linear') v = griddata(CC, u2, (grid_x, grid_y), method='linear') uisnan = np.isnan(u) visnan = np.isnan(v) un = np.empty(np.shape(u)) vn = np.empty(np.shape(v)) un[uisnan] = griddata(CC, u1, (grid_x[uisnan], grid_y[uisnan]), method='nearest') vn[visnan] = griddata(CC, u2, (grid_x[visnan], grid_y[visnan]), method='nearest') u[uisnan] = un[uisnan] v[visnan] = vn[visnan] if (x0 != None and y0 != None): for myx in zip(x0, y0): streamplot(x, y, u.transpose(), v.transpose(), x_0=myx[0], y_0=myx[1], density=density, linewidth=linewidth, INTEGRATOR='RK4', color=color, arrowsize=arrowsize) else: streamplot(x, y, u.transpose(), v.transpose(), density=density, linewidth=linewidth, INTEGRATOR='RK4', color=color, arrowsize=arrowsize)