def fillPolygon(self,xy,color = None, fill_color = None, ax=None,zorder=None,alpha=None): if self.resolution is None: raise AttributeError('there are no boundary datasets associated with this Basemap instance') # get current axes instance (if none specified). ax = ax or self._check_ax() # get axis background color. axisbgc = ax.get_axis_bgcolor() npoly = 0 polys = [] # xa, ya = list(zip(*map.county_region[0])) # check to see if all four corners of domain in polygon (if so, # don't draw since it will just fill in the whole map). # ** turn this off for now since it prevents continents that # fill the whole map from being filled ** # xy = list(zip(xa.tolist(),ya.tolist())) if self.coastpolygontypes[npoly] not in [2,4]: poly = Polygon(xy,facecolor=color,edgecolor=color,linewidth=0) else: # lakes filled with background color by default if fill_color is None: poly = Polygon(xy,facecolor=axisbgc,edgecolor=axisbgc,linewidth=0) else: poly = Polygon(xy,facecolor=fill_color,edgecolor=fill_color,linewidth=0) if zorder is not None: poly.set_zorder(zorder) if alpha is not None: poly.set_alpha(alpha) ax.add_patch(poly) polys.append(poly) npoly = npoly + 1 # set axes limits to fit map region. self.set_axes_limits(ax=ax) # clip continent polygons to map limbs polys,c = self._cliplimb(ax,polys) return polys
class HighLight(): def __init__(self): self.polygon = Polygon([[0, 0], [0, 0]], facecolor='lightyellow', edgecolor='green', linewidth=2) # dummy data for xs,ys def update_polygon(self, tri): if tri == -1: points = [(0, 0), (0, 0)] else: points = draggablePolygon.List[tri].sq.vertices # dps[tri].poly.set_edgecolor('green') # dps[tri].poly.set_color('red') self.polygon.set_xy(zip(*points)) self.polygon.set_zorder(tri + 1e6)
class MplPolygonalROI(AbstractMplRoi): """ Defines and displays polygonal ROIs on matplotlib plots Attributes: plot_opts: Dictionary instance A dictionary of plot keywords that are passed to the patch representing the ROI. These control the visual properties of the ROI """ def __init__(self, axes, roi=None): """ :param axes: A matplotlib Axes object to attach the graphical ROI to """ AbstractMplRoi.__init__(self, axes, roi=roi) self.plot_opts = { 'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR, 'alpha': 0.3 } self._setup_patch() def _setup_patch(self): self._patch = Polygon(np.array(list(zip([0, 1], [0, 1])))) self._patch.set_zorder(100) self._patch.set(**self.plot_opts) self._axes.add_patch(self._patch) self._patch.set_visible(False) self._sync_patch() def _roi_factory(self): return PolygonalROI() def _sync_patch(self): # Update geometry if not self._roi.defined(): self._patch.set_visible(False) else: x, y = self._roi.to_polygon() self._patch.set_xy(list(zip(x + [x[0]], y + [y[0]]))) self._patch.set_visible(True) # Update appearance self._patch.set(**self.plot_opts) # Refresh self._axes.figure.canvas.draw() def start_selection(self, event, scrubbing=False): if event.inaxes != self._axes: return False if scrubbing or event.key == SCRUBBING_KEY: if not self._roi.defined(): return False elif not self._roi.contains(event.xdata, event.ydata): return False self._roi_store() if scrubbing or event.key == SCRUBBING_KEY: self._scrubbing = True self._cx = event.xdata self._cy = event.ydata else: self.reset() self._roi.add_point(event.xdata, event.ydata) self._mid_selection = True self._sync_patch() def update_selection(self, event): if not self._mid_selection or event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False if self._scrubbing: self._roi.move_to(event.xdata - self._cx, event.ydata - self._cy) self._cx = event.xdata self._cy = event.ydata else: self._roi.add_point(event.xdata, event.ydata) self._sync_patch() def finalize_selection(self, event): self._scrubbing = False self._mid_selection = False self._patch.set_visible(False) self._axes.figure.canvas.draw()
class MplPolygonalROI(AbstractMplRoi): """ Defines and displays polygonal ROIs on matplotlib plots Attributes: plot_opts: Dictionary instance A dictionary of plot keywords that are passed to the patch representing the ROI. These control the visual properties of the ROI """ def __init__(self, axes): """ :param axes: A matplotlib Axes object to attach the graphical ROI to """ AbstractMplRoi.__init__(self, axes) self.plot_opts = {'edgecolor': PATCH_COLOR, 'facecolor': PATCH_COLOR, 'alpha': 0.3} self._setup_patch() def _setup_patch(self): self._patch = Polygon(np.array(list(zip([0, 1], [0, 1])))) self._patch.set_zorder(100) self._patch.set(**self.plot_opts) self._axes.add_patch(self._patch) self._patch.set_visible(False) self._sync_patch() def _roi_factory(self): return PolygonalROI() def _sync_patch(self): # Update geometry if not self._roi.defined(): self._patch.set_visible(False) else: x, y = self._roi.to_polygon() self._patch.set_xy(list(zip(x + [x[0]], y + [y[0]]))) self._patch.set_visible(True) # Update appearance self._patch.set(**self.plot_opts) # Refresh self._axes.figure.canvas.draw() def start_selection(self, event): if event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False elif not self._roi.contains(event.xdata, event.ydata): return False self._roi_store() if event.key == SCRUBBING_KEY: self._scrubbing = True self._cx = event.xdata self._cy = event.ydata else: self.reset() self._roi.add_point(event.xdata, event.ydata) self._mid_selection = True self._sync_patch() def update_selection(self, event): if not self._mid_selection or event.inaxes != self._axes: return False if event.key == SCRUBBING_KEY: if not self._roi.defined(): return False if self._scrubbing: self._roi.move_to(event.xdata - self._cx, event.ydata - self._cy) self._cx = event.xdata self._cy = event.ydata else: self._roi.add_point(event.xdata, event.ydata) self._sync_patch() def finalize_selection(self, event): self._scrubbing = False self._mid_selection = False self._patch.set_visible(False) self._axes.figure.canvas.draw()
m = Basemap(projection='eck4', lon_0=160, resolution='c', ax=ax) m.drawcoastlines(linewidth=0, color="#FFFFFF") m.drawmapboundary(color="aqua") m.fillcontinents(color='#cccccc', lake_color='#FFFFFF') # Read shapefile m.readshapefile("data/ne_10m_admin_0_countries/ne_10m_admin_0_countries", "units", drawbounds=False) patches = [] for info, shape in zip(m.units_info, m.units): poly = Polygon(np.array(shape), True) poly.set_facecolor('none') poly.set_linewidth(0) poly.set_edgecolor("#000000") poly.set_zorder(1) poly = ax.add_patch(poly) patches.append(poly) x1, y1 = m(coords["lng"].values, coords["lat"].values) _c = [scalarMap.to_rgba(groups.index(i)) for i in groups] p = m.scatter(x1, y1, marker="o", alpha=1, color=_c, zorder=2, sizes=[0] * coords.shape[0]) lwidths = list(np.logspace(np.log(1), np.log(5), 300, base=np.e)) beziers = []
def fillcontinentsX(self, color='0.8', lake_color=None, ax=None, zorder=None): """ Fill continents. .. tabularcolumns:: |l|L| ============== ==================================================== Keyword Description ============== ==================================================== color color to fill continents (default gray). lake_color color to fill inland lakes (default axes background). ax axes instance (overrides default axes instance). zorder sets the zorder for the continent polygons (if not specified, uses default zorder for a Polygon patch). Set to zero if you want to paint over the filled continents). ============== ==================================================== After filling continents, lakes are re-filled with axis background color. returns a list of matplotlib.patches.Polygon objects. """ if self.resolution is None: raise AttributeError, 'there are no boundary datasets associated with this Basemap instance' # get current axes instance (if none specified). ax = ax or self._check_ax() # get axis background color. axisbgc = ax.get_axis_bgcolor() npoly = 0 polys = [] for x, y in self.coastpolygons: xa = num.array(x, num.float32) ya = num.array(y, num.float32) # check to see if all four corners of domain in polygon (if so, # don't draw since it will just fill in the whole map). delx = 10 dely = 10 if self.projection in ['cyl']: delx = 0.1 dely = 0.1 test1 = num.fabs(xa - self.urcrnrx) < delx test2 = num.fabs(xa - self.llcrnrx) < delx test3 = num.fabs(ya - self.urcrnry) < dely test4 = num.fabs(ya - self.llcrnry) < dely hasp1 = num.sum(test1 * test3) hasp2 = num.sum(test2 * test3) hasp4 = num.sum(test2 * test4) hasp3 = num.sum(test1 * test4) if not hasp1 or not hasp2 or not hasp3 or not hasp4: xy = zip(xa.tolist(), ya.tolist()) if self.coastpolygontypes[npoly] not in [2, 4]: poly = Polygon(xy, facecolor=color, edgecolor=color, linewidth=0) else: # lakes filled with background color by default if lake_color is None: poly = Polygon(xy, facecolor=axisbgc, edgecolor=axisbgc, linewidth=0) else: poly = Polygon(xy, facecolor=lake_color, edgecolor=lake_color, linewidth=0) if zorder is not None: poly.set_zorder(zorder) ax.add_patch(poly) polys.append(poly) npoly = npoly + 1 # set axes limits to fit map region. self.set_axes_limits(ax=ax) return polys
def fillwaterbodies(basemap, color='blue', inlands=True, ax=None, zorder=None): """ Fills the water bodies with color. If inlands is True, inland water bodies are also filled. Parameters ---------- basemap : Basemap The basemap on which to fill. color : {'blue', string, RGBA tuple}, optional Filling color inlands : {True, False}, optional Whether inland water bodies should be filled. ax : {None, Axes instance}, optional Axe on which to plot. If None, the current axe is selected. zorder : {None, integer}, optional zorder of the water bodies polygons. """ if not isinstance(basemap, Basemap): raise TypeError, "The input basemap should be a valid Basemap object!" # if ax is None and basemap.ax is None: try: ax = pyplot.gca() except: import matplotlib.pyplot as pyplot ax = pyplot.gca() basemap.ax = ax # coastpolygons = basemap.coastpolygons coastpolygontypes = basemap.coastpolygontypes (llx, lly) = (basemap.llcrnrx, basemap.llcrnry) (urx, ury) = (basemap.urcrnrx, basemap.urcrnry) background = Polygon([(llx, lly), (urx, lly), (urx, ury), (llx, ury)]) # ogr_background = polygon_to_geometry(background) inland_polygons = [] # for (poly, polytype) in zip(coastpolygons, coastpolygontypes): if polytype != 2: verts = ["%s %s" % (x, y) for (x, y) in zip(*poly)] ogr_poly = ogr.CreateGeometryFromWkt("POLYGON ((%s))" % ','.join(verts)) ogr_background = ogr_background.Difference(ogr_poly) else: inland_polygons.append( Polygon(zip(*poly), facecolor=color, edgecolor=color, linewidth=0)) # background = geometry_to_vertices(ogr_background) for xy in background: patch = Polygon(xy, facecolor=color, edgecolor=color, linewidth=0) if zorder is not None: patch.set_zorder(zorder) ax.add_patch(patch) # if inlands: for poly in inland_polygons: if zorder is not None: poly.set_zorder(zorder) ax.add_patch(poly) basemap.set_axes_limits(ax=ax) return
def fillwaterbodies(basemap, color='blue', inlands=True, ax=None, zorder=None): """ Fills the water bodies with color. If inlands is True, inland water bodies are also filled. Parameters ---------- basemap : Basemap The basemap on which to fill. color : {'blue', string, RGBA tuple}, optional Filling color inlands : {True, False}, optional Whether inland water bodies should be filled. ax : {None, Axes instance}, optional Axe on which to plot. If None, the current axe is selected. zorder : {None, integer}, optional zorder of the water bodies polygons. """ if not isinstance(basemap, Basemap): raise TypeError, "The input basemap should be a valid Basemap object!" # if ax is None and basemap.ax is None: try: ax = pyplot.gca() except: import matplotlib.pyplot as pyplot ax = pyplot.gca() basemap.ax = ax # coastpolygons = basemap.coastpolygons coastpolygontypes = basemap.coastpolygontypes (llx, lly) = (basemap.llcrnrx, basemap.llcrnry) (urx, ury) = (basemap.urcrnrx, basemap.urcrnry) background = Polygon([(llx, lly), (urx, lly), (urx, ury), (llx, ury)]) # ogr_background = polygon_to_geometry(background) inland_polygons = [] # for (poly, polytype) in zip(coastpolygons, coastpolygontypes): if polytype != 2: verts = ["%s %s" % (x, y) for (x, y) in zip(*poly)] ogr_poly = ogr.CreateGeometryFromWkt("POLYGON ((%s))" % ','.join(verts)) ogr_background = ogr_background.Difference(ogr_poly) else: inland_polygons.append(Polygon(zip(*poly), facecolor=color, edgecolor=color, linewidth=0)) # background = geometry_to_vertices(ogr_background) for xy in background: patch = Polygon(xy, facecolor=color, edgecolor=color, linewidth=0) if zorder is not None: patch.set_zorder(zorder) ax.add_patch(patch) # if inlands: for poly in inland_polygons: if zorder is not None: poly.set_zorder(zorder) ax.add_patch(poly) basemap.set_axes_limits(ax=ax) return
def fillcontinentsX(self,color='0.8',lake_color=None,ax=None,zorder=None): """ Fill continents. .. tabularcolumns:: |l|L| ============== ==================================================== Keyword Description ============== ==================================================== color color to fill continents (default gray). lake_color color to fill inland lakes (default axes background). ax axes instance (overrides default axes instance). zorder sets the zorder for the continent polygons (if not specified, uses default zorder for a Polygon patch). Set to zero if you want to paint over the filled continents). ============== ==================================================== After filling continents, lakes are re-filled with axis background color. returns a list of matplotlib.patches.Polygon objects. """ if self.resolution is None: raise AttributeError, 'there are no boundary datasets associated with this Basemap instance' # get current axes instance (if none specified). ax = ax or self._check_ax() # get axis background color. axisbgc = ax.get_axis_bgcolor() npoly = 0 polys = [] for x,y in self.coastpolygons: xa = num.array(x,num.float32) ya = num.array(y,num.float32) # check to see if all four corners of domain in polygon (if so, # don't draw since it will just fill in the whole map). delx = 10; dely = 10 if self.projection in ['cyl']: delx = 0.1 dely = 0.1 test1 = num.fabs(xa-self.urcrnrx) < delx test2 = num.fabs(xa-self.llcrnrx) < delx test3 = num.fabs(ya-self.urcrnry) < dely test4 = num.fabs(ya-self.llcrnry) < dely hasp1 = num.sum(test1*test3) hasp2 = num.sum(test2*test3) hasp4 = num.sum(test2*test4) hasp3 = num.sum(test1*test4) if not hasp1 or not hasp2 or not hasp3 or not hasp4: xy = zip(xa.tolist(),ya.tolist()) if self.coastpolygontypes[npoly] not in [2,4]: poly = Polygon(xy,facecolor=color,edgecolor=color,linewidth=0) else: # lakes filled with background color by default if lake_color is None: poly = Polygon(xy,facecolor=axisbgc,edgecolor=axisbgc,linewidth=0) else: poly = Polygon(xy,facecolor=lake_color,edgecolor=lake_color,linewidth=0) if zorder is not None: poly.set_zorder(zorder) ax.add_patch(poly) polys.append(poly) npoly = npoly + 1 # set axes limits to fit map region. self.set_axes_limits(ax=ax) return polys