def test_update_clip_path_change_wcs(self, tmpdir): # When WCS is changed, a new frame is created, so we need to make sure # that the path is carried over to the new frame. fig = plt.figure() ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], aspect='equal') fig.add_axes(ax) ax.set_xlim(0., 2.) ax.set_ylim(0., 2.) # Force drawing, which freezes the clip path returned by WCSAxes fig.savefig(tmpdir.join('nothing').strpath) ax.reset_wcs() ax.imshow(np.zeros((12, 4))) ax.set_xlim(-0.5, 3.5) ax.set_ylim(-0.5, 11.5) ax.coords[0].set_auto_axislabel(False) ax.coords[1].set_auto_axislabel(False) return fig
def test_update_clip_path_rectangular(self, tmpdir): fig = plt.figure() ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], aspect='equal') fig.add_axes(ax) ax.set_xlim(0., 2.) ax.set_ylim(0., 2.) # Force drawing, which freezes the clip path returned by WCSAxes fig.savefig(tmpdir.join('nothing').strpath) ax.imshow(np.zeros((12, 4))) ax.set_xlim(-0.5, 3.5) ax.set_ylim(-0.5, 11.5) return fig
def test_update_clip_path_change_wcs(self, tmpdir): # When WCS is changed, a new frame is created, so we need to make sure # that the path is carried over to the new frame. fig = plt.figure() ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], aspect='equal') fig.add_axes(ax) ax.set_xlim(0., 2.) ax.set_ylim(0., 2.) # Force drawing, which freezes the clip path returned by WCSAxes fig.savefig(tmpdir.join('nothing').strpath) ax.reset_wcs() ax.imshow(np.zeros((12, 4))) ax.set_xlim(-0.5, 3.5) ax.set_ylim(-0.5, 11.5) return fig
def test_custom_frame(self): wcs = WCS(self.msx_header) fig = plt.figure(figsize=(4, 4)) ax = WCSAxes(fig, [0.15, 0.15, 0.7, 0.7], wcs=wcs, frame_class=HexagonalFrame) fig.add_axes(ax) ax.coords.grid(color='white') im = ax.imshow(np.ones((149, 149)), vmin=0., vmax=2., origin='lower', cmap=plt.cm.gist_heat) minpad = {} minpad['a'] = minpad['d'] = 1 minpad['b'] = minpad['c'] = minpad['e'] = minpad['f'] = 2.75 ax.coords['glon'].set_axislabel("Longitude", minpad=minpad) ax.coords['glon'].set_axislabel_position('ad') ax.coords['glat'].set_axislabel("Latitude", minpad=minpad) ax.coords['glat'].set_axislabel_position('bcef') ax.coords['glon'].set_ticklabel_position('ad') ax.coords['glat'].set_ticklabel_position('bcef') # Set limits so that no labels overlap ax.set_xlim(5.5, 100.5) ax.set_ylim(5.5, 110.5) # Clip the image to the frame im.set_clip_path(ax.coords.frame.patch) return fig
wcs = WCS(f[0].header) ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs) fig.add_axes(ax) data = sp.float64(f[0].data) if args.smooth > 0: data = gaussian_filter(data, args.smooth / (2 * np.sqrt(2 * np.log(2)))) pass vmin = data.min() vmax = data.max() #ax = fig.add_axes([0.17, 0.02, 0.72, 0.79]) axcolor = fig.add_axes([0.9, 0.1, 0.03, 0.8]) im = ax.imshow(data, cmap=cm.jet, origin="lower", norm=LogNorm(), vmin=1e-3 * vmax, vmax=vmax) #t = [0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0] fig.colorbar(im, cax=axcolor) #, ticks=t, format='$%.2f$') # gc = aplpy.FITSFigure(cmap_file,figure=fig) # gc.show_colorscale(aspect='auto') if regions is not None: r = pyregion.open(regions) r2 = r.as_imagecoord(f[0].header) patch_list, artist_list = r2.get_mpl_patches_texts() for p in patch_list: ax.add_patch(p) pass for t in artist_list: ax.add_artist(t)
# read in the image xray_name = "pspc_skyview.fits" f_xray = fits.open(xray_name) try: from astropy.wcs import WCS from astropy.visualization.wcsaxes import WCSAxes wcs = WCS(f_xray[0].header) fig = plt.figure() ax = WCSAxes(fig, [0.1, 0.1, 0.8, 0.8], wcs=wcs) fig.add_axes(ax) except ImportError: ax = plt.subplot(111) ax.imshow(f_xray[0].data, cmap=cm.gray, vmin=0., vmax=0.00038, origin="lower") reg_name = "test.reg" r = pyregion.open(reg_name).as_imagecoord(header=f_xray[0].header) from pyregion.mpl_helper import properties_func_default # Use custom function for patch attribute def fixed_color(shape, saved_attrs): attr_list, attr_dict = saved_attrs attr_dict["color"] = "red" kwargs = properties_func_default(shape, (attr_list, attr_dict)) return kwargs
def plot_image(img_file, hdu="IMAGE", stretch='linear', vmin=None, vmax=None, facecolor='black', center=None, width=None, figsize=(10, 10), cmap=None): """ Plot a FITS image created by SOXS using Matplotlib. Parameters ---------- img_file : str The on-disk FITS image to plot. hdu : str or int, optional The image extension to plot. Default is "IMAGE" stretch : str, optional The stretch to apply to the colorbar scale. Options are "linear", "log", and "sqrt". Default: "linear" vmin : float, optional The minimum value of the colorbar. If not set, it will be the minimum value in the image. vmax : float, optional The maximum value of the colorbar. If not set, it will be the maximum value in the image. facecolor : str, optional The color of zero-valued pixels. Default: "black" center : array-like A 2-element object giving an (RA, Dec) coordinate for the center in degrees. If not set, the reference pixel of the image (usually the center) is used. width : float, optional The width of the image in degrees. If not set, the width of the entire image will be used. figsize : tuple, optional A 2-tuple giving the size of the image in inches, e.g. (12, 15). Default: (10,10) cmap : str, optional The colormap to be used. If not set, the default Matplotlib colormap will be used. Returns ------- A tuple of the :class:`~matplotlib.figure.Figure` and the :class:`~matplotlib.axes.Axes` objects. """ import matplotlib.pyplot as plt from matplotlib.colors import PowerNorm, LogNorm, Normalize from astropy.wcs.utils import proj_plane_pixel_scales from astropy.visualization.wcsaxes import WCSAxes if stretch == "linear": norm = Normalize(vmin=vmin, vmax=vmax) elif stretch == "log": norm = LogNorm(vmin=vmin, vmax=vmax) elif stretch == "sqrt": norm = PowerNorm(0.5, vmin=vmin, vmax=vmax) else: raise RuntimeError(f"'{stretch}' is not a valid stretch!") with fits.open(img_file) as f: hdu = f[hdu] w = wcs.WCS(hdu.header) pix_scale = proj_plane_pixel_scales(w) if center is None: center = w.wcs.crpix else: center = w.wcs_world2pix(center[0], center[1], 0) if width is None: dx_pix = 0.5 * hdu.shape[0] dy_pix = 0.5 * hdu.shape[1] else: dx_pix = width / pix_scale[0] dy_pix = width / pix_scale[1] fig = plt.figure(figsize=figsize) ax = WCSAxes(fig, [0.15, 0.1, 0.8, 0.8], wcs=w) fig.add_axes(ax) im = ax.imshow(hdu.data, norm=norm, cmap=cmap) ax.set_xlim(center[0] - 0.5 * dx_pix, center[0] + 0.5 * dx_pix) ax.set_ylim(center[1] - 0.5 * dy_pix, center[1] + 0.5 * dy_pix) ax.set_facecolor(facecolor) cbar = plt.colorbar(im) return fig, ax
filename = './rcutout.fits' image = fits.open(filename) try: from astropy.wcs import WCS from astropy.visualization.wcsaxes import WCSAxes wcs = WCS(image[0].header) fig = plt.figure() ax = WCSAxes(fig, [.1,.1,.8,.8], wcs=wcs) fig.add_axes(ax) except ImportError: ax = plt.subplot(111) ax.imshow(image[0].data, cmap=cm.gray, vmin=0, vmax=0.00038, origin='lower') region_name = './satpeaks4.reg' r = pyregion.open(region_name).as_imagecoord(header=image[0].header) from pyregion.mpl_helper import properties_func_default def fixed_color(shape, saved_attrs): attr_list, attr_dict = saved_attrs attr_dict["color"] = "red" kwargs = properties_func_default(shape, (attr_list,attr_dict)) return kwargs r1 = pyregion.ShapeList([rr for rr in r if rr.attr[1].get("tag") == "Group 1"]) patch_list1, artist_list1 = r1.get_mpl_patches_texts(fixed_color)