# use path_effects my_path_effects = [patheffects.withStroke(linewidth=3, foreground="w")] pywcsgrid2.Axes.default_path_effects = my_path_effects if 1: f = pyfits.open("radio_21cm.fits") d, h = f[0].data, f[0].header plt.figure(1, [4.5,4.]) ax = pywcsgrid2.axes([0.15, 0.15, 0.8, 0.8], header=h) ax.imshow(d, origin="low", cmap=plt.cm.gist_heat_r, interpolation="nearest") ax.set_xlim(20, 52) ax.set_ylim(20, 52) # Figure title t = ax.add_inner_title("Figure 1", loc=2, path_effects=False) # compass ax.add_compass(loc=1) # Beam size # (major, minor) = 3, 4 in pixel, angle=20
# Now construct a header that is in the equatorial frame (North up, east to the left) h_equatorial = h_original.copy() h_equatorial.update( cd1_1=-np.hypot(h_original["CD1_1"], h_original["CD1_2"]), cd1_2=0.0, cd2_1=0.0, cd2_2=np.hypot(h_original["CD2_1"], h_original["CD2_2"]), orientat=0.0) if not grayscale_drawn: # Define the axis limits if this is the first frame with a valid WCS. This # first frame defines the axes for all subsequent drawing. ax = pywcsgrid2.axes(wcs=h_equatorial, aspect=1) ax.set_xlim(-nx/8, 9*nx/8) ax.set_ylim(-ny/4, 5*ny/4) ax.locator_params(axis="x", nbins=5) ax.locator_params(axis="y", nbins=5) target = h_original["target"] ax.set_title(this_image) # Mark the target as a crosshair in red result = subprocess.Popen(["coords","-d",target], stdout=subprocess.PIPE) (sout,serr) = result.communicate() ra_target,dec_target = string.rstrip(sout).split( ); ra_target = float(ra_target) dec_target = float(dec_target) cd=math.cos(dec_target*3.14159/180.) ax["fk5"].plot([ra_target+6*arcmin, ra_target+10*arcmin], [dec_target, dec_target], "r")
def _make_axis(self): if self.fig is None: self.fig = plt.figure() if self.cmap is None: self.cmap = plt.cm.gray_r if self.twin and not self.right: if self.gs: gs = self.gs else: gs = 121 self.ax = pywcsgrid2.subplot(gs, wcs=self.h1, aspect=self.aspect,adjustable='box',) elif self.twin and self.right: if self.gs: gs = self.gs else: gs = 122 self.ax = pywcsgrid2.subplot(gs, wcs=self.h1, aspect=self.aspect,adjustable='box') elif self.gs: if 'PACS' in self.filename: self.ax = plt.subplot(self.gs,projection=self.w0) else: self.ax = pywcsgrid2.subplot(self.gs, wcs=self.h1, aspect=self.aspect,adjustable='box') #if self.h0['CTYPE1'] == 'GLON-CAR': # self.ax.set_display_coord_system('fk5') else: self.ax = pywcsgrid2.axes(wcs=self.h1, aspect=self.aspect) #aspect=1 self.ax.set_xlim(-self.nx/4, 5*self.nx/4) self.ax.set_ylim(-self.ny/4, 5*self.ny/4) if self.interp is None: if 'PACS' in self.filename: self.im = self.ax.imshow(self.data,origin='lower',vmin=self.vmin,vmax=self.vmax,cmap=self.cmap,norm=self.norm) else: self.im = self.ax[self.h0].imshow_affine(self.data,origin='lower',vmin=self.vmin,vmax=self.vmax,cmap=self.cmap,norm=self.norm) else: self.im = self.ax[self.h0].imshow(self.data,origin='lower',vmin=self.vmin,vmax=self.vmax,cmap=self.cmap,interpolation=self.interp,norm=self.norm) if self.xlim: self.ax.set_xlim(self.xlim) if self.ylim: self.ax.set_ylim(self.ylim) self.ax.grid(color='black', alpha=0.2, linestyle='dashed') if self.right: self.ax.axis['left'].major_ticklabels.set_visible(False) #self.ax.axis['right'].major_ticklabels.set_visible(True) self.ax.axis['right'].label.set_text(r"$\delta_{2000}$") plt.setp(self.ax.axis['right'].label,fontsize=14) self.ax.axis['left'].label.set_text('') else: try: plt.setp(self.ax.axis['left'].label,fontsize=14) except TypeError: plt.setp(self.ax.yaxis.label,fontsize=14) try: plt.setp(self.ax.axis['bottom'].label,fontsize=14) except TypeError: plt.setp(self.ax.xaxis.label,fontsize=14) return self.ax
[ra0, dec0], = w2.wcs_pix2world([[i0, j0]], 1) h2.update(crpix1=i0, crpix2=j0, crval1=ra0, crval2=dec0) # Now construct a header that is in the equatorial frame (North up, east to the left) h1 = h2.copy() h1.update( cd1_1=-np.hypot(h2["CD1_1"], h2["CD1_2"]), cd1_2=0.0, cd2_1=0.0, cd2_2=np.hypot(h2["CD2_1"], h2["CD2_2"]), orientat=0.0, ) # Finally plot the image in the new frame arcsec = 1.0 / 3600 ax = pywcsgrid2.axes(wcs=h1, aspect=1) # ax.set_xlim(-nx/4, 5*nx/4) # ax.set_ylim(-ny/4, 5*ny/4) ax.set_xlim(-nx / 8, 9 * nx / 8) ax.set_ylim(-ny / 4, 5 * ny / 4) # Mark a line in red # ax["fk5"].plot([ra0, ra0], [dec0 + 10*arcsec, dec0 + 15*arcsec], "r") ax.locator_params(axis="x", nbins=5) ax.locator_params(axis="y", nbins=5) ax[h2].imshow_affine(processed_image, origin="lower", cmap=plt.cm.gray_r) ax.grid() # ax.figure.savefig("/var/tmp/nice_image.pdf", dpi=150) ax.figure.savefig("/var/tmp/nice_image.png") # Email results if requested if args.mail:
def plot_h2_sd(plot_dict, contour_image=None, limits=None, filename=None, show=True, vlimits=None, av_vlimits=None, cloud_names=('california', 'perseus', 'taurus'),): # Import external modules import matplotlib.pyplot as plt import matplotlib import numpy as np import pyfits as fits import matplotlib.pyplot as plt import myplotting as myplt import pywcsgrid2 import pywcs from pylab import cm # colormaps from matplotlib.patches import Polygon from mpl_toolkits.axes_grid1.axes_grid import AxesGrid # Set up plot aesthetics plt.clf(); plt.close() cmap = plt.cm.copper norm = matplotlib.colors.LogNorm() # Create figure instance fig = plt.figure(figsize=(3.6, 9)) if 1: nrows = 3 ncols = 1 nrows_ncols=(3, 1) ngrids=3 figsize = (3.6, 8) fig = pywcsgrid2.plt.figure(figsize=figsize) else: nrows_ncols=(1,1) ngrids=1 axes = AxesGrid(fig, (1,1,1), nrows_ncols=nrows_ncols, ngrids=ngrids, cbar_mode="each", cbar_location='right', cbar_pad="2%", cbar_size='3%', axes_pad=0.1, axes_class=(wcs.Axes, dict(header=plot_dict['header'])), aspect=True, label_mode='L', share_all=True) colorbar_axes = [0.05, 0.97, 0.95, 0.02,] map_axes = np.array([0.05, 0.69, 0.95, 0.25]) for i, cloud_name in enumerate(cloud_names): header = plot_dict[cloud_name]['header'] limits = plot_dict[cloud_name]['limits'] contour_image = plot_dict[cloud_name]['contour_image'] contours = plot_dict[cloud_name]['contours'] #ax = pywcsgrid2.subplot(311+i, header=header) ax = pywcsgrid2.axes(map_axes, header=header) #ax = fig.add_subplot(311 + i, header=) h2_sd = plot_dict[cloud_name]['h2_sd'] # create axes # show the image im = ax.imshow(h2_sd, interpolation='nearest', origin='lower', cmap=cmap, vmin=vlimits[0], vmax=vlimits[1], #norm=matplotlib.colors.LogNorm() ) # Asthetics ax.set_display_coord_system("fk5") ax.set_ticklabel_type("hms", "dms") if i == 2: ax.set_xlabel('Right Ascension [J2000]',) else: ax.set_xlabel('') ax.set_xlabel('Right Ascension [J2000]',) ax.set_ylabel('Declination [J2000]',) ax.locator_params(nbins=6) #cb_axes.colorbar(im) #cb_axes.axis["right"].toggle(ticklabels=False) # colorbar #cb = ax.cax.colorbar(im) cmap.set_bad(color='w') # plot limits if limits is not None: limits_pix = myplt.convert_wcs_limits(limits, header, frame='fk5') ax.set_xlim(limits_pix[0],limits_pix[1]) ax.set_ylim(limits_pix[2],limits_pix[3]) # Plot Av contours if contour_image is not None: ax.contour(contour_image, levels=contours, colors='w') # Write label to colorbar #cb.set_label_text(r'$\Sigma_{\rm H_2} [M_\odot$\,pc$^{-2}$]',) if 0: if regions is not None: for region in props['region_name_pos']: vertices = np.copy(regions[region]['poly_verts']['pixel']) rect = ax.add_patch(Polygon( vertices[:, ::-1], facecolor='none', edgecolor='w')) ax.annotate(cloud_name.capitalize(), xytext=(0.96, 0.94), xy=(0.96, 0.94), textcoords='axes fraction', xycoords='axes fraction', size=10, color='k', bbox=dict(boxstyle='square', facecolor='w', alpha=1), horizontalalignment='right', verticalalignment='top', ) # create new box for plot map_axes[1] -= 0.3 #map_axes[3] -= 0.3 ax = plt.axes(colorbar_axes, ) cb = plt.colorbar(im, cax=ax, orientation='horizontal', ) cb.ax.set_xlabel(r'$\Sigma_{\rm H_2} [{\rm M_\odot}$\,pc$^{-2}$]',) cb.ax.xaxis.set_label_position('top') if filename is not None: plt.savefig(filename, bbox_inches='tight') if show: fig.show()
nx, ny = h2["naxis1"], h2["naxis2"] i0, j0 = (float(nx) + 1.0) / 2, (float(ny) + 1.0) / 2 [ra0, dec0], = w2.wcs_pix2world([[i0, j0]], 1) h2.update(crpix1=i0, crpix2=j0, crval1=ra0, crval2=dec0) # Now construct a header that is in the equatorial frame (North up, east to the left) h1 = h2.copy() h1.update(cd1_1=-np.hypot(h2["CD1_1"], h2["CD1_2"]), cd1_2=0.0, cd2_1=0.0, cd2_2=np.hypot(h2["CD2_1"], h2["CD2_2"]), orientat=0.0) # Finally plot the image in the new frame arcsec = 1. / 3600 ax = pywcsgrid2.axes(wcs=h1, aspect=1) #ax.set_xlim(-nx/4, 5*nx/4) #ax.set_ylim(-ny/4, 5*ny/4) ax.set_xlim(-nx / 8, 9 * nx / 8) ax.set_ylim(-ny / 4, 5 * ny / 4) # Mark a line in red # ax["fk5"].plot([ra0, ra0], [dec0 + 10*arcsec, dec0 + 15*arcsec], "r") ax.locator_params(axis="x", nbins=5) ax.locator_params(axis="y", nbins=5) ax[h2].imshow_affine(processed_image, origin='lower', cmap=plt.cm.gray_r) ax.grid() #ax.figure.savefig("/var/tmp/nice_image.pdf", dpi=150) ax.figure.savefig("/var/tmp/nice_image.png") # Email results if requested if args.mail:
def plot_h2_sd( plot_dict, contour_image=None, limits=None, filename=None, show=True, vlimits=None, av_vlimits=None, cloud_names=('california', 'perseus', 'taurus'), ): # Import external modules import matplotlib.pyplot as plt import matplotlib import numpy as np import pyfits as fits import matplotlib.pyplot as plt import myplotting as myplt import pywcsgrid2 import pywcs from pylab import cm # colormaps from matplotlib.patches import Polygon from mpl_toolkits.axes_grid1.axes_grid import AxesGrid # Set up plot aesthetics plt.clf() plt.close() cmap = plt.cm.copper norm = matplotlib.colors.LogNorm() # Create figure instance fig = plt.figure(figsize=(3.6, 9)) if 1: nrows = 3 ncols = 1 nrows_ncols = (3, 1) ngrids = 3 figsize = (3.6, 8) fig = pywcsgrid2.plt.figure(figsize=figsize) else: nrows_ncols = (1, 1) ngrids = 1 axes = AxesGrid(fig, (1, 1, 1), nrows_ncols=nrows_ncols, ngrids=ngrids, cbar_mode="each", cbar_location='right', cbar_pad="2%", cbar_size='3%', axes_pad=0.1, axes_class=(wcs.Axes, dict(header=plot_dict['header'])), aspect=True, label_mode='L', share_all=True) colorbar_axes = [ 0.05, 0.97, 0.95, 0.02, ] map_axes = np.array([0.05, 0.69, 0.95, 0.25]) for i, cloud_name in enumerate(cloud_names): header = plot_dict[cloud_name]['header'] limits = plot_dict[cloud_name]['limits'] contour_image = plot_dict[cloud_name]['contour_image'] contours = plot_dict[cloud_name]['contours'] #ax = pywcsgrid2.subplot(311+i, header=header) ax = pywcsgrid2.axes(map_axes, header=header) #ax = fig.add_subplot(311 + i, header=) h2_sd = plot_dict[cloud_name]['h2_sd'] # create axes # show the image im = ax.imshow( h2_sd, interpolation='nearest', origin='lower', cmap=cmap, vmin=vlimits[0], vmax=vlimits[1], #norm=matplotlib.colors.LogNorm() ) # Asthetics ax.set_display_coord_system("fk5") ax.set_ticklabel_type("hms", "dms") if i == 2: ax.set_xlabel('Right Ascension [J2000]', ) else: ax.set_xlabel('') ax.set_xlabel('Right Ascension [J2000]', ) ax.set_ylabel('Declination [J2000]', ) ax.locator_params(nbins=6) #cb_axes.colorbar(im) #cb_axes.axis["right"].toggle(ticklabels=False) # colorbar #cb = ax.cax.colorbar(im) cmap.set_bad(color='w') # plot limits if limits is not None: limits_pix = myplt.convert_wcs_limits(limits, header, frame='fk5') ax.set_xlim(limits_pix[0], limits_pix[1]) ax.set_ylim(limits_pix[2], limits_pix[3]) # Plot Av contours if contour_image is not None: ax.contour(contour_image, levels=contours, colors='w') # Write label to colorbar #cb.set_label_text(r'$\Sigma_{\rm H_2} [M_\odot$\,pc$^{-2}$]',) if 0: if regions is not None: for region in props['region_name_pos']: vertices = np.copy(regions[region]['poly_verts']['pixel']) rect = ax.add_patch( Polygon(vertices[:, ::-1], facecolor='none', edgecolor='w')) ax.annotate( cloud_name.capitalize(), xytext=(0.96, 0.94), xy=(0.96, 0.94), textcoords='axes fraction', xycoords='axes fraction', size=10, color='k', bbox=dict(boxstyle='square', facecolor='w', alpha=1), horizontalalignment='right', verticalalignment='top', ) # create new box for plot map_axes[1] -= 0.3 #map_axes[3] -= 0.3 ax = plt.axes(colorbar_axes, ) cb = plt.colorbar( im, cax=ax, orientation='horizontal', ) cb.ax.set_xlabel(r'$\Sigma_{\rm H_2} [{\rm M_\odot}$\,pc$^{-2}$]', ) cb.ax.xaxis.set_label_position('top') if filename is not None: plt.savefig(filename, bbox_inches='tight') if show: fig.show()
from astropy.io import fits as pyfits import matplotlib.pyplot as plt f = pyfits.open("data/lmc.fits") d, h = f[0].data, f[0].header plt.figure(1, figsize=[5, 5]) import pywcsgrid2 ax = pywcsgrid2.axes([0.2, 0.15, 0.7, 0.8], header=h) im1 = ax.imshow(d, origin="low", vmin=0, vmax=2000, cmap=plt.cm.gray_r) # viewlimits in image coordinate ax.set_xlim(6, 81) ax.set_ylim(23, 98) # draw grids ax.grid() # make y ticklabels in absolute degree. ax.set_ticklabel2_type("absdeg", locs=[-65, -67.5, -70, -72.5]) ax.set_default_label(None, "absdeg") # update the label using the default values # change grid density of the x axis ax.locator_params(axis="x", nbins=6) plt.show()