def set_ternary_lim(self, tmin, tmax, lmin, lmax, rmin, rmax, *args, **kwargs): """ Notes ----- xmin, xmax : holizontal limits of the triangle ymin, ymax : bottom and top of the triangle """ t = tmax + lmin + rmin l = tmin + lmax + rmin r = tmin + lmin + rmax s = self.ternary_scale tol = 1e-12 if (abs(t - s) > tol) or (abs(l - s) > tol) or (abs(r - s) > tol): raise ValueError(t, l, r, s) boxin = self._create_bbox_from_ternary_lim() self.set_tlim(tmin, tmax) self.set_llim(lmin, lmax) self.set_rlim(rmin, rmax) boxout = self._create_bbox_from_ternary_lim() trans = mtransforms.BboxTransform(boxin, boxout) xmin, xmax = self.get_xlim() ymin, ymax = self.get_ylim() points = [[xmin, ymin], [xmax, ymax]] ((xmin, ymin), (xmax, ymax)) = trans.transform(points) self.set_xlim(xmin, xmax) self.set_ylim(ymin, ymax)
def get_cursor_data(self, event): """Get the cursor data for a given event""" xmin, xmax, ymin, ymax = self.get_extent() if self.origin == 'upper': ymin, ymax = ymax, ymin arr = self.get_array() data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = mtransforms.Bbox([[0, 0], arr.shape[:2]]) trans = mtransforms.BboxTransform(boxin=data_extent, boxout=array_extent) y, x = event.ydata, event.xdata i, j = trans.transform_point([y, x]).astype(int) return arr[i, j]
def __init__(self, waveform, axis, offset): self.offset = offset self.axis = axis self.waveform = waveform self.normalized = False self.current = False self.points = {} # Compute offset transform offset = transforms.Affine2D().translate(0, self.offset) self.t_reg = self.axis.transLimits + offset + self.axis.transAxes # Compute normalized transform. Basically the min/max of the waveform # are scaled to the range [0, 1] (i.e. normalized) before being passed # to the t_reg transform. boxin = transforms.Bbox([[0, self.waveform.y.min()], [1, self.waveform.y.max()]]) boxout = transforms.Bbox([[0, 0], [1, 1]]) self.t_norm = transforms.BboxTransform(boxin, boxout) + self.t_reg # Create the plot self.plot, = self.axis.plot(self.waveform.x, self.waveform.y) self.update()
def make_axes(parents, location=None, orientation=None, fraction=0.15, shrink=1.0, aspect=20, **kw): ''' Resize and reposition parent axes, and return a child axes suitable for a colorbar. Keyword arguments may include the following (with defaults): location : [None|'left'|'right'|'top'|'bottom'] The position, relative to **parents**, where the colorbar axes should be created. If None, the value will either come from the given ``orientation``, else it will default to 'right'. orientation : [None|'vertical'|'horizontal'] The orientation of the colorbar. Typically, this keyword shouldn't be used, as it can be derived from the ``location`` keyword. %s Returns (cax, kw), the child axes and the reduced kw dictionary to be passed when creating the colorbar instance. ''' locations = ["left", "right", "top", "bottom"] if orientation is not None and location is not None: msg = ('position and orientation are mutually exclusive. ' 'Consider setting the position to any of ' '{0}'.format(', '.join(locations))) raise TypeError(msg) # provide a default location if location is None and orientation is None: location = 'right' # allow the user to not specify the location by specifying the # orientation instead if location is None: location = 'right' if orientation == 'vertical' else 'bottom' if location not in locations: raise ValueError('Invalid colorbar location. Must be one ' 'of %s' % ', '.join(locations)) default_location_settings = {'left': {'anchor': (1.0, 0.5), 'panchor': (0.0, 0.5), 'pad': 0.10, 'orientation': 'vertical'}, 'right': {'anchor': (0.0, 0.5), 'panchor': (1.0, 0.5), 'pad': 0.05, 'orientation': 'vertical'}, 'top': {'anchor': (0.5, 0.0), 'panchor': (0.5, 1.0), 'pad': 0.05, 'orientation': 'horizontal'}, 'bottom': {'anchor': (0.5, 1.0), 'panchor': (0.5, 0.0), 'pad': 0.15, # backwards compat 'orientation': 'horizontal'}, } loc_settings = default_location_settings[location] # put appropriate values into the kw dict for passing back to # the Colorbar class kw['orientation'] = loc_settings['orientation'] kw['ticklocation'] = location anchor = kw.pop('anchor', loc_settings['anchor']) parent_anchor = kw.pop('panchor', loc_settings['panchor']) pad = kw.pop('pad', loc_settings['pad']) # turn parents into a list if it is not already if not isinstance(parents, (list, tuple)): parents = [parents] fig = parents[0].get_figure() if not all(fig is ax.get_figure() for ax in parents): raise ValueError('Unable to create a colorbar axes as not all ' 'parents share the same figure.') # take a bounding box around all of the given axes parents_bbox = mtrans.Bbox.union([ax.get_position(original=True).frozen() for ax in parents]) pb = parents_bbox if location in ('left', 'right'): if location == 'left': pbcb, _, pb1 = pb.splitx(fraction, fraction + pad) else: pb1, _, pbcb = pb.splitx(1 - fraction - pad, 1 - fraction) pbcb = pbcb.shrunk(1.0, shrink).anchored(anchor, pbcb) else: if location == 'bottom': pbcb, _, pb1 = pb.splity(fraction, fraction + pad) else: pb1, _, pbcb = pb.splity(1 - fraction - pad, 1 - fraction) pbcb = pbcb.shrunk(shrink, 1.0).anchored(anchor, pbcb) # define the aspect ratio in terms of y's per x rather than x's per y aspect = 1.0 / aspect # define a transform which takes us from old axes coordinates to # new axes coordinates shrinking_trans = mtrans.BboxTransform(parents_bbox, pb1) # transform each of the axes in parents using the new transform for ax in parents: new_posn = shrinking_trans.transform(ax.get_position()) new_posn = mtrans.Bbox(new_posn) ax.set_position(new_posn) if parent_anchor is not False: ax.set_anchor(parent_anchor) cax = fig.add_axes(pbcb) cax.set_aspect(aspect, anchor=anchor, adjustable='box') return cax, kw