def set_scale_bar(self, location=None, length=None, maxlen=0.25, **kwargs): """Add a legend bar showing the scale to the plot. Parameters ---------- location : tuple the location of the bar (in the plot's relative coordinates) length : float the length of the bar in proj units (m or deg). Default is to find the nicest number satisfying ``maxlen`` maxlen : float the maximum lenght of the bar (in the plot's relative coordinates) when choosing the length automatically kwargs : dict any kwarg accepted by ``set_geometry``. Defaults are put on ``color``, ``linewidth``, ``text``, ``text_kwargs``... But you can do whatever you want """ x0, x1, y0, y1 = self.grid.extent # Find a sensible length for the scale if length is None: length = utils.nice_scale(x1 - x0, maxlen=maxlen) if location is None: location = (0.96 - length / 2 / (x1 - x0), 0.04) # scalebar center location in proj coordinates sbcx, sbcy = x0 + (x1 - x0) * location[0], y0 + (y1 - y0) * location[1] # coordinates for the scalebar line = LineString(([sbcx - length / 2, sbcy], [sbcx + length / 2, sbcy])) # Units if self.grid.proj.is_latlong(): units = 'deg' elif length >= 1000.: length /= 1000 units = 'km' else: units = 'm' # Nice number if int(length) == length: length = int(length) # Defaults kwargs.setdefault('color', 'k') kwargs.setdefault('text', '{} '.format(length) + units) kwargs.setdefault('text_delta', (0.0, 0.015)) kwargs.setdefault('linewidth', 3) tkw = kwargs.get('text_kwargs', {}) tkw.setdefault('horizontalalignment', 'center') tkw.setdefault('color', kwargs['color']) kwargs['text_kwargs'] = tkw self.set_geometry(line, crs=self.grid.proj, **kwargs)
def set_scale_bar(self, location=None, length=None, maxlen=0.25, add_bbox=False, bbox_dx=1.2, bbox_dy=1.2, bbox_kwargs=None, **kwargs): """Add a legend bar showing the scale to the plot. Parameters ---------- location : tuple the location of the bar (in the plot's relative coordinates) length : float the length of the bar in proj units (m or deg). Default is to find the nicest number satisfying ``maxlen`` maxlen : float the maximum lenght of the bar (in the plot's relative coordinates) when choosing the length automatically add_bbox : bool add a bounding box to the scale bar (WIP: experimental) bbox_dx : bool, default: 1.2 a multiplicating factor controlling the x size of the bounding box (trial and error works best for this one) bbox_dy : bool, default: 1.2 a multiplicating factor controlling the y size of the bounding box (trial and error works best for this one) bbox_kwargs : dict kwarg to pass to set_geometry() for the bounding box (e.g. facecolor, alpha, etc...) kwargs : dict any kwarg accepted by ``set_geometry``. Defaults are put on ``color``, ``linewidth``, ``text``, ``text_kwargs``... But you can do whatever you want """ x0, x1, y0, y1 = self.grid.extent # Find a sensible length for the scale if length is None: length = utils.nice_scale(x1 - x0, maxlen=maxlen) if location is None: location = (0.96 - length / 2 / (x1 - x0), 0.04) # scalebar center location in proj coordinates sbcx, sbcy = x0 + (x1 - x0) * location[0], y0 + (y1 - y0) * location[1] # coordinates for the scalebar line = LineString(([sbcx - length / 2, sbcy], [sbcx + length / 2, sbcy])) # Of the bounding box bbox = [ [sbcx - length / 2 * bbox_dx, sbcy - length / 4 * bbox_dy], [sbcx - length / 2 * bbox_dx, sbcy + length / 4 * bbox_dy], [sbcx + length / 2 * bbox_dx, sbcy + length / 4 * bbox_dy], [sbcx + length / 2 * bbox_dx, sbcy - length / 4 * bbox_dy], ] # Units if self.grid.proj.is_latlong(): units = 'deg' elif length >= 1000.: length /= 1000 units = 'km' else: units = 'm' # Nice number if int(length) == length: length = int(length) # Defaults kwargs.setdefault('color', 'k') kwargs.setdefault('text', '{} '.format(length) + units) kwargs.setdefault('text_delta', (0.0, 0.015)) kwargs.setdefault('linewidth', 3) kwargs.setdefault('zorder', 99) tkw = kwargs.get('text_kwargs', {}) tkw.setdefault('horizontalalignment', 'center') tkw.setdefault('zorder', 99) tkw.setdefault('color', kwargs['color']) kwargs['text_kwargs'] = tkw if add_bbox: if bbox_kwargs is None: bbox_kwargs = {} bbox_kwargs.setdefault('facecolor', 'w') bbox_kwargs.setdefault('edgecolor', 'k') bbox_kwargs.setdefault('zorder', 98) poly = Polygon(np.asarray(bbox)) self.set_geometry(poly, crs=self.grid.proj, **bbox_kwargs) self.set_geometry(line, crs=self.grid.proj, **kwargs)