gammapy/maps/hpxnd.py
def _plot_poly(self, proj="AIT", step=1, ax=None):
        """Plot the map using a collection of polygons.

        Parameters
        ----------
        proj : string, optional
            Any valid WCS projection type.
        step : int
            Set the number vertices that will be computed for each
            pixel in multiples of 4.
        """
        # FIXME: At the moment this only works for all-sky maps if the
        # projection is centered at (0,0)

        # FIXME: Figure out how to force a square aspect-ratio like imshow

        import matplotlib.pyplot as plt
        from matplotlib.patches import Polygon
        from matplotlib.collections import PatchCollection
        import healpy as hp

        wcs = self.geom.to_wcs_geom(proj=proj, oversample=1)
        if ax is None:
            fig = plt.gcf()
            ax = fig.add_subplot(111, projection=wcs.wcs, aspect="equal")

        wcs_lonlat = wcs.center_coord[:2]
        idx = self.geom.get_idx()
        vtx = hp.boundaries(self.geom.nside, idx[0], nest=self.geom.nest, step=step)
        theta, phi = hp.vec2ang(np.rollaxis(vtx, 2))
        theta = theta.reshape((4 * step, -1)).T
        phi = phi.reshape((4 * step, -1)).T

        patches = []
        data = []

        def get_angle(x, t):
            return 180.0 - (180.0 - x + t) % 360.0

        for i, (x, y) in enumerate(zip(phi, theta)):

            lon, lat = np.degrees(x), np.degrees(np.pi / 2.0 - y)
            # Add a small ofset to avoid vertices wrapping to the
            # other size of the projection
            if get_angle(np.median(lon), wcs_lonlat[0].to_value("deg")) > 0:
                idx = wcs.coord_to_pix((lon - 1e-4, lat))
            else:
                idx = wcs.coord_to_pix((lon + 1e-4, lat))

            dist = np.max(np.abs(idx[0][0] - idx[0]))

            # Split pixels that wrap around the edges of the projection
            if dist > wcs.npix[0] / 1.5:
                lon, lat = np.degrees(x), np.degrees(np.pi / 2.0 - y)
                lon0 = lon - 1e-4
                lon1 = lon + 1e-4
                pix0 = wcs.coord_to_pix((lon0, lat))
                pix1 = wcs.coord_to_pix((lon1, lat))

                idx0 = np.argsort(pix0[0])
                idx1 = np.argsort(pix1[0])

                pix0 = (pix0[0][idx0][:3], pix0[1][idx0][:3])
                pix1 = (pix1[0][idx1][1:], pix1[1][idx1][1:])

                patches.append(Polygon(np.vstack((pix0[0], pix0[1])).T, True))
                patches.append(Polygon(np.vstack((pix1[0], pix1[1])).T, True))
                data.append(self.data[i])
                data.append(self.data[i])
            else:
                polygon = Polygon(np.vstack((idx[0], idx[1])).T, True)
                patches.append(polygon)
                data.append(self.data[i])

        p = PatchCollection(patches, linewidths=0, edgecolors="None")
        p.set_array(np.array(data))
        ax.add_collection(p)
        ax.autoscale_view()
        ax.coords.grid(color="w", linestyle=":", linewidth=0.5)

        return fig, ax, p
Plot the map using a collection of polygons.

        Parameters
        ----------
        proj : string, optional
            Any valid WCS projection type.
        step : int
            Set the number vertices that will be computed for each
            pixel in multiples of 4.
        
Plot the map using a collection of polygons.
Parameters

proj : string, optional
Any valid WCS projection type.
step : int
Set the number vertices that will be computed for each
pixel in multiples of 4.
Plot the map using a collection of polygons . Parameters proj : string optional Any valid WCS projection type . step : int Set the number vertices that will be computed for each pixel in multiples of 4 .
def _plot_poly(self, proj="AIT", step=1, ax=None):
        import matplotlib.pyplot as plt
        from matplotlib.patches import Polygon
        from matplotlib.collections import PatchCollection
        import healpy as hp
        wcs = self.geom.to_wcs_geom(proj=proj, oversample=1)
        if ax is None:
            fig = plt.gcf()
            ax = fig.add_subplot(111, projection=wcs.wcs, aspect="equal")
        wcs_lonlat = wcs.center_coord[:2]
        idx = self.geom.get_idx()
        vtx = hp.boundaries(self.geom.nside, idx[0], nest=self.geom.nest, step=step)
        theta, phi = hp.vec2ang(np.rollaxis(vtx, 2))
        theta = theta.reshape((4 * step, -1)).T
        phi = phi.reshape((4 * step, -1)).T
        patches = []
        data = []
        def get_angle(x, t):
            return 180.0 - (180.0 - x + t) % 360.0
        for i, (x, y) in enumerate(zip(phi, theta)):
            lon, lat = np.degrees(x), np.degrees(np.pi / 2.0 - y)
            if get_angle(np.median(lon), wcs_lonlat[0].to_value("deg")) > 0:
                idx = wcs.coord_to_pix((lon - 1e-4, lat))
            else:
                idx = wcs.coord_to_pix((lon + 1e-4, lat))
            dist = np.max(np.abs(idx[0][0] - idx[0]))
            if dist > wcs.npix[0] / 1.5:
                lon, lat = np.degrees(x), np.degrees(np.pi / 2.0 - y)
                lon0 = lon - 1e-4
                lon1 = lon + 1e-4
                pix0 = wcs.coord_to_pix((lon0, lat))
                pix1 = wcs.coord_to_pix((lon1, lat))
                idx0 = np.argsort(pix0[0])
                idx1 = np.argsort(pix1[0])
                pix0 = (pix0[0][idx0][:3], pix0[1][idx0][:3])
                pix1 = (pix1[0][idx1][1:], pix1[1][idx1][1:])
                patches.append(Polygon(np.vstack((pix0[0], pix0[1])).T, True))
                patches.append(Polygon(np.vstack((pix1[0], pix1[1])).T, True))
                data.append(self.data[i])
                data.append(self.data[i])
            else:
                polygon = Polygon(np.vstack((idx[0], idx[1])).T, True)
                patches.append(polygon)
                data.append(self.data[i])
        p = PatchCollection(patches, linewidths=0, edgecolors="None")
        p.set_array(np.array(data))
        ax.add_collection(p)
        ax.autoscale_view()
        ax.coords.grid(color="w", linestyle=":", linewidth=0.5)
        return fig, ax, p
def _plot_poly ( self , proj = "AIT" , step = 1 , ax = None ) : import matplotlib . pyplot as plt from matplotlib . patches import Polygon from matplotlib . collections import PatchCollection import healpy as hp wcs = self . geom . to_wcs_geom ( proj = proj , oversample = 1 ) if ax is None : fig = plt . gcf ( ) ax = fig . add_subplot ( 111 , projection = wcs . wcs , aspect = "equal" ) wcs_lonlat = wcs . center_coord [ : 2 ] idx = self . geom . get_idx ( ) vtx = hp . boundaries ( self . geom . nside , idx [ 0 ] , nest = self . geom . nest , step = step ) theta , phi = hp . vec2ang ( np . rollaxis ( vtx , 2 ) ) theta = theta . reshape ( ( 4 * step , - 1 ) ) . T phi = phi . reshape ( ( 4 * step , - 1 ) ) . T patches = [ ] data = [ ] def get_angle ( x , t ) : return 180.0 - ( 180.0 - x + t ) % 360.0 for i , ( x , y ) in enumerate ( zip ( phi , theta ) ) : lon , lat = np . degrees ( x ) , np . degrees ( np . pi / 2.0 - y ) if get_angle ( np . median ( lon ) , wcs_lonlat [ 0 ] . to_value ( "deg" ) ) > 0 : idx = wcs . coord_to_pix ( ( lon - 1e-4 , lat ) ) else : idx = wcs . coord_to_pix ( ( lon + 1e-4 , lat ) ) dist = np . max ( np . abs ( idx [ 0 ] [ 0 ] - idx [ 0 ] ) ) if dist > wcs . npix [ 0 ] / 1.5 : lon , lat = np . degrees ( x ) , np . degrees ( np . pi / 2.0 - y ) lon0 = lon - 1e-4 lon1 = lon + 1e-4 pix0 = wcs . coord_to_pix ( ( lon0 , lat ) ) pix1 = wcs . coord_to_pix ( ( lon1 , lat ) ) idx0 = np . argsort ( pix0 [ 0 ] ) idx1 = np . argsort ( pix1 [ 0 ] ) pix0 = ( pix0 [ 0 ] [ idx0 ] [ : 3 ] , pix0 [ 1 ] [ idx0 ] [ : 3 ] ) pix1 = ( pix1 [ 0 ] [ idx1 ] [ 1 : ] , pix1 [ 1 ] [ idx1 ] [ 1 : ] ) patches . append ( Polygon ( np . vstack ( ( pix0 [ 0 ] , pix0 [ 1 ] ) ) . T , True ) ) patches . append ( Polygon ( np . vstack ( ( pix1 [ 0 ] , pix1 [ 1 ] ) ) . T , True ) ) data . append ( self . data [ i ] ) data . append ( self . data [ i ] ) else : polygon = Polygon ( np . vstack ( ( idx [ 0 ] , idx [ 1 ] ) ) . T , True ) patches . append ( polygon ) data . append ( self . data [ i ] ) p = PatchCollection ( patches , linewidths = 0 , edgecolors = "None" ) p . set_array ( np . array ( data ) ) ax . add_collection ( p ) ax . autoscale_view ( ) ax . coords . grid ( color = "w" , linestyle = ":" , linewidth = 0.5 ) return fig , ax , p
Plot the map using a collection of polygons.
Plot the map using a collection of polygons .
"""Plot the map using a collection of polygons.

        Parameters
        ----------
        proj : string, optional
            Any valid WCS projection type.
        step : int
            Set the number vertices that will be computed for each
            pixel in multiples of 4.
        """ # FIXME: At the moment this only works for all-sky maps if the # projection is centered at (0,0) # FIXME: Figure out how to force a square aspect-ratio like imshow # Add a small ofset to avoid vertices wrapping to the # other size of the projection # Split pixels that wrap around the edges of the projection
self
proj
step
ax