def plot( self, cut=0.5, levels=12, cmap="RdYlBu_r", png=False, step=1, normed=False, dpi=80, bgcolor=None, deminc=True ): """ Plotting function (it can also write the png files) :param levels: number of contour levels :type levels: int :param cmap: name of the colormap :type cmap: str :param cut: adjust the contour extrema to max(abs(data))*cut :type cut: float :param png: save the movie as a series of png files when set to True :type png: bool :param dpi: dot per inch when saving PNGs :type dpi: int :param bgcolor: background color of the figure :type bgcolor: str :param normed: the colormap is rescaled every timestep when set to True, otherwise it is calculated from the global extrema :type normed: bool :param step: the stepping between two timesteps :type step: int :param deminc: a logical to indicate if one wants do get rid of the possible azimuthal symmetry :type deminc: bool """ if png: plt.ioff() if not os.path.exists("movie"): os.mkdir("movie") else: plt.ion() if not normed: vmin = -max(abs(self.data.max()), abs(self.data.min())) vmin = cut * vmin vmax = -vmin # vmin, vmax = self.data.min(), self.data.max() cs = np.linspace(vmin, vmax, levels) if self.surftype == "phi_constant": # if self.movtype in [1, 7]: # th = np.linspace(0., 2.*np.pi, 2*self.n_theta_max) # else: th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) rr, tth = np.meshgrid(self.radius, th) xx = rr * np.cos(tth) yy = rr * np.sin(tth) xxout = rr.max() * np.cos(th) yyout = rr.max() * np.sin(th) xxin = rr.min() * np.cos(th) yyin = rr.min() * np.sin(th) fig = plt.figure(figsize=(4, 8)) elif self.surftype == "r_constant": th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) if deminc: phi = np.linspace(-np.pi, np.pi, self.n_phi_tot * self.minc + 1) xxout, yyout = hammer2cart(th, -np.pi) xxin, yyin = hammer2cart(th, np.pi) else: phi = np.linspace(-np.pi / self.minc, np.pi / self.minc, self.n_phi_tot) xxout, yyout = hammer2cart(th, -np.pi / self.minc) xxin, yyin = hammer2cart(th, np.pi / self.minc) ttheta, pphi = np.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) fig = plt.figure(figsize=(8, 4)) elif self.surftype == "theta_constant": if deminc: phi = np.linspace(0.0, 2.0 * np.pi, self.n_phi_tot * self.minc + 1) else: phi = np.linspace(0.0, 2.0 * np.pi / self.minc, self.n_phi_tot) rr, pphi = np.meshgrid(self.radius, phi) xx = rr * np.cos(pphi) yy = rr * np.sin(pphi) xxout = rr.max() * np.cos(pphi) yyout = rr.max() * np.sin(pphi) xxin = rr.min() * np.cos(pphi) yyin = rr.min() * np.sin(pphi) fig = plt.figure(figsize=(6, 6)) elif self.surftype == "3d volume": self.data = self.data[..., 0] th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) phi = np.linspace(-np.pi, np.pi, self.n_phi_tot) ttheta, pphi = np.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -np.pi) xxin, yyin = hammer2cart(th, np.pi) fig = plt.figure(figsize=(8, 4)) fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01) ax = fig.add_subplot(111, frameon=False) for k in range(self.nvar): if k == 0: if normed: vmin = -max(abs(self.data[k, ...].max()), abs(self.data[k, ...].min())) vmin = cut * vmin vmax = -vmin cs = np.linspace(vmin, vmax, levels) if self.surftype in ["r_constant", "theta_constant"]: if deminc: dat = symmetrize(self.data[k, ...], self.minc) else: dat = self.data[k, ...] im = ax.contourf(xx, yy, dat, cs, cmap=cmap, extend="both") else: im = ax.contourf(xx, yy, self.data[k, ...], cs, cmap=cmap, extend="both") ax.plot(xxout, yyout, "k-", lw=1.5) ax.plot(xxin, yyin, "k-", lw=1.5) ax.axis("off") man = plt.get_current_fig_manager() man.canvas.draw() if k != 0 and k % step == 0: if not png: print(k + self.var2 - self.nvar) plt.cla() if normed: vmin = -max(abs(self.data[k, ...].max()), abs(self.data[k, ...].min())) vmin = cut * vmin vmax = -vmin cs = np.linspace(vmin, vmax, levels) if self.surftype in ["r_constant", "theta_constant"]: if deminc: dat = symmetrize(self.data[k, ...], self.minc) else: dat = self.data[k, ...] im = ax.contourf(xx, yy, dat, cs, cmap=cmap, extend="both") else: im = ax.contourf(xx, yy, self.data[k, ...], cs, cmap=cmap, extend="both") ax.plot(xxout, yyout, "k-", lw=1.5) ax.plot(xxin, yyin, "k-", lw=1.5) ax.axis("off") man.canvas.draw() if png: filename = "movie/img%05d.png" % k print("write %s" % filename) # st = 'echo %i' % ivar + ' > movie/imgmax' if bgcolor is not None: fig.savefig(filename, facecolor=bgcolor, dpi=dpi) else: fig.savefig(filename, dpi=dpi)
def plot(self, cut=0.5, levels=12, cmap='RdYlBu_r', png=False, step=1, normed=False, dpi=80, bgcolor=None, deminc=True): """ Plotting function (it can also write the png files) :param levels: number of contour levels :type levels: int :param cmap: name of the colormap :type cmap: str :param cut: adjust the contour extrema to max(abs(data))*cut :type cut: float :param png: save the movie as a series of png files when set to True :type png: bool :param dpi: dot per inch when saving PNGs :type dpi: int :param bgcolor: background color of the figure :type bgcolor: str :param normed: the colormap is rescaled every timestep when set to True, otherwise it is calculated from the global extrema :type normed: bool :param step: the stepping between two timesteps :type step: int :param deminc: a logical to indicate if one wants do get rid of the possible azimuthal symmetry :type deminc: bool """ if png: P.ioff() if not os.path.exists('movie'): os.mkdir('movie') else: P.ion() if not normed: vmin = -max(abs(self.data.max()), abs(self.data.min())) vmin = cut * vmin vmax = -vmin #vmin, vmax = self.data.min(), self.data.max() cs = N.linspace(vmin, vmax, levels) if self.surftype == 'phi_constant': #if self.movtype in [1, 7]: #th = N.linspace(0., 2.*N.pi, 2*self.n_theta_max) #else: th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) rr, tth = N.meshgrid(self.radius, th) xx = rr * N.cos(tth) yy = rr * N.sin(tth) xxout = rr.max() * N.cos(th) yyout = rr.max() * N.sin(th) xxin = rr.min() * N.cos(th) yyin = rr.min() * N.sin(th) fig = P.figure(figsize=(4, 8)) elif self.surftype == 'r_constant': th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) if deminc: phi = N.linspace(-N.pi, N.pi, self.n_phi_tot * self.minc + 1) xxout, yyout = hammer2cart(th, -N.pi) xxin, yyin = hammer2cart(th, N.pi) else: phi = N.linspace(-N.pi / self.minc, N.pi / self.minc, self.n_phi_tot) xxout, yyout = hammer2cart(th, -N.pi / self.minc) xxin, yyin = hammer2cart(th, N.pi / self.minc) ttheta, pphi = N.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) fig = P.figure(figsize=(8, 4)) elif self.surftype == 'theta_constant': if deminc: phi = N.linspace(0., 2. * N.pi, self.n_phi_tot * self.minc + 1) else: phi = N.linspace(0., 2. * N.pi / self.minc, self.n_phi_tot) rr, pphi = N.meshgrid(self.radius, phi) xx = rr * N.cos(pphi) yy = rr * N.sin(pphi) xxout = rr.max() * N.cos(pphi) yyout = rr.max() * N.sin(pphi) xxin = rr.min() * N.cos(pphi) yyin = rr.min() * N.sin(pphi) fig = P.figure(figsize=(6, 6)) elif self.surftype == '3d volume': self.data = self.data[..., 0] th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) phi = N.linspace(-N.pi, N.pi, self.n_phi_tot) ttheta, pphi = N.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -N.pi) xxin, yyin = hammer2cart(th, N.pi) fig = P.figure(figsize=(8, 4)) fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01) ax = fig.add_subplot(111, frameon=False) for k in range(self.nvar): if k == 0: if normed: vmin = -max(abs(self.data[k, ...].max()), abs(self.data[k, ...].min())) vmin = cut * vmin vmax = -vmin cs = N.linspace(vmin, vmax, levels) if self.surftype in ['r_constant', 'theta_constant']: if deminc: dat = symmetrize(self.data[k, ...], self.minc) else: dat = self.data[k, ...] im = ax.contourf(xx, yy, dat, cs, cmap=cmap, extend='both') else: im = ax.contourf(xx, yy, self.data[k, ...], cs, cmap=cmap, extend='both') ax.plot(xxout, yyout, 'k-', lw=1.5) ax.plot(xxin, yyin, 'k-', lw=1.5) ax.axis('off') man = P.get_current_fig_manager() man.canvas.draw() if k != 0 and k % step == 0: if not png: print(k + self.var2 - self.nvar) P.cla() if normed: vmin = -max(abs(self.data[k, ...].max()), abs(self.data[k, ...].min())) vmin = cut * vmin vmax = -vmin cs = N.linspace(vmin, vmax, levels) if self.surftype in ['r_constant', 'theta_constant']: if deminc: dat = symmetrize(self.data[k, ...], self.minc) else: dat = self.data[k, ...] im = ax.contourf(xx, yy, dat, cs, cmap=cmap, extend='both') else: im = ax.contourf(xx, yy, self.data[k, ...], cs, cmap=cmap, extend='both') ax.plot(xxout, yyout, 'k-', lw=1.5) ax.plot(xxin, yyin, 'k-', lw=1.5) ax.axis('off') man.canvas.draw() if png: filename = 'movie/img%05d.png' % k print('write %s' % filename) #st = 'echo %i' % ivar + ' > movie/imgmax' if bgcolor is not None: fig.savefig(filename, facecolor=bgcolor, dpi=dpi) else: fig.savefig(filename, dpi=dpi)
def avgStd(self, std=False, cut=0.5, levels=12, cmap="RdYlBu_r"): """ Plot time-average or standard deviation :param std: the standard deviation is computed instead the average when std is True :type std: bool :param levels: number of contour levels :type levels: int :param cmap: name of the colormap :type cmap: str :param cut: adjust the contour extrema to max(abs(data))*cut :type cut: float """ if std: avg = self.data.std(axis=0) else: avg = self.data.mean(axis=0) vmin = -max(abs(avg.max()), abs(avg.min())) vmin = cut * vmin vmax = -vmin cs = np.linspace(vmin, vmax, levels) if self.surftype == "phi_constant": th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) rr, tth = np.meshgrid(self.radius, th) xx = rr * np.cos(tth) yy = rr * np.sin(tth) xxout = rr.max() * np.cos(th) yyout = rr.max() * np.sin(th) xxin = rr.min() * np.cos(th) yyin = rr.min() * np.sin(th) fig = plt.figure(figsize=(4, 8)) elif self.surftype == "r_constant": th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) phi = np.linspace(-np.pi, np.pi, self.n_phi_tot) ttheta, pphi = np.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -np.pi) xxin, yyin = hammer2cart(th, np.pi) fig = plt.figure(figsize=(8, 4)) elif self.surftype == "theta_constant": phi = np.linspace(0.0, 2.0 * np.pi, self.n_phi_tot) rr, pphi = np.meshgrid(self.radius, phi) xx = rr * np.cos(pphi) yy = rr * np.sin(pphi) xxout = rr.max() * np.cos(pphi) yyout = rr.max() * np.sin(pphi) xxin = rr.min() * np.cos(pphi) yyin = rr.min() * np.sin(pphi) fig = plt.figure(figsize=(6, 6)) elif self.surftype == "3d volume": self.data = self.data[..., 0] th = np.linspace(np.pi / 2.0, -np.pi / 2.0, self.n_theta_max) phi = np.linspace(-np.pi, np.pi, self.n_phi_tot) ttheta, pphi = np.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -np.pi) xxin, yyin = hammer2cart(th, np.pi) fig = plt.figure(figsize=(8, 4)) fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01) ax = fig.add_subplot(111, frameon=False) im = ax.contourf(xx, yy, avg, cs, cmap=cmap, extend="both") ax.plot(xxout, yyout, "k-", lw=1.5) ax.plot(xxin, yyin, "k-", lw=1.5) ax.axis("off")
def avgStd(self, std=False, cut=0.5, levels=12, cmap='RdYlBu_r'): """ Plot time-average or standard deviation :param std: the standard deviation is computed instead the average when std is True :type std: bool :param levels: number of contour levels :type levels: int :param cmap: name of the colormap :type cmap: str :param cut: adjust the contour extrema to max(abs(data))*cut :type cut: float """ if std: avg = self.data.std(axis=0) else: avg = self.data.mean(axis=0) vmin = -max(abs(avg.max()), abs(avg.min())) vmin = cut * vmin vmax = -vmin cs = N.linspace(vmin, vmax, levels) if self.surftype == 'phi_constant': th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) rr, tth = N.meshgrid(self.radius, th) xx = rr * N.cos(tth) yy = rr * N.sin(tth) xxout = rr.max() * N.cos(th) yyout = rr.max() * N.sin(th) xxin = rr.min() * N.cos(th) yyin = rr.min() * N.sin(th) fig = P.figure(figsize=(4, 8)) elif self.surftype == 'r_constant': th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) phi = N.linspace(-N.pi, N.pi, self.n_phi_tot) ttheta, pphi = N.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -N.pi) xxin, yyin = hammer2cart(th, N.pi) fig = P.figure(figsize=(8, 4)) elif self.surftype == 'theta_constant': phi = N.linspace(0., 2. * N.pi, self.n_phi_tot) rr, pphi = N.meshgrid(self.radius, phi) xx = rr * N.cos(pphi) yy = rr * N.sin(pphi) xxout = rr.max() * N.cos(pphi) yyout = rr.max() * N.sin(pphi) xxin = rr.min() * N.cos(pphi) yyin = rr.min() * N.sin(pphi) fig = P.figure(figsize=(6, 6)) elif self.surftype == '3d volume': self.data = self.data[..., 0] th = N.linspace(N.pi / 2., -N.pi / 2., self.n_theta_max) phi = N.linspace(-N.pi, N.pi, self.n_phi_tot) ttheta, pphi = N.meshgrid(th, phi) xx, yy = hammer2cart(ttheta, pphi) xxout, yyout = hammer2cart(th, -N.pi) xxin, yyin = hammer2cart(th, N.pi) fig = P.figure(figsize=(8, 4)) fig.subplots_adjust(top=0.99, right=0.99, bottom=0.01, left=0.01) ax = fig.add_subplot(111, frameon=False) im = ax.contourf(xx, yy, avg, cs, cmap=cmap, extend='both') ax.plot(xxout, yyout, 'k-', lw=1.5) ax.plot(xxin, yyin, 'k-', lw=1.5) ax.axis('off')