def test_imshow_clip(): # As originally reported by Gellule Xg <*****@*****.**> #Create a NxN image N=100 (x,y) = np.indices((N,N)) x -= N/2 y -= N/2 r = np.sqrt(x**2+y**2-x*y) #Create a contour plot at N/4 and extract both the clip path and transform fig = plt.figure() ax = fig.add_subplot(111) c = ax.contour(r,[N/4]) x = c.collections[0] clipPath = x.get_paths()[0] clipTransform = x.get_transform() from matplotlib.transforms import TransformedPath clip_path = TransformedPath(clipPath, clipTransform) #Plot the image clipped by the contour ax.imshow(r, clip_path=clip_path) fig.savefig('imshow_clip')
class BezierPath(Line2D): def __init__(self, path, *args, **kwargs): """ Parameters ---------- path : `~.path.Path` The path to draw. **kwargs All remaining keyword arguments are passed to `.Line2D`. """ Line2D.__init__(self, [], [], *args, **kwargs) self._path = path self._invalid = False def recache(self): self._transformed_path = TransformedPath(self._path, self.get_transform()) self._invalid = False def set_path(self, path): self._path = path self._invalid = True def draw(self, renderer): if self._invalid: self.recache() if not self._visible: return renderer.open_group('line2d', gid=self.get_gid()) gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_foreground(self._color) gc.set_antialiased(self._antialiased) gc.set_linewidth(self._linewidth) gc.set_alpha(self._alpha) if self.is_dashed(): cap = self._dashcapstyle join = self._dashjoinstyle else: cap = self._solidcapstyle join = self._solidjoinstyle gc.set_joinstyle(join) gc.set_capstyle(cap) gc.set_dashes(self._dashOffset, self._dashSeq) if self._lineStyles[self._linestyle] != '_draw_nothing': tpath, affine = ( self._transformed_path.get_transformed_path_and_affine()) renderer.draw_path(gc, tpath, affine.frozen()) gc.restore() renderer.close_group('line2d')
def test_transformed_path(): points = [(0, 0), (1, 0), (1, 1), (0, 1)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] path = Path(points, codes) trans = mtrans.Affine2D() trans_path = TransformedPath(path, trans) assert np.allclose(trans_path.get_fully_transformed_path().vertices, points) # Changing the transform should change the result. r2 = 1 / np.sqrt(2) trans.rotate(np.pi / 4) assert np.allclose(trans_path.get_fully_transformed_path().vertices, [(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)]) # Changing the path does not change the result (it's cached). path.points = [(0, 0)] * 4 assert np.allclose(trans_path.get_fully_transformed_path().vertices, [(0, 0), (r2, r2), (0, 2 * r2), (-r2, r2)])
class BezierPath(mlines.Line2D): def __init__(self, path, *kl, **kw): mlines.Line2D.__init__(self, [], [], *kl, **kw) self._path = path self._invalid = False def recache(self): self._transformed_path = TransformedPath(self._path, self.get_transform()) self._invalid = False def set_path(self, path): self._path = path self._invalid = True def draw(self, renderer): if self._invalid: self.recache() renderer.open_group('line2d') if not self._visible: return gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_foreground(self._color) gc.set_antialiased(self._antialiased) gc.set_linewidth(self._linewidth) gc.set_alpha(self._alpha) if self.is_dashed(): cap = self._dashcapstyle join = self._dashjoinstyle else: cap = self._solidcapstyle join = self._solidjoinstyle gc.set_joinstyle(join) gc.set_capstyle(cap) funcname = self._lineStyles.get(self._linestyle, '_draw_nothing') if funcname != '_draw_nothing': tpath, affine = self._transformed_path.get_transformed_path_and_affine() lineFunc = getattr(self, funcname) lineFunc(renderer, gc, tpath, affine.frozen()) gc.restore() renderer.close_group('line2d')
class BezierPath(mlines.Line2D): def __init__(self, path, *kl, **kw): mlines.Line2D.__init__(self, [], [], *kl, **kw) self._path = path self._invalid = False def recache(self): self._transformed_path = TransformedPath(self._path, self.get_transform()) self._invalid = False def set_path(self, path): self._path = path self._invalid = True def draw(self, renderer): if self._invalid: self.recache() if not self._visible: return renderer.open_group('line2d') gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_foreground(self._color) gc.set_antialiased(self._antialiased) gc.set_linewidth(self._linewidth) gc.set_alpha(self._alpha) if self.is_dashed(): cap = self._dashcapstyle join = self._dashjoinstyle else: cap = self._solidcapstyle join = self._solidjoinstyle gc.set_joinstyle(join) gc.set_capstyle(cap) gc.set_dashes(self._dashOffset, self._dashSeq) if self._lineStyles[self._linestyle] != '_draw_nothing': tpath, affine = ( self._transformed_path.get_transformed_path_and_affine()) renderer.draw_path(gc, tpath, affine.frozen()) gc.restore() renderer.close_group('line2d')
class BezierPath(Line2D): def __init__(self, path, *args, **kwargs): Line2D.__init__(self, [], [], *args, **kwargs) self._path = path self._invalid = False def recache(self): self._transformed_path = TransformedPath( self._path, self.get_transform()) self._invalid = False def set_path(self, path): self._path = path self._invalid = True def draw(self, renderer): if self._invalid: self.recache() if not self._visible: return renderer.open_group('line2d') gc = renderer.new_gc() self._set_gc_clip(gc) gc.set_foreground(self._color) gc.set_antialiased(self._antialiased) gc.set_linewidth(self._linewidth) gc.set_alpha(self._alpha) if self.is_dashed(): cap = self._dashcapstyle join = self._dashjoinstyle else: cap = self._solidcapstyle join = self._solidjoinstyle gc.set_joinstyle(join) gc.set_capstyle(cap) gc.set_dashes(self._dashOffset, self._dashSeq) if self._lineStyles[self._linestyle] != '_draw_nothing': tpath, affine = ( self._transformed_path.get_transformed_path_and_affine()) renderer.draw_path(gc, tpath, affine.frozen()) gc.restore() renderer.close_group('line2d')
def recache(self): self._transformed_path = TransformedPath(self._path, self.get_transform()) self._invalid = False
def _get_transformed_clip_path(self): x, y, width, height = self.state.clipping_path rect = ((x, y), (x + width, y), (x + width, y + height), (x, y + height)) transform = Affine2D.from_values(*affine.affine_params(self.get_ctm())) return TransformedPath(Path(rect), transform)
def _transform_path(self): _path = Path(self._xydata) self._transformed_path = TransformedPath(_path, self.get_transform())