Skip to content

Commit

Permalink
Merge pull request #4 from gbrammer/add_patch_to_axis
Browse files Browse the repository at this point in the history
add_patch_to_axis method
  • Loading branch information
gbrammer authored Aug 16, 2023
2 parents fba8121 + 81c838a commit 429d26d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
37 changes: 35 additions & 2 deletions sregion/sregion.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@ def __init__(self, inp, label=None, wrap=True, **kwargs):
self.ds9_properties = ''
self.label = label


@property
def N(self):
return len(self.xy)


@property
def centroid(self):
return [np.mean(fp, axis=0) for fp in self.xy]


def sky_buffer(self, buffer_deg):
"""
Buffer polygons accounting for cos(dec)
Expand All @@ -222,20 +225,23 @@ def sky_buffer(self, buffer_deg):

self.xy = new_xy


@property
def path(self):
"""
`~matplotlib.path.Path` object
"""
return [Path(fp) for fp in self.xy]


@property
def shapely(self):
"""
`~shapely.geometry.Polygon` object.
"""
return [Polygon(fp).convex_hull for fp in self.xy]



@property
def geoms(self):
"""
Expand All @@ -245,14 +251,16 @@ def geoms(self):
return self.shapely.geoms
else:
return self.shapely



@property
def area(self):
"""
Area of shapely polygons
"""
return [sh.area for sh in self.geoms]


def sky_area(self, unit=u.arcmin**2):
"""
Assuming coordinates provided are RA/Dec degrees, compute area
Expand All @@ -261,6 +269,7 @@ def sky_area(self, unit=u.arcmin**2):
return [(sh.area*cosd*u.deg**2).to(unit)
for sh in self.geoms]


def matplotlib_patch(self, **kwargs):
"""
`~matplotlib.patches.PathPatch` object
Expand All @@ -273,6 +282,7 @@ def matplotlib_patch(self, **kwargs):

return patches


def descartes_patch(self, **kwargs):
"""
`~descartes.PolygonPatch` object
Expand All @@ -285,6 +295,7 @@ def descartes_patch(self, **kwargs):

return [PolygonPatch(p, **kwargs) for p in self.geoms]


def patch(self, **kwargs):
"""
general patch object
Expand All @@ -296,6 +307,7 @@ def patch(self, **kwargs):
except ImportError:
return self.matplotlib_patch(**kwargs)


def get_patch(self, **kwargs):
"""
`~descartes.PolygonPatch` object
Expand All @@ -304,6 +316,24 @@ def get_patch(self, **kwargs):
"""
return self.patch(**kwargs)


def add_patch_to_axis(self, ax, **kwargs):
"""
Add patches to a plot axis
Parameters
----------
ax : matplotlib axis
Axis in which do draw patches
kwargs : dict
Keyword arguments passed to `~sregion.SRegion.get_patch`
"""
for patch in self.get_patch(**kwargs):
ax.add_patch(patch)


def union(self, shape=None, as_polygon=False):
"""
Union of self and `shape` object. If no `shape` provided, then
Expand All @@ -322,6 +352,7 @@ def union(self, shape=None, as_polygon=False):
else:
return SRegion(un)


def intersects(self, shape):
"""
Union of self and `shape` object
Expand All @@ -332,6 +363,7 @@ def intersects(self, shape):

return test


@property
def region(self):
"""
Expand All @@ -353,6 +385,7 @@ def region(self):
return [pstr.format(','.join([f'{c:.6f}' for c in fp.flatten()]))+tail
for fp in self.xy]


@property
def s_region(self):
"""
Expand Down
19 changes: 18 additions & 1 deletion sregion/tests/test_sregion.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,21 @@ def test_patch():
"""

circ = SRegion('CIRCLE 5. 5. 1', ncircle=256)
patch = patch_from_polygon(circ.shapely[0], fc='k')
patch = patch_from_polygon(circ.shapely[0], fc='k')


def test_draw_patch():
"""
Draw in matplotlib axis
"""
import matplotlib.pyplot as plt

fig, ax = plt.subplots(1,1)
ax.plot([-10,10], [-10,10])

circ = SRegion('CIRCLE 5. 5. 1', ncircle=256)

circ.add_patch_to_axis(ax, fc='r', alpha=0.5)

plt.close('all')

0 comments on commit 429d26d

Please sign in to comment.