Skip to content

Commit

Permalink
Merge pull request #24 from joleroi/plotting
Browse files Browse the repository at this point in the history
Update plotting API
  • Loading branch information
joleroi committed May 17, 2016
2 parents 7b8aab6 + 920815d commit 07f4136
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 29 deletions.
8 changes: 7 additions & 1 deletion regions/core/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def to_mask(self, mode='center'):
def to_sky(self, wcs, mode='local', tolerance=None):
raise NotImplementedError("")

def as_patch(self, **kwargs):
raise NotImplementedError("")

def __repr__(self):
return "({0} {1} {2})".format(self.region1, self.operator, self.region2)

Expand All @@ -41,9 +44,12 @@ def contains(self, skycoord):
def to_pixel(self, wcs, mode='local', tolerance=None):
raise NotImplementedError("")

def as_patch(self, ax, **kwargs):
raise NotImplementedError("")

def __repr__(self):
return "({0}\n{1}\n{2})".format(self.region1, self.operator, self.region2)

@property
def area(self):
raise NotImplementedError("")
raise NotImplementedError("")
65 changes: 64 additions & 1 deletion regions/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def __xor__(self, other):
return self.symmetric_difference(other)

def __contains__(self, coord):
# Todo: only works for scalar cases, test for this?
if not coord.isscalar:
raise ValueError('{} must be scalar'.format(coord))
return self.contains(coord)


Expand Down Expand Up @@ -165,6 +166,37 @@ def to_shapely(self):
"""
raise NotImplementedError("")

@abc.abstractmethod
def as_patch(self, **kwargs):
"""Convert to mpl patch
Returns
-------
patch : `~matplotlib.patches.Patch`
Matplotlib patch
"""
raise NotImplementedError

def plot(self, ax=None, **kwargs):
"""
Calls as_patch method forwarding all kwargs and adds patch
to given axis.
Parameters
----------
ax : `~matplotlib.axes`, optional
Axis
"""
import matplotlib.pyplot as plt

if ax is None:
ax = plt.gca()

patch = self.as_patch(**kwargs)
ax.add_patch(patch)

return ax


@six.add_metaclass(abc.ABCMeta)
class SkyRegion(Region):
Expand Down Expand Up @@ -248,3 +280,34 @@ def to_pixel(self, wcs, mode='local', tolerance=None):
The tolerance for the ``'full'`` mode described above.
"""
raise NotImplementedError("")

@abc.abstractmethod
def as_patch(self, ax, **kwargs):
"""Convert to mpl patch using a given wcs axis
Parameters
----------
ax : `~astropy.wcsaxes.WCSAxes`
WCS axis object
Returns
-------
patch : `~matplotlib.patches.Patch`
Matplotlib patch
"""
raise NotImplementedError

def plot(self, ax=None, **kwargs):
"""
Calls as_patch method forwarding all kwargs and adds patch
to given wcs axis.
Parameters
----------
ax : `~astropy.wcsaxes.WCSAxes`
WCS axis object
"""
patch = self.as_patch(ax, **kwargs)
ax.add_patch(patch)

return ax
26 changes: 2 additions & 24 deletions regions/shapes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ def to_mask(self, mode='center'):
# TODO: needs to be implemented
raise NotImplementedError("")

def to_mpl_patch(self, **kwargs):
"""Convert to mpl patch.
Returns
-------
patch : `~matplotlib.mpatches.Circle`
Matplotlib patch
"""
def as_patch(self, **kwargs):
import matplotlib.patches as mpatches

patch = mpatches.Circle(self.center, self.radius, **kwargs)
Expand Down Expand Up @@ -140,22 +133,7 @@ def to_pixel(self, mywcs, mode='local', tolerance=None):

return CirclePixelRegion(pixel_positions, radius_pix)

def to_mpl_patch(self, ax, **kwargs):
"""Convert to mpl patch using a given wcs axis
Parameters
----------
ax : `~astropy.wcsaxes.WCSAxes`
WCS axis object
kwargs : dict
kwargs are forwarded to mpatches.Circle
Returns
-------
patch : `~matplotlib.mpatches.Circle`
Matplotlib patch
"""

def as_patch(self, ax, **kwargs):
import matplotlib.patches as mpatches

val = self.center.icrs
Expand Down
7 changes: 7 additions & 0 deletions regions/shapes/ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def to_mask(self, mode='center'):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")


class EllipseSkyRegion(SkyRegion):
Expand Down Expand Up @@ -92,3 +95,7 @@ def contains(self, skycoord):
def to_pixel(self, wcs, mode='local', tolerance=None):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")
9 changes: 9 additions & 0 deletions regions/shapes/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def to_mask(self, mode='center'):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")



class PointSkyRegion(SkyRegion):
Expand Down Expand Up @@ -68,3 +72,8 @@ def contains(self, skycoord):
def to_pixel(self, wcs, mode='local', tolerance=None):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")

7 changes: 7 additions & 0 deletions regions/shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def to_mask(self, mode='center'):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")

class PolygonSkyRegion(SkyRegion):
"""
Expand Down Expand Up @@ -67,3 +70,7 @@ def contains(self, skycoord):
def to_pixel(self, wcs, mode='local', tolerance=None):
# TODO: needs to be implemented
raise NotImplementedError("")

def as_patch(self, **kwargs):
# TODO: needs to be implemented
raise NotImplementedError("")
2 changes: 1 addition & 1 deletion regions/shapes/rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RectangleSkyRegion(SkyRegion):
def __init__(self, center, height, width, angle=0 * u.deg, meta=None, visual=None):
# TODO: use quantity_input to check that height, width, and angle are angles
self.center = center
self.height = minor
self.height = height
self.width = width
self.angle = angle
self.meta = meta or {}
Expand Down
5 changes: 3 additions & 2 deletions regions/shapes/tests/test_circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ def test_plot():
wcs = WCS(h)
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], projection=wcs)
p = c.to_mpl_patch(ax, alpha=0.6)
ax.add_patch(p)
p = c.as_patch(ax, alpha=0.6)

assert_allclose(p.center[0], skycoord.icrs.ra.value)
assert_allclose(p.center[1], skycoord.icrs.dec.value)
assert p.get_facecolor() == (1, 0, 0, 0.6)

c.plot(ax, alpha=0.6)

0 comments on commit 07f4136

Please sign in to comment.