def plot_model(axes, model): n = len(model.waveforms) offset_step = 1/(n+1) plots = [] text_trans = T.blended_transform_factory(axes.figure.transFigure, axes.transAxes) limits = [(w.y.min(), w.y.max()) for w in model.waveforms] base_scale = np.mean(np.abs(np.array(limits))) bscale_in_box = T.Bbox([[0, -base_scale], [1, base_scale]]) bscale_out_box = T.Bbox([[0, -1], [1, 1]]) bscale_in = T.BboxTransformFrom(bscale_in_box) bscale_out = T.BboxTransformTo(bscale_out_box) tscale_in_box = T.Bbox([[0, -1], [1, 1]]) tscale_out_box = T.Bbox([[0, 0], [1, offset_step]]) tscale_in = T.BboxTransformFrom(tscale_in_box) tscale_out = T.BboxTransformTo(tscale_out_box) boxes = { 'tscale': tscale_in_box, 'tnorm': [], 'norm_limits': limits/base_scale, } for i, waveform in enumerate(model.waveforms): y_min, y_max = waveform.y.min(), waveform.y.max() tnorm_in_box = T.Bbox([[0, -1], [1, 1]]) tnorm_out_box = T.Bbox([[0, -1], [1, 1]]) tnorm_in = T.BboxTransformFrom(tnorm_in_box) tnorm_out = T.BboxTransformTo(tnorm_out_box) boxes['tnorm'].append(tnorm_in_box) offset = offset_step * i + offset_step * 0.5 translate = T.Affine2D().translate(0, offset) y_trans = bscale_in + bscale_out + \ tnorm_in + tnorm_out + \ tscale_in + tscale_out + \ translate + axes.transAxes trans = T.blended_transform_factory(axes.transData, y_trans) plot = WaveformPlot(waveform, axes, trans) plots.append(plot) text_trans = T.blended_transform_factory(axes.transAxes, y_trans) axes.text(-0.05, 0, f'{waveform.level}', transform=text_trans) axes.set_yticks([]) axes.grid() for spine in ('top', 'left', 'right'): axes.spines[spine].set_visible(False) return plots, boxes
def dragdone_a(self, a, evt, shift=None, scale=None): shift = evt.guiEvent.ShiftDown() redraw, scale0 = super(FigLegend, self).dragdone_a(a, evt, shift=shift, scale=scale) x = min(self._drag_rec[:2]) y = min(self._drag_rec[2:]) bbx = a.get_bbox_to_anchor() pos = mpltransforms.BboxTransformFrom(bbx).transform_point((x, y)) h = [ UndoRedoFigobjMethod(a, 'legendloc', (True, self.getp('legendloc'), pos)) ] window = evt.guiEvent.GetEventObject().GetTopLevelParent() hist = GlobalHistory().get_history(window) self._hit_a = None self._picker_a_mode = 0 # h = h + self.scale_artist(scale0, action = True) if len(h) != 0: hist.start_record() for item in h: hist.add_history(item) hist.stop_record() return 0, scale0
def coords2index(im, x, y): """ This function is modified from here: https://github.com/joferkington/mpldatacursor/blob/7dabc589ed02c35ac5d89de5931f91e0323aa795/mpldatacursor/pick_info.py#L28 Converts data coordinates to index coordinates of the array. Parameters ----------- im : An AxesImage instance The image artist to operate on x : number The x-coordinate in data coordinates. y : number The y-coordinate in data coordinates. Returns -------- i, j : Index coordinates of the array associated with the image. """ xmin, xmax, ymin, ymax = im.get_extent() if im.origin == 'upper': ymin, ymax = ymax, ymin data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = mtransforms.Bbox([[0, 0], im.get_array().shape[:2]]) trans = (mtransforms.BboxTransformFrom(data_extent) + mtransforms.BboxTransformTo(array_extent)) return trans.transform_point([y, x]).astype(int)
def _set_lim_and_transforms(self): """ Override transform initialization """ # axis coords to display coords self.transAxes = transforms.BboxTransformTo(self.bbox) # X and Y axis scaling self.transScale = transforms.TransformWrapper( transforms.IdentityTransform()) # transform from given Bbox to unit Bbox # the given transformedBbox is updated every time the # viewLim changes or the transScale changes self.transLimits = transforms.BboxTransformFrom( transforms.TransformedBbox(self.viewLim, self.transScale)) # data to display coordinates self.transData = self.transScale + (self.transLimits + self.transAxes) # blended transforms for xaxis and yaxis self._xaxis_transform = transforms.blended_transform_factory( self.transData, self.transAxes) self._yaxis_transform = transforms.blended_transform_factory( self.transAxes, self.transData)
def _coords2index(im, x, y, inverted=False): """ Converts data coordinates to index coordinates of the array. Parameters ----------- im : An AxesImage instance The image artist to operation on x : number The x-coordinate in data coordinates. y : number The y-coordinate in data coordinates. inverted : bool, optional If True, convert index to data coordinates instead of data coordinates to index. Returns -------- i, j : Index coordinates of the array associated with the image. """ xmin, xmax, ymin, ymax = im.get_extent() if im.origin == 'upper': ymin, ymax = ymax, ymin data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = mtransforms.Bbox([[0, 0], im.get_array().shape[:2]]) trans = mtransforms.BboxTransformFrom(data_extent) +\ mtransforms.BboxTransformTo(array_extent) if inverted: trans = trans.inverted() return trans.transform_point([y, x]).astype(int)
def _set_lim_and_transforms(self): super()._set_lim_and_transforms() transTernaryScale = TernaryScaleTransform(self.ternary_scale) transTLimits = mtransforms.BboxTransformFrom( mtransforms.TransformedBbox(self.viewTLim, self.transScale)) transLLimits = mtransforms.BboxTransformFrom( mtransforms.TransformedBbox(self.viewLLim, self.transScale)) transRLimits = mtransforms.BboxTransformFrom( mtransforms.TransformedBbox(self.viewRLim, self.transScale)) corners_axes = self.corners_axes taxis_transform = TernaryTransform(corners_axes, 0) laxis_transform = TernaryTransform(corners_axes, 1) raxis_transform = TernaryTransform(corners_axes, 2) self._taxis_transform = transTLimits + taxis_transform + self.transAxes self._laxis_transform = transLLimits + laxis_transform + self.transAxes self._raxis_transform = transRLimits + raxis_transform + self.transAxes # For axis labels t_l_t = TernaryPerpendicularTransform(self.transAxes, corners_axes, 0) l_l_t = TernaryPerpendicularTransform(self.transAxes, corners_axes, 1) r_l_t = TernaryPerpendicularTransform(self.transAxes, corners_axes, 2) self._taxis_label_transform = t_l_t self._laxis_label_transform = l_l_t self._raxis_label_transform = r_l_t # From ternary coordinates to the original data coordinates self.transProjection = (transTernaryScale + BarycentricTransform(self.corners_data)) # From ternary coordinates to the original Axes coordinates self._ternary_axes_transform = self.transProjection + self.transLimits # From barycentric coordinates to the original Axes coordinates self.transAxesProjection = BarycentricTransform(self.corners_axes) # From barycentric coordinates to display coordinates self.transTernaryAxes = self.transAxesProjection + self.transAxes
def _coords2index(im, x, y): """ Convert data coordinates to index coordinates of the image array. Credit: mpldatacursor developers. Copyright (c) 2012. BSD License Modified from original found at: https://github.com/joferkington/mpldatacursor/blob/master/mpldatacursor/pick_info.py """ xmin, xmax, ymin, ymax = im.get_extent() if im.origin == 'upper': ymin, ymax = ymax, ymin im_shape = im.get_array().shape[:2] data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = mtransforms.Bbox([[0, 0], im_shape]) trans = (mtransforms.BboxTransformFrom(data_extent) + mtransforms.BboxTransformTo(array_extent)) j, i = trans.transform_point([y, x]).astype(int) # Clip the coordinates to the array bounds. return min(max(j, 0), im_shape[0] - 1), min(max(i, 0), im_shape[1] - 1)
def _coords2index(im, x, y): """ Converts data coordinates to index coordinates of the array. Parameters ----------- im : A matplotlib image artist. x : The x-coordinate in data coordinates. y : The y-coordinate in data coordinates. Returns -------- i, j : Index coordinates of the array associated with the image. """ xmin, xmax, ymin, ymax = im.get_extent() if im.origin == 'upper': ymin, ymax = ymax, ymin data_extent = mtransforms.Bbox([[ymin, xmin], [ymax, xmax]]) array_extent = mtransforms.Bbox([[0, 0], im.get_array().shape[:2]]) trans = mtransforms.BboxTransformFrom(data_extent) +\ mtransforms.BboxTransformTo(array_extent) return trans.transform_point([y, x]).astype(int)
def _set_lim_and_transforms(self): # A view limit where the minimum radius can be locked if the user # specifies an alternate origin. self._originViewLim = mtransforms.LockableBbox(self.viewLim) # Handle angular offset and direction. self._direction = mtransforms.Affine2D() \ .scale(self._default_theta_direction, 1.0) self._theta_offset = mtransforms.Affine2D() \ .translate(self._default_theta_offset, 0.0) self.transShift = self._direction + self._theta_offset # A view limit shifted to the correct location after accounting for # orientation and offset. self._realViewLim = mtransforms.TransformedBbox( self.viewLim, self.transShift) # Transforms the x and y axis separately by a scale factor # It is assumed that this part will have non-linear components self.transScale = mtransforms.TransformWrapper( mtransforms.IdentityTransform()) # Scale view limit into a bbox around the selected wedge. This may be # smaller than the usual unit axes rectangle if not plotting the full # circle. self.axesLim = _WedgeBbox((0.5, 0.5), self._realViewLim, self._originViewLim) # Scale the wedge to fill the axes. self.transWedge = mtransforms.BboxTransformFrom(self.axesLim) # Scale the axes to fill the figure. self.transAxes = mtransforms.BboxTransformTo(self.bbox) # A (possibly non-linear) projection on the (already scaled) # data. This one is aware of rmin self.transProjection = self.PolarTransform( self, _apply_theta_transforms=False) # Add dependency on rorigin. self.transProjection.set_children(self._originViewLim) # An affine transformation on the data, generally to limit the # range of the axes self.transProjectionAffine = self.PolarAffine(self.transScale, self._originViewLim) # The complete data transformation stack -- from data all the # way to display coordinates self.transData = ( self.transScale + self.transShift + self.transProjection + (self.transProjectionAffine + self.transWedge + self.transAxes)) # This is the transform for theta-axis ticks. It is # equivalent to transData, except it always puts r == 0.0 and r == 1.0 # at the edge of the axis circles. self._xaxis_transform = (mtransforms.blended_transform_factory( mtransforms.IdentityTransform(), mtransforms.BboxTransformTo(self.viewLim)) + self.transData) # The theta labels are flipped along the radius, so that text 1 is on # the outside by default. This should work the same as before. flipr_transform = mtransforms.Affine2D() \ .translate(0.0, -0.5) \ .scale(1.0, -1.0) \ .translate(0.0, 0.5) self._xaxis_text_transform = flipr_transform + self._xaxis_transform # This is the transform for r-axis ticks. It scales the theta # axis so the gridlines from 0.0 to 1.0, now go from thetamin to # thetamax. self._yaxis_transform = (mtransforms.blended_transform_factory( mtransforms.BboxTransformTo(self.viewLim), mtransforms.IdentityTransform()) + self.transData) # The r-axis labels are put at an angle and padded in the r-direction self._r_label_position = mtransforms.Affine2D() \ .translate(self._default_rlabel_position, 0.0) self._yaxis_text_transform = mtransforms.TransformWrapper( self._r_label_position + self.transData)