lsst.afw  16.0-9-gdf7162dbc+1
polygon.py
Go to the documentation of this file.
1 from ._polygon import Polygon
2 
3 __all__ = [] # import this module only for its side effects
4 
5 
6 def __repr__(self):
7  return "%s(%s)" % (self.__class__.__name__, [p for p in self.getVertices()])
8 
9 
10 def __reduce__(self):
11  return self.__class__, (self.getVertices(),)
12 
13 
14 def __iter__(self):
15  """Iterator over vertices"""
16  vertices = self.getVertices()
17  return iter(vertices)
18 
19 
20 def __getitem__(self, i):
21  return [pt for pt in self][i]
22 
23 
24 def __len__(self):
25  return self.getNumEdges()
26 
27 
28 def __contains__(self, point):
29  """point in polygon?"""
30  return self.contains(point)
31 
32 
33 def display(self, xy0=None, frame=1, ctype=None):
34  """Display polygon on existing frame in ds9"""
35  import lsst.geom
36  import lsst.afw.display.ds9 as ds9
37  xy0 = lsst.geom.Extent2D(0, 0) if xy0 is None else lsst.geom.Extent2D(xy0)
38  with ds9.Buffering():
39  for p1, p2 in self.getEdges():
40  ds9.line((p1 - xy0, p2 - xy0), frame=frame, ctype=ctype)
41 
42 
43 def plot(self, axes=None, **kwargs):
44  """Plot polygon with matplotlib
45 
46  Parameters
47  ----------
48  axes : `matplotlib.axes.Axes`
49  Matplotlib axes to use, or None
50  kwargs : any
51  Additional arguments to `matplotlib.axes.Axes.plot`
52  or `matplotlib.axes.Axes.scatter`.
53 
54  Returns
55  -------
56  axes : `matplotlib.axes.Axes`
57  The axes used to make the plot (same as ``axes``, if provided).
58  """
59  import numpy
60  if axes is None:
61  import matplotlib.pyplot as plt
62  plt.figure()
63  axes = plt.axes()
64  for p1, p2 in self.getEdges():
65  x = (p1.getX(), p2.getX())
66  y = (p1.getY(), p2.getY())
67  axes.plot(x, y, **kwargs)
68  vertices = self.getVertices()
69  x = numpy.array([p[0] for p in vertices])
70  y = numpy.array([p[1] for p in vertices])
71  axes.scatter(x, y, **kwargs)
72  return axes
73 
74 
75 Polygon.__repr__ = __repr__
76 Polygon.__reduce__ = __reduce__
77 Polygon.__iter__ = __iter__
78 Polygon.__getitem__ = __getitem__
79 Polygon.__len__ = __len__
80 Polygon.__contains__ = __contains__
81 Polygon.display = display
82 Polygon.plot = plot
def display(self, xy0=None, frame=1, ctype=None)
Definition: polygon.py:33
def __contains__(self, point)
Definition: polygon.py:28
def plot(self, axes=None, kwargs)
Definition: polygon.py:43