def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=1.0, # the default linewidth of the frame frameon=True, # whether or not to draw the figure frame subplotpars=None, # default to rc ): """ *figsize* w,h tuple in inches *dpi* dots per inch *facecolor* the figure patch facecolor; defaults to rc ``figure.facecolor`` *edgecolor* the figure patch edge color; defaults to rc ``figure.edgecolor`` *linewidth* the figure patch edge linewidth; the default linewidth of the frame *frameon* if False, suppress drawing the figure frame *subplotpars* a :class:`SubplotParams` instance, defaults to rc """ Artist.__init__(self) self.callbacks = cbook.CallbackRegistry(('dpi_changed', )) if figsize is None: figsize = rcParams['figure.figsize'] if dpi is None: dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self.dpi_scale_trans = Affine2D() self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans) self.frameon = frameon self.transFigure = BboxTransformTo(self.bbox) self.patch = self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.patch) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self._axstack = Stack() # maintain the current axes self.axes = [] self.clf() self._cachedRenderer = None
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=1.0, # the default linewidth of the frame frameon=True, # whether or not to draw the figure frame subplotpars=None, # default to rc ): """ figsize is a w,h tuple in inches dpi is dots per inch subplotpars is a SubplotParams instance, defaults to rc """ Artist.__init__(self) if figsize is None: figsize = rcParams['figure.figsize'] if dpi is None: dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self._dpi_scale_trans = Affine2D() self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) self.bbox = TransformedBbox(self.bbox_inches, self._dpi_scale_trans) self.frameon = frameon self.transFigure = BboxTransformTo(self.bbox) self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self._axstack = Stack() # maintain the current axes self.axes = [] self.clf() self._cachedRenderer = None self._autoLayout = rcParams['figure.autolayout']
def __init__(self, figsize = None, # defaults to rc figure.figsize dpi = None, # defaults to rc figure.dpi facecolor = None, # defaults to rc figure.facecolor edgecolor = None, # defaults to rc figure.edgecolor linewidth = 1.0, # the default linewidth of the frame frameon = True, # whether or not to draw the figure frame subplotpars = None, # default to rc ): """ figsize is a w,h tuple in inches dpi is dots per inch subplotpars is a SubplotParams instance, defaults to rc """ Artist.__init__(self) if figsize is None : figsize = rcParams['figure.figsize'] if dpi is None : dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self._dpi_scale_trans = Affine2D() self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) self.bbox = TransformedBbox(self.bbox_inches, self._dpi_scale_trans) self.frameon = frameon self.transFigure = BboxTransformTo(self.bbox) self.figurePatch = Rectangle( xy=(0,0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.figurePatch) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self._axstack = Stack() # maintain the current axes self.axes = [] self.clf() self._cachedRenderer = None self._autoLayout = rcParams['figure.autolayout']
def _find_best_position(self, width, height, consider=None): """Determine the best location to place the legend. `consider` is a list of (x, y) pairs to consider as a potential lower-left corner of the legend. All are axes coords. """ assert self.isaxes # should always hold because function is only called internally verts, bboxes, lines = self._auto_legend_data() consider = [ self._loc_to_axes_coords(x, width, height) for x in range(1, len(self.codes)) ] tx, ty = self.legendPatch.get_x(), self.legendPatch.get_y() candidates = [] for l, b in consider: legendBox = Bbox.from_bounds(l, b, width, height) badness = 0 badness = legendBox.count_contains(verts) badness += legendBox.count_overlaps(bboxes) for line in lines: if line.intersects_bbox(legendBox): badness += 1 ox, oy = l - tx, b - ty if badness == 0: return ox, oy candidates.append((badness, (ox, oy))) # rather than use min() or list.sort(), do this so that we are assured # that in the case of two equal badnesses, the one first considered is # returned. minCandidate = candidates[0] for candidate in candidates: if candidate[0] < minCandidate[0]: minCandidate = candidate ox, oy = minCandidate[1] return ox, oy
def get_window_extent(self, renderer=None): #return _unit_box if not self.get_visible(): return Bbox.unit() if self._text == '': tx, ty = self._get_xy_display() return Bbox.from_bounds(tx,ty,0,0) if renderer is not None: self._renderer = renderer if self._renderer is None: raise RuntimeError('Cannot get window extent w/o renderer') angle = self.get_rotation() bbox, info = self._get_layout(self._renderer) x, y = self.get_position() x, y = self.get_transform().transform_point((x, y)) bbox = bbox.translated(x, y) return bbox
def get_window_extent(self, renderer=None): #return _unit_box if not self.get_visible(): return Bbox.unit() if self._text == '': tx, ty = self._get_xy_display() return Bbox.from_bounds(tx, ty, 0, 0) if renderer is not None: self._renderer = renderer if self._renderer is None: raise RuntimeError('Cannot get window extent w/o renderer') angle = self.get_rotation() bbox, info = self._get_layout(self._renderer) x, y = self.get_position() x, y = self.get_transform().transform_point((x, y)) bbox = bbox.translated(x, y) return bbox
def _find_best_position(self, width, height, consider=None): """Determine the best location to place the legend. `consider` is a list of (x, y) pairs to consider as a potential lower-left corner of the legend. All are axes coords. """ assert self.isaxes # should always hold because function is only called internally verts, bboxes, lines = self._auto_legend_data() consider = [self._loc_to_axes_coords(x, width, height) for x in range(1, len(self.codes))] tx, ty = self.legendPatch.get_x(), self.legendPatch.get_y() candidates = [] for l, b in consider: legendBox = Bbox.from_bounds(l, b, width, height) badness = 0 badness = legendBox.count_contains(verts) badness += legendBox.count_overlaps(bboxes) for line in lines: if line.intersects_bbox(legendBox): badness += 1 ox, oy = l-tx, b-ty if badness == 0: return ox, oy candidates.append((badness, (ox, oy))) # rather than use min() or list.sort(), do this so that we are assured # that in the case of two equal badnesses, the one first considered is # returned. minCandidate = candidates[0] for candidate in candidates: if candidate[0] < minCandidate[0]: minCandidate = candidate ox, oy = minCandidate[1] return ox, oy
def get_window_extent(self, renderer=None, dpi=None): ''' Return a :class:`~matplotlib.transforms.Bbox` object bounding the text, in display units. In addition to being used internally, this is useful for specifying clickable regions in a png file on a web page. *renderer* defaults to the _renderer attribute of the text object. This is not assigned until the first execution of :meth:`draw`, so you must use this kwarg if you want to call :meth:`get_window_extent` prior to the first :meth:`draw`. For getting web page regions, it is simpler to call the method after saving the figure. *dpi* defaults to self.figure.dpi; the renderer dpi is irrelevant. For the web application, if figure.dpi is not the value used when saving the figure, then the value that was used must be specified as the *dpi* argument. ''' #return _unit_box if not self.get_visible(): return Bbox.unit() if dpi is not None: dpi_orig = self.figure.dpi self.figure.dpi = dpi if self._text == '': tx, ty = self._get_xy_display() return Bbox.from_bounds(tx,ty,0,0) if renderer is not None: self._renderer = renderer if self._renderer is None: raise RuntimeError('Cannot get window extent w/o renderer') bbox, info = self._get_layout(self._renderer) x, y = self.get_position() x, y = self.get_transform().transform_point((x, y)) bbox = bbox.translated(x, y) if dpi is not None: self.figure.dpi = dpi_orig return bbox
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=0.0, # the default linewidth of the frame frameon=True, # whether or not to draw the figure frame subplotpars=None, # default to rc ): """ *figsize* w,h tuple in inches *dpi* dots per inch *facecolor* the figure patch facecolor; defaults to rc ``figure.facecolor`` *edgecolor* the figure patch edge color; defaults to rc ``figure.edgecolor`` *linewidth* the figure patch edge linewidth; the default linewidth of the frame *frameon* if ``False``, suppress drawing the figure frame *subplotpars* a :class:`SubplotParams` instance, defaults to rc """ Artist.__init__(self) self.callbacks = cbook.CallbackRegistry() if figsize is None: figsize = rcParams['figure.figsize'] if dpi is None: dpi = rcParams['figure.dpi'] if facecolor is None: facecolor = rcParams['figure.facecolor'] if edgecolor is None: edgecolor = rcParams['figure.edgecolor'] self.dpi_scale_trans = Affine2D() self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans) self.frameon = frameon self.transFigure = BboxTransformTo(self.bbox) # the figurePatch name is deprecated self.patch = self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, ) self._set_artist_props(self.patch) self.patch.set_aa(False) self._hold = rcParams['axes.hold'] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self._axstack = AxesStack() # track all figure axes and current axes self.clf() self._cachedRenderer = None
def _get_layout(self, renderer): key = self.get_prop_tup() if self.cached.has_key(key): return self.cached[key] horizLayout = [] thisx, thisy = 0.0, 0.0 xmin, ymin = 0.0, 0.0 width, height = 0.0, 0.0 lines = self._text.split('\n') whs = npy.zeros((len(lines), 2)) horizLayout = npy.zeros((len(lines), 4)) # Find full vertical extent of font, # including ascenders and descenders: tmp, heightt, bl = renderer.get_text_width_height_descent( 'lp', self._fontproperties, ismath=False) offsety = heightt * self._linespacing baseline = None for i, line in enumerate(lines): w, h, d = renderer.get_text_width_height_descent( line, self._fontproperties, ismath=self.is_math_text(line)) if baseline is None: baseline = h - d whs[i] = w, h horizLayout[i] = thisx, thisy, w, h thisy -= offsety width = max(width, w) ymin = horizLayout[-1][1] ymax = horizLayout[0][1] + horizLayout[0][3] height = ymax-ymin xmax = xmin + width # get the rotation matrix M = Affine2D().rotate_deg(self.get_rotation()) offsetLayout = npy.zeros((len(lines), 2)) offsetLayout[:] = horizLayout[:, 0:2] # now offset the individual text lines within the box if len(lines)>1: # do the multiline aligment malign = self._get_multialignment() if malign == 'center': offsetLayout[:, 0] += width/2.0 - horizLayout[:, 2] / 2.0 elif malign == 'right': offsetLayout[:, 0] += width - horizLayout[:, 2] # the corners of the unrotated bounding box cornersHoriz = npy.array( [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)], npy.float_) # now rotate the bbox cornersRotated = M.transform(cornersHoriz) txs = cornersRotated[:, 0] tys = cornersRotated[:, 1] # compute the bounds of the rotated box xmin, xmax = txs.min(), txs.max() ymin, ymax = tys.min(), tys.max() width = xmax - xmin height = ymax - ymin # Now move the box to the targe position offset the display bbox by alignment halign = self._horizontalalignment valign = self._verticalalignment # compute the text location in display coords and the offsets # necessary to align the bbox with that location if halign=='center': offsetx = (xmin + width/2.0) elif halign=='right': offsetx = (xmin + width) else: offsetx = xmin if valign=='center': offsety = (ymin + height/2.0) elif valign=='top': offsety = (ymin + height) elif valign=='baseline': offsety = (ymin + height) + baseline else: offsety = ymin xmin -= offsetx ymin -= offsety bbox = Bbox.from_bounds(xmin, ymin, width, height) # now rotate the positions around the first x,y position xys = M.transform(offsetLayout) xys -= (offsetx, offsety) xs, ys = xys[:, 0], xys[:, 1] ret = bbox, zip(lines, whs, xs, ys) self.cached[key] = ret return ret
def __init__( self, figsize=None, # defaults to rc figure.figsize dpi=None, # defaults to rc figure.dpi facecolor=None, # defaults to rc figure.facecolor edgecolor=None, # defaults to rc figure.edgecolor linewidth=0.0, # the default linewidth of the frame frameon=True, # whether or not to draw the figure frame subplotpars=None, # default to rc ): """ *figsize* w,h tuple in inches *dpi* Dots per inch *facecolor* The figure patch facecolor; defaults to rc ``figure.facecolor`` *edgecolor* The figure patch edge color; defaults to rc ``figure.edgecolor`` *linewidth* The figure patch edge linewidth; the default linewidth of the frame *frameon* If *False*, suppress drawing the figure frame *subplotpars* A :class:`SubplotParams` instance, defaults to rc """ Artist.__init__(self) self.callbacks = cbook.CallbackRegistry() if figsize is None: figsize = rcParams["figure.figsize"] if dpi is None: dpi = rcParams["figure.dpi"] if facecolor is None: facecolor = rcParams["figure.facecolor"] if edgecolor is None: edgecolor = rcParams["figure.edgecolor"] self.dpi_scale_trans = Affine2D() self.dpi = dpi self.bbox_inches = Bbox.from_bounds(0, 0, *figsize) self.bbox = TransformedBbox(self.bbox_inches, self.dpi_scale_trans) self.frameon = frameon self.transFigure = BboxTransformTo(self.bbox) # the figurePatch name is deprecated self.patch = self.figurePatch = Rectangle( xy=(0, 0), width=1, height=1, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth ) self._set_artist_props(self.patch) self.patch.set_aa(False) self._hold = rcParams["axes.hold"] self.canvas = None if subplotpars is None: subplotpars = SubplotParams() self.subplotpars = subplotpars self._axstack = AxesStack() # track all figure axes and current axes self.clf() self._cachedRenderer = None
def _get_layout(self, renderer): key = self.get_prop_tup() if self.cached.has_key(key): return self.cached[key] horizLayout = [] thisx, thisy = 0.0, 0.0 xmin, ymin = 0.0, 0.0 width, height = 0.0, 0.0 lines = self._text.split('\n') whs = np.zeros((len(lines), 2)) horizLayout = np.zeros((len(lines), 4)) # Find full vertical extent of font, # including ascenders and descenders: tmp, heightt, bl = renderer.get_text_width_height_descent( 'lp', self._fontproperties, ismath=False) offsety = heightt * self._linespacing baseline = None for i, line in enumerate(lines): w, h, d = renderer.get_text_width_height_descent( line, self._fontproperties, ismath=self.is_math_text(line)) if baseline is None: baseline = h - d whs[i] = w, h horizLayout[i] = thisx, thisy, w, h thisy -= offsety width = max(width, w) ymin = horizLayout[-1][1] ymax = horizLayout[0][1] + horizLayout[0][3] height = ymax - ymin xmax = xmin + width # get the rotation matrix M = Affine2D().rotate_deg(self.get_rotation()) offsetLayout = np.zeros((len(lines), 2)) offsetLayout[:] = horizLayout[:, 0:2] # now offset the individual text lines within the box if len(lines) > 1: # do the multiline aligment malign = self._get_multialignment() if malign == 'center': offsetLayout[:, 0] += width / 2.0 - horizLayout[:, 2] / 2.0 elif malign == 'right': offsetLayout[:, 0] += width - horizLayout[:, 2] # the corners of the unrotated bounding box cornersHoriz = np.array([(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin)], np.float_) # now rotate the bbox cornersRotated = M.transform(cornersHoriz) txs = cornersRotated[:, 0] tys = cornersRotated[:, 1] # compute the bounds of the rotated box xmin, xmax = txs.min(), txs.max() ymin, ymax = tys.min(), tys.max() width = xmax - xmin height = ymax - ymin # Now move the box to the targe position offset the display bbox by alignment halign = self._horizontalalignment valign = self._verticalalignment # compute the text location in display coords and the offsets # necessary to align the bbox with that location if halign == 'center': offsetx = (xmin + width / 2.0) elif halign == 'right': offsetx = (xmin + width) else: offsetx = xmin if valign == 'center': offsety = (ymin + height / 2.0) elif valign == 'top': offsety = (ymin + height) elif valign == 'baseline': offsety = (ymin + height) + baseline else: offsety = ymin xmin -= offsetx ymin -= offsety bbox = Bbox.from_bounds(xmin, ymin, width, height) # now rotate the positions around the first x,y position xys = M.transform(offsetLayout) xys -= (offsetx, offsety) xs, ys = xys[:, 0], xys[:, 1] ret = bbox, zip(lines, whs, xs, ys) self.cached[key] = ret return ret