From f97126765f2ea2d954a02557a47d57bd7abcf557 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 7 Nov 2018 11:18:29 +0100 Subject: [PATCH 1/2] Cleanup Renderer/GraphicsContext docs. Removed a bunch of duplicated docstrings from Renderer subclasses (they're inherited anyways). --- lib/matplotlib/backend_bases.py | 266 ++++++++++--------------- lib/matplotlib/backends/backend_agg.py | 16 -- lib/matplotlib/backends/backend_pdf.py | 25 +-- lib/matplotlib/backends/backend_pgf.py | 7 - lib/matplotlib/backends/backend_ps.py | 49 +---- lib/matplotlib/backends/backend_svg.py | 13 +- lib/matplotlib/backends/backend_wx.py | 12 -- 7 files changed, 122 insertions(+), 266 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index d7b3a8bf076d..820afe2ec078 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -142,23 +142,24 @@ class RendererBase(object): * :meth:`draw_markers` * :meth:`draw_path_collection` * :meth:`draw_quad_mesh` - """ + def __init__(self): self._texmanager = None self._text2path = textpath.TextToPath() def open_group(self, s, gid=None): """ - Open a grouping element with label *s*. If *gid* is given, use - *gid* as the id of the group. Is only currently used by - :mod:`~matplotlib.backends.backend_svg`. + Open a grouping element with label *s* and *gid* (if set) as id. + + Only used by the SVG renderer. """ def close_group(self, s): """ Close a grouping element with label *s* - Is only currently used by :mod:`~matplotlib.backends.backend_svg` + + Only used by the SVG renderer. """ def draw_path(self, gc, path, transform, rgbFace=None): @@ -487,15 +488,18 @@ def draw_image(self, gc, x, y, im, transform=None): def option_image_nocomposite(self): """ - override this method for renderers that do not necessarily always - want to rescale and composite raster images. (like SVG, PDF, or PS) + Return whether image composition by Matplotlib should be skipped. + + Raster backends should usually return False (letting the C-level + rasterizer take care of image composition); vector backends should + usually return ``not rcParams["image.composite_image"]``. """ return False def option_scale_image(self): """ - override this method for renderers that support arbitrary affine - transformations in :meth:`draw_image` (most vector backends). + Return whether arbitrary affine transformations in :meth:`draw_image` + are supported (True for most vector backends). """ return False @@ -506,30 +510,24 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): """ - Draw the text instance + Draw the text instance. Parameters ---------- gc : `GraphicsContextBase` - the graphics context - + The graphics context. x : scalar - the x location of the text in display coords - + The x location of the text in display coords. y : scalar - the y location of the text baseline in display coords - + The y location of the text baseline in display coords. s : str - the text string - + The text string. prop : `matplotlib.font_manager.FontProperties` - font properties - + The font properties. angle : scalar - the rotation angle in degrees - + The rotation angle in degrees. mtext : `matplotlib.text.Text` - the original text object to be rendered + The original text object to be rendered. Notes ----- @@ -549,21 +547,18 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): def _get_text_path_transform(self, x, y, s, prop, angle, ismath): """ - return the text path and transform + Return the text path and transform. Parameters ---------- prop : `matplotlib.font_manager.FontProperties` - font property - + The font property. s : str - text to be converted - + The text to be converted. usetex : bool - If True, use matplotlib usetex mode. - - ismath : bool - If True, use mathtext parser. If "TeX", use *usetex* mode. + Whether to use matplotlib usetex mode. + ismath : bool or "TeX" + If True, use mathtext parser. If "TeX", use *usetex* mode. """ text2path = self._text2path @@ -591,26 +586,22 @@ def _get_text_path_transform(self, x, y, s, prop, angle, ismath): def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath): """ - draw the text by converting them to paths using textpath module. + Draw the text by converting them to paths using textpath module. Parameters ---------- prop : `matplotlib.font_manager.FontProperties` - font property - + The font property. s : str - text to be converted - + The text to be converted. usetex : bool - If True, use matplotlib usetex mode. - - ismath : bool - If True, use mathtext parser. If "TeX", use *usetex* mode. + Whether to use matplotlib usetex mode. + ismath : bool or "TeX" + If True, use mathtext parser. If "TeX", use *usetex* mode. """ path, transform = self._get_text_path_transform( x, y, s, prop, angle, ismath) color = gc.get_rgb() - gc.set_linewidth(0.0) self.draw_path(gc, path, transform, rgbFace=color) @@ -649,34 +640,30 @@ def get_text_width_height_descent(self, s, prop, ismath): def flipy(self): """ - Return true if y small numbers are top for renderer Is used - for drawing text (:mod:`matplotlib.text`) and images - (:mod:`matplotlib.image`) only + Return whether y values increase from top to bottom. + + Note that this only affects drawing of texts and images. """ return True def get_canvas_width_height(self): - 'return the canvas width and height in display coords' + """Return the canvas width and height in display coords.""" return 1, 1 def get_texmanager(self): - """ - return the :class:`matplotlib.texmanager.TexManager` instance - """ + """Return the `.TexManager` instance.""" if self._texmanager is None: from matplotlib.texmanager import TexManager self._texmanager = TexManager() return self._texmanager def new_gc(self): - """ - Return an instance of a :class:`GraphicsContextBase` - """ + """Return an instance of a `GraphicsContextBase`.""" return GraphicsContextBase() def points_to_pixels(self, points): """ - Convert points to display units + Convert points to display units. You need to override this function (unless your backend doesn't have a dpi, e.g., postscript or svg). Some imaging @@ -701,35 +688,38 @@ def strip_math(self, s): def start_rasterizing(self): """ - Used in MixedModeRenderer. Switch to the raster renderer. + Switch to the raster renderer. + + Used by `MixedModeRenderer`. """ def stop_rasterizing(self): """ - Used in MixedModeRenderer. Switch back to the vector renderer - and draw the contents of the raster renderer as an image on - the vector renderer. + Switch back to the vector renderer and draw the contents of the raster + renderer as an image on the vector renderer. + + Used by `MixedModeRenderer`. """ def start_filter(self): """ - Used in AggRenderer. Switch to a temporary renderer for image - filtering effects. + Switch to a temporary renderer for image filtering effects. + + Currently only supported by the agg renderer. """ def stop_filter(self, filter_func): """ - Used in AggRenderer. Switch back to the original renderer. - The contents of the temporary renderer is processed with the - *filter_func* and is drawn on the original renderer as an - image. + Switch back to the original renderer. The contents of the temporary + renderer is processed with the *filter_func* and is drawn on the + original renderer as an image. + + Currently only supported by the agg renderer. """ class GraphicsContextBase(object): - """ - An abstract base class that provides color, line styles, etc... - """ + """An abstract base class that provides color, line styles, etc.""" def __init__(self): self._alpha = 1.0 @@ -775,30 +765,29 @@ def copy_properties(self, gc): def restore(self): """ Restore the graphics context from the stack - needed only - for backends that save graphics contexts on a stack + for backends that save graphics contexts on a stack. """ def get_alpha(self): """ Return the alpha value used for blending - not supported on - all backends + all backends. """ return self._alpha def get_antialiased(self): - "Return true if the object should try to do antialiased rendering" + "Return whether the object should try to do antialiased rendering." return self._antialiased def get_capstyle(self): """ - Return the capstyle as a string in ('butt', 'round', 'projecting') + Return the capstyle as a string in ('butt', 'round', 'projecting'). """ return self._capstyle def get_clip_rectangle(self): """ - Return the clip rectangle as a :class:`~matplotlib.transforms.Bbox` - instance + Return the clip rectangle as a `~matplotlib.transforms.Bbox` instance. """ return self._cliprect @@ -835,51 +824,40 @@ def get_forced_alpha(self): return self._forced_alpha def get_joinstyle(self): - """ - Return the line join style as one of ('miter', 'round', 'bevel') - """ + """Return the line join style as one of ('miter', 'round', 'bevel').""" return self._joinstyle def get_linewidth(self): - """ - Return the line width in points as a scalar - """ + """Return the line width in points.""" return self._linewidth def get_rgb(self): - """ - returns a tuple of three or four floats from 0-1. - """ + """Return a tuple of three or four floats from 0-1.""" return self._rgb def get_url(self): - """ - returns a url if one is set, None otherwise - """ + """Return a url if one is set, None otherwise.""" return self._url def get_gid(self): - """ - Return the object identifier if one is set, None otherwise. - """ + """Return the object identifier if one is set, None otherwise.""" return self._gid def get_snap(self): """ - returns the snap setting which may be: - - * True: snap vertices to the nearest pixel center + Returns the snap setting, which can be: - * False: leave vertices as-is - - * None: (auto) If the path contains only rectilinear line - segments, round to the nearest pixel center + * True: snap vertices to the nearest pixel center + * False: leave vertices as-is + * None: (auto) If the path contains only rectilinear line segments, + round to the nearest pixel center """ return self._snap def set_alpha(self, alpha): """ Set the alpha value used for blending - not supported on all backends. + If ``alpha=None`` (the default), the alpha components of the foreground and fill colors will be used to set their respective transparencies (where applicable); otherwise, ``alpha`` will override @@ -894,20 +872,12 @@ def set_alpha(self, alpha): self.set_foreground(self._rgb, isRGBA=True) def set_antialiased(self, b): - """ - True if object should be drawn with antialiased rendering - """ - - # use 0, 1 to make life easier on extension code trying to read the gc - if b: - self._antialiased = 1 - else: - self._antialiased = 0 + """Set whether object should be drawn with antialiased rendering.""" + # Use ints to make life easier on extension code trying to read the gc. + self._antialiased = int(bool(b)) def set_capstyle(self, cs): - """ - Set the capstyle as a string in ('butt', 'round', 'projecting') - """ + """Set the capstyle to be one of ('butt', 'round', 'projecting').""" if cs in ('butt', 'round', 'projecting'): self._capstyle = cs else: @@ -969,87 +939,65 @@ def set_foreground(self, fg, isRGBA=False): self._rgb = colors.to_rgba(fg) def set_joinstyle(self, js): - """ - Set the join style to be one of ('miter', 'round', 'bevel') - """ + """Set the join style to be one of ('miter', 'round', 'bevel').""" if js in ('miter', 'round', 'bevel'): self._joinstyle = js else: raise ValueError('Unrecognized join style. Found %s' % js) def set_linewidth(self, w): - """ - Set the linewidth in points - """ + """Set the linewidth in points.""" self._linewidth = float(w) def set_url(self, url): - """ - Sets the url for links in compatible backends - """ + """Set the url for links in compatible backends.""" self._url = url def set_gid(self, id): - """ - Sets the id. - """ + """Set the id.""" self._gid = id def set_snap(self, snap): """ - Sets the snap setting which may be: + Set the snap setting which may be: - * True: snap vertices to the nearest pixel center - - * False: leave vertices as-is - - * None: (auto) If the path contains only rectilinear line - segments, round to the nearest pixel center + * True: snap vertices to the nearest pixel center + * False: leave vertices as-is + * None: (auto) If the path contains only rectilinear line segments, + round to the nearest pixel center """ self._snap = snap def set_hatch(self, hatch): - """ - Sets the hatch style for filling - """ + """Set the hatch style (for fills).""" self._hatch = hatch def get_hatch(self): - """ - Gets the current hatch style - """ + """Get the current hatch style.""" return self._hatch def get_hatch_path(self, density=6.0): - """ - Returns a Path for the current hatch. - """ + """Return a `Path` for the current hatch.""" hatch = self.get_hatch() if hatch is None: return None return Path.hatch(hatch, density) def get_hatch_color(self): - """ - Gets the color to use for hatching. - """ + """Get the hatch color.""" return self._hatch_color def set_hatch_color(self, hatch_color): - """ - sets the color to use for hatching. - """ + """Set the hatch color.""" self._hatch_color = hatch_color def get_hatch_linewidth(self): - """ - Gets the linewidth to use for hatching. - """ + """Get the hatch linewidth.""" return self._hatch_linewidth def get_sketch_params(self): """ - Returns the sketch parameters for the artist. + Return the sketch parameters for the artist. Returns ------- @@ -1057,13 +1005,11 @@ def get_sketch_params(self): A 3-tuple with the following elements: - * `scale`: The amplitude of the wiggle perpendicular to the - source line. - - * `length`: The length of the wiggle along the line. - - * `randomness`: The scale factor by which the length is - shrunken or expanded. + * `scale`: The amplitude of the wiggle perpendicular to the + source line. + * `length`: The length of the wiggle along the line. + * `randomness`: The scale factor by which the length is + shrunken or expanded. May return `None` if no sketch parameters were set. """ @@ -1071,23 +1017,19 @@ def get_sketch_params(self): def set_sketch_params(self, scale=None, length=None, randomness=None): """ - Sets the sketch parameters. + Set the sketch parameters. Parameters ---------- - scale : float, optional - The amplitude of the wiggle perpendicular to the source - line, in pixels. If scale is `None`, or not provided, no - sketch filter will be provided. - + The amplitude of the wiggle perpendicular to the source line, in + pixels. If scale is `None`, or not provided, no sketch filter will + be provided. length : float, optional - The length of the wiggle along the line, in pixels - (default 128) - + The length of the wiggle along the line, in pixels (default 128). randomness : float, optional - The scale factor by which the length is shrunken or - expanded (default 16) + The scale factor by which the length is shrunken or expanded + (default 16). """ self._sketch = ( None if scale is None diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index fd262c2bb710..cbf0f5ac3f19 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -162,9 +162,6 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): - """ - Render the text - """ if ismath: return self.draw_mathtext(gc, x, y, s, prop, angle) @@ -192,11 +189,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): font, np.round(x - xd + xo), np.round(y + yd + yo) + 1, angle, gc) def get_text_width_height_descent(self, s, prop, ismath): - """ - Get the width, height, and descent (offset from the bottom - to the baseline), in display coords, of the string *s* with - :class:`~matplotlib.font_manager.FontProperties` *prop* - """ if ismath in ["TeX", "TeX!"]: # todo: handle props size = prop.get_size_in_points() @@ -239,7 +231,6 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): self._renderer.draw_text_image(Z, x, y, angle, gc) def get_canvas_width_height(self): - 'return the canvas width and height in display coords' return self.width, self.height def _get_agg_font(self, prop): @@ -256,10 +247,6 @@ def _get_agg_font(self, prop): return font def points_to_pixels(self, points): - """ - convert point measures to pixes using dpi and the pixels per - inch of the display - """ return points * self.dpi / 72 def tostring_rgb(self): @@ -281,9 +268,6 @@ def option_image_nocomposite(self): return True def option_scale_image(self): - """ - agg backend doesn't support arbitrary scaling of image. - """ return False def restore_region(self, region, bbox=None, xy=None): diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index d046b0cf42b7..7b4198dd73d0 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1620,8 +1620,7 @@ def check_gc(self, gc, fillcolor=None): gc._effective_alphas = orig_alphas def track_characters(self, font, s): - """Keeps track of which characters are required from - each font.""" + """Keeps track of which characters are required from each font.""" if isinstance(font, str): fname = font else: @@ -1641,16 +1640,9 @@ def get_image_magnification(self): return self.image_dpi/72.0 def option_scale_image(self): - """ - pdf backend support arbitrary scaling of image. - """ return True def option_image_nocomposite(self): - """ - return whether to generate a composite image from multiple images on - a set of axes - """ return not rcParams['image.composite_image'] def draw_image(self, gc, x, y, im, transform=None): @@ -2011,9 +2003,11 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): fonttype = 42 def check_simple_method(s): - """Determine if we should use the simple or woven method - to output this text, and chunks the string into 1-byte and - 2-byte sections if necessary.""" + """ + Determine if we should use the simple or woven method to output + this text, and chunks the string into 1-byte and 2-byte sections if + necessary. + """ use_simple_method = True chunks = [] @@ -2046,9 +2040,10 @@ def draw_text_simple(): Op.end_text) def draw_text_woven(chunks): - """Outputs text using the woven method, alternating - between chunks of 1-byte characters and 2-byte characters. - Only used for Type 3 fonts.""" + """ + Outputs text using the woven method, alternating between chunks of + 1-byte and 2-byte characters. Only used for Type 3 fonts. + """ chunks = [(a, ''.join(b)) for a, b in chunks] # Do the rotation and global translation as a single matrix diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index c62dc7ec1f67..335122a26d2a 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -608,16 +608,9 @@ def _pgf_path_draw(self, stroke=True, fill=False): writeln(self.fh, r"\pgfusepath{%s}" % ",".join(actions)) def option_scale_image(self): - """ - pgf backend supports affine transform of image. - """ return True def option_image_nocomposite(self): - """ - return whether to generate a composite image from multiple images on - a set of axes - """ return not rcParams['image.composite_image'] def draw_image(self, gc, x, y, im, transform=None): diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index 2d1c75db2cd9..e2b8d1f78a9c 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -165,7 +165,8 @@ def quote_ps_string(s): def _move_path_to_path_or_stream(src, dst): - """Move the contents of file at *src* to path-or-filelike *dst*. + """ + Move the contents of file at *src* to path-or-filelike *dst*. If *dst* is a path, the metadata of *src* are *not* copied. """ @@ -188,11 +189,9 @@ class RendererPS(RendererBase): afmfontd = maxdict(50) def __init__(self, width, height, pswriter, imagedpi=72): - """ - Although postscript itself is dpi independent, we need to - imform the image code about a requested dpi to generate high - res images and them scale them before embeddin them - """ + # Although postscript itself is dpi independent, we need to imform the + # image code about a requested dpi to generate high resolution images + # and them scale them before embedding them. RendererBase.__init__(self) self.width = width self.height = height @@ -222,8 +221,7 @@ def __init__(self, width, height, pswriter, imagedpi=72): rcParams['datapath'], 'fonts', 'afm') def track_characters(self, font, s): - """Keeps track of which characters are required from - each font.""" + """Keeps track of which characters are required from each font.""" realpath, stat_key = get_realpath_and_stat(font.fname) used_characters = self.used_characters.setdefault( stat_key, (realpath, set())) @@ -326,15 +324,9 @@ def create_hatch(self, hatch): return name def get_canvas_width_height(self): - 'return the canvas width and height in display coords' return self.width * 72.0, self.height * 72.0 def get_text_width_height_descent(self, s, prop, ismath): - """ - get the width and height in display coords of the string s - with FontPropertry prop - - """ if rcParams['text.usetex']: texmanager = self.get_texmanager() fontsize = prop.get_size_in_points() @@ -370,7 +362,6 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, d def flipy(self): - 'return true if small y numbers are top for renderer' return False def _get_font_afm(self, prop): @@ -420,16 +411,9 @@ def get_image_magnification(self): return self.image_magnification def option_scale_image(self): - """ - ps backend support arbitrary scaling of image. - """ return True def option_image_nocomposite(self): - """ - return whether to generate a composite image from multiple images on - a set of axes - """ return not rcParams['image.composite_image'] def _get_image_h_w_bits_command(self, im): @@ -439,12 +423,6 @@ def _get_image_h_w_bits_command(self, im): return h, w, bits, imagecmd def draw_image(self, gc, x, y, im, transform=None): - """ - Draw the Image instance into the current axes; x is the - distance in pixels from the left hand side of the canvas and y - is the distance from bottom - """ - h, w, bits, imagecmd = self._get_image_h_w_bits_command(im) hexlines = b'\n'.join(self._hex_lines(bits)).decode('ascii') @@ -510,9 +488,6 @@ def _get_clip_path(self, clippath, clippath_transform): return pid def draw_path(self, gc, path, transform, rgbFace=None): - """ - Draws a Path instance using the given affine transform. - """ clip = rgbFace is None and gc.get_hatch_path() is None simplify = path.should_simplify and clip ps = self._convert_path(path, transform, clip=clip, simplify=simplify) @@ -520,11 +495,7 @@ def draw_path(self, gc, path, transform, rgbFace=None): def draw_markers( self, gc, marker_path, marker_trans, path, trans, rgbFace=None): - """ - Draw the markers defined by path at each of the positions in x - and y. path coordinates are points, x and y coords will be - transformed by the transform - """ + if debugPS: self._pswriter.write('% draw_markers \n') @@ -624,9 +595,6 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms, self._path_collection_id += 1 def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): - """ - draw a Text instance - """ w, h, bl = self.get_text_width_height_descent(s, prop, ismath) fontsize = prop.get_size_in_points() thetext = 'psmarker%d' % self.textcnt @@ -664,9 +632,6 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): self.textcnt += 1 def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): - """ - Draw a Text instance. - """ # local to avoid repeated attribute lookups write = self._pswriter.write if debugPS: diff --git a/lib/matplotlib/backends/backend_svg.py b/lib/matplotlib/backends/backend_svg.py index 5e45a24f22d1..7985a9d40e3e 100644 --- a/lib/matplotlib/backends/backend_svg.py +++ b/lib/matplotlib/backends/backend_svg.py @@ -409,10 +409,7 @@ def _write_hatches(self): writer.end('defs') def _get_style_dict(self, gc, rgbFace): - """ - return the style string. style is generated from the - GraphicsContext and rgbFace - """ + """Generate a style string from the GraphicsContext and rgbFace.""" attrib = {} forced_alpha = gc.get_forced_alpha() @@ -543,10 +540,6 @@ def _write_svgfonts(self): writer.end('defs') def open_group(self, s, gid=None): - """ - Open a grouping element with label *s*. If *gid* is given, use - *gid* as the id of the group. - """ if gid: self.writer.start('g', id=gid) else: @@ -557,10 +550,6 @@ def close_group(self, s): self.writer.end('g') def option_image_nocomposite(self): - """ - return whether to generate a composite image from multiple images on - a set of axes - """ return not rcParams['image.composite_image'] def _convert_path(self, path, transform=None, clip=None, simplify=None, diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 182424ffeda9..cd5ae387d24d 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -218,10 +218,6 @@ def offset_text_height(self): return True def get_text_width_height_descent(self, s, prop, ismath): - """ - get the width and height in display coords of the string s - with FontPropertry prop - """ if ismath: s = cbook.strip_math(s) @@ -237,7 +233,6 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, descent def get_canvas_width_height(self): - 'return the canvas width and height in display coords' return self.width, self.height def handle_clip_rectangle(self, gc): @@ -328,9 +323,6 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): gc.unselect() def new_gc(self): - """ - Return an instance of a GraphicsContextWx, and sets the current gc copy - """ DEBUG_MSG('new_gc()', 2, self) self.gc = GraphicsContextWx(self.bitmap, self) self.gc.select() @@ -382,10 +374,6 @@ def get_wx_font(self, s, prop): return font def points_to_pixels(self, points): - """ - convert point measures to pixes using dpi and the pixels per - inch of the display - """ return points * (PIXELS_PER_INCH / 72.0 * self.dpi / 72.0) From 6de75f37d41350fa6a69e8c6cb5067d416ea584e Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 9 Nov 2018 02:16:03 +0100 Subject: [PATCH 2/2] Add docstring inherited messages --- lib/matplotlib/backends/backend_agg.py | 11 +++++++++++ lib/matplotlib/backends/backend_cairo.py | 11 ++++++++++- lib/matplotlib/backends/backend_pdf.py | 15 +++++++++++++++ lib/matplotlib/backends/backend_pgf.py | 16 ++++++++++++++++ lib/matplotlib/backends/backend_ps.py | 15 +++++++++++++++ lib/matplotlib/backends/backend_svg.py | 15 +++++++++++++++ lib/matplotlib/backends/backend_template.py | 3 +++ lib/matplotlib/backends/backend_wx.py | 9 +++++++++ lib/matplotlib/patheffects.py | 2 ++ 9 files changed, 96 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index cbf0f5ac3f19..2c9d1566f335 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -118,6 +118,7 @@ def tostring_rgba_minimized(self): return np.array(region), extents def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited nmax = rcParams['agg.path.chunksize'] # here at least for testing npts = path.vertices.shape[0] @@ -162,6 +163,8 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + if ismath: return self.draw_mathtext(gc, x, y, s, prop, angle) @@ -189,6 +192,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): font, np.round(x - xd + xo), np.round(y + yd + yo) + 1, angle, gc) def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + if ismath in ["TeX", "TeX!"]: # todo: handle props size = prop.get_size_in_points() @@ -214,6 +219,7 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, d def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): + # docstring inherited # todo, handle props, angle, origins size = prop.get_size_in_points() @@ -231,6 +237,7 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): self._renderer.draw_text_image(Z, x, y, angle, gc) def get_canvas_width_height(self): + # docstring inherited return self.width, self.height def _get_agg_font(self, prop): @@ -247,6 +254,7 @@ def _get_agg_font(self, prop): return font def points_to_pixels(self, points): + # docstring inherited return points * self.dpi / 72 def tostring_rgb(self): @@ -262,12 +270,15 @@ def clear(self): self._renderer.clear() def option_image_nocomposite(self): + # docstring inherited + # It is generally faster to composite each image directly to # the Figure, and there's no file size benefit to compositing # with the Agg backend return True def option_scale_image(self): + # docstring inherited return False def restore_region(self, region, bbox=None, xy=None): diff --git a/lib/matplotlib/backends/backend_cairo.py b/lib/matplotlib/backends/backend_cairo.py index ce68d198fe67..d52febe04ee0 100644 --- a/lib/matplotlib/backends/backend_cairo.py +++ b/lib/matplotlib/backends/backend_cairo.py @@ -225,6 +225,7 @@ def convert_path(ctx, path, transform, clip=None): _append_path(ctx, path, transform, clip) def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited ctx = gc.ctx # Clip the path to the actual rendering extents if it isn't filled. clip = (ctx.clip_extents() @@ -239,8 +240,9 @@ def draw_path(self, gc, path, transform, rgbFace=None): def draw_markers(self, gc, marker_path, marker_trans, path, transform, rgbFace=None): - ctx = gc.ctx + # docstring inherited + ctx = gc.ctx ctx.new_path() # Create the path for the marker; it needs to be flipped here already! _append_path(ctx, marker_path, marker_trans + Affine2D().scale(1, -1)) @@ -349,6 +351,8 @@ def draw_image(self, gc, x, y, im): ctx.restore() def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + # Note: x,y are device/display coords, not user-coords, unlike other # draw_* methods if ismath: @@ -404,9 +408,12 @@ def _draw_mathtext(self, gc, x, y, s, prop, angle): ctx.restore() def get_canvas_width_height(self): + # docstring inherited return self.width, self.height def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + if ismath: width, height, descent, fonts, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) @@ -433,12 +440,14 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, h + y_bearing def new_gc(self): + # docstring inherited self.gc.ctx.save() self.gc._alpha = 1 self.gc._forced_alpha = False # if True, _alpha overrides A from RGBA return self.gc def points_to_pixels(self, points): + # docstring inherited return points / 72 * self.dpi diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 7b4198dd73d0..74b23608a5ee 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1640,12 +1640,16 @@ def get_image_magnification(self): return self.image_dpi/72.0 def option_scale_image(self): + # docstring inherited return True def option_image_nocomposite(self): + # docstring inherited return not rcParams['image.composite_image'] def draw_image(self, gc, x, y, im, transform=None): + # docstring inherited + h, w = im.shape[:2] if w == 0 or h == 0: return @@ -1674,6 +1678,7 @@ def draw_image(self, gc, x, y, im, transform=None): imob, Op.use_xobject, Op.grestore) def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited self.check_gc(gc, rgbFace) self.file.writePath( path, transform, @@ -1753,6 +1758,8 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms, def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None): + # docstring inherited + # Same logic as in draw_path_collection len_marker_path = len(marker_path) uses = len(path) @@ -1885,6 +1892,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle): self.file.output(Op.grestore) def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): + # docstring inherited texmanager = self.get_texmanager() fontsize = prop.get_size_in_points() dvifile = texmanager.make_dvi(s, fontsize) @@ -1968,6 +1976,8 @@ def encode_string(self, s, fonttype): return s.encode('utf-16be', 'replace') def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + # TODO: combine consecutive texts into one BT/ET delimited section # This function is rather complex, since there is no way to @@ -2111,6 +2121,8 @@ def draw_text_woven(chunks): return draw_text_woven(chunks) def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + if rcParams['text.usetex']: texmanager = self.get_texmanager() fontsize = prop.get_size_in_points() @@ -2166,12 +2178,15 @@ def _get_font_ttf(self, prop): return font def flipy(self): + # docstring inherited return False def get_canvas_width_height(self): + # docstring inherited return self.file.width * 72.0, self.file.height * 72.0 def new_gc(self): + # docstring inherited return GraphicsContextPdf(self.file) diff --git a/lib/matplotlib/backends/backend_pgf.py b/lib/matplotlib/backends/backend_pgf.py index 335122a26d2a..600c27369a84 100644 --- a/lib/matplotlib/backends/backend_pgf.py +++ b/lib/matplotlib/backends/backend_pgf.py @@ -406,6 +406,8 @@ def __init__(self, figure, fh, dummy=False): def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None): + # docstring inherited + writeln(self.fh, r"\begin{pgfscope}") # convert from display units to in @@ -437,6 +439,7 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, writeln(self.fh, r"\end{pgfscope}") def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited writeln(self.fh, r"\begin{pgfscope}") # draw the path self._print_pgf_clip(gc) @@ -608,12 +611,16 @@ def _pgf_path_draw(self, stroke=True, fill=False): writeln(self.fh, r"\pgfusepath{%s}" % ",".join(actions)) def option_scale_image(self): + # docstring inherited return True def option_image_nocomposite(self): + # docstring inherited return not rcParams['image.composite_image'] def draw_image(self, gc, x, y, im, transform=None): + # docstring inherited + h, w = im.shape[:2] if w == 0 or h == 0: return @@ -648,9 +655,12 @@ def draw_image(self, gc, x, y, im, transform=None): writeln(self.fh, r"\end{pgfscope}") def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None): + # docstring inherited self.draw_text(gc, x, y, s, prop, angle, ismath, mtext) def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + # prepare string for tex s = common_texification(s) prop_cmds = _font_properties_str(prop) @@ -700,6 +710,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): writeln(self.fh, r"\end{pgfscope}") def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + # check if the math is supposed to be displaystyled s = common_texification(s) @@ -712,15 +724,19 @@ def get_text_width_height_descent(self, s, prop, ismath): return w * f, h * f, d * f def flipy(self): + # docstring inherited return False def get_canvas_width_height(self): + # docstring inherited return self.figure.get_figwidth(), self.figure.get_figheight() def points_to_pixels(self, points): + # docstring inherited return points * mpl_pt_to_in * self.dpi def new_gc(self): + # docstring inherited return GraphicsContextPgf() diff --git a/lib/matplotlib/backends/backend_ps.py b/lib/matplotlib/backends/backend_ps.py index e2b8d1f78a9c..c58ff927a604 100644 --- a/lib/matplotlib/backends/backend_ps.py +++ b/lib/matplotlib/backends/backend_ps.py @@ -324,9 +324,12 @@ def create_hatch(self, hatch): return name def get_canvas_width_height(self): + # docstring inherited return self.width * 72.0, self.height * 72.0 def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + if rcParams['text.usetex']: texmanager = self.get_texmanager() fontsize = prop.get_size_in_points() @@ -362,6 +365,7 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, d def flipy(self): + # docstring inherited return False def _get_font_afm(self, prop): @@ -411,9 +415,11 @@ def get_image_magnification(self): return self.image_magnification def option_scale_image(self): + # docstring inherited return True def option_image_nocomposite(self): + # docstring inherited return not rcParams['image.composite_image'] def _get_image_h_w_bits_command(self, im): @@ -423,6 +429,8 @@ def _get_image_h_w_bits_command(self, im): return h, w, bits, imagecmd def draw_image(self, gc, x, y, im, transform=None): + # docstring inherited + h, w, bits, imagecmd = self._get_image_h_w_bits_command(im) hexlines = b'\n'.join(self._hex_lines(bits)).decode('ascii') @@ -488,6 +496,7 @@ def _get_clip_path(self, clippath, clippath_transform): return pid def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited clip = rgbFace is None and gc.get_hatch_path() is None simplify = path.should_simplify and clip ps = self._convert_path(path, transform, clip=clip, simplify=simplify) @@ -495,6 +504,7 @@ def draw_path(self, gc, path, transform, rgbFace=None): def draw_markers( self, gc, marker_path, marker_trans, path, trans, rgbFace=None): + # docstring inherited if debugPS: self._pswriter.write('% draw_markers \n') @@ -595,6 +605,8 @@ def draw_path_collection(self, gc, master_transform, paths, all_transforms, self._path_collection_id += 1 def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): + # docstring inherited + w, h, bl = self.get_text_width_height_descent(s, prop, ismath) fontsize = prop.get_size_in_points() thetext = 'psmarker%d' % self.textcnt @@ -632,6 +644,8 @@ def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): self.textcnt += 1 def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + # local to avoid repeated attribute lookups write = self._pswriter.write if debugPS: @@ -734,6 +748,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): self._pswriter.write(ps) def new_gc(self): + # docstring inherited return GraphicsContextPS() def draw_mathtext(self, gc, x, y, s, prop, angle): diff --git a/lib/matplotlib/backends/backend_svg.py b/lib/matplotlib/backends/backend_svg.py index 7985a9d40e3e..3eae41b78b71 100644 --- a/lib/matplotlib/backends/backend_svg.py +++ b/lib/matplotlib/backends/backend_svg.py @@ -540,6 +540,7 @@ def _write_svgfonts(self): writer.end('defs') def open_group(self, s, gid=None): + # docstring inherited if gid: self.writer.start('g', id=gid) else: @@ -547,9 +548,11 @@ def open_group(self, s, gid=None): self.writer.start('g', id="%s_%d" % (s, self._groupd[s])) def close_group(self, s): + # docstring inherited self.writer.end('g') def option_image_nocomposite(self): + # docstring inherited return not rcParams['image.composite_image'] def _convert_path(self, path, transform=None, clip=None, simplify=None, @@ -563,6 +566,7 @@ def _convert_path(self, path, transform=None, clip=None, simplify=None, [b'M', b'L', b'Q', b'C', b'z'], False).decode('ascii') def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited trans_and_flip = self._make_flip_transform(transform) clip = (rgbFace is None and gc.get_hatch_path() is None) simplify = path.should_simplify and clip @@ -585,6 +589,8 @@ def draw_path(self, gc, path, transform, rgbFace=None): def draw_markers( self, gc, marker_path, marker_trans, path, trans, rgbFace=None): + # docstring inherited + if not len(path.vertices): return @@ -828,12 +834,15 @@ def draw_gouraud_triangles(self, gc, triangles_array, colors_array, self.writer.end('g') def option_scale_image(self): + # docstring inherited return True def get_image_magnification(self): return self.image_dpi / 72.0 def draw_image(self, gc, x, y, im, transform=None): + # docstring inherited + h, w = im.shape[:2] if w == 0 or h == 0: @@ -1179,9 +1188,12 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None): writer.end('g') def draw_tex(self, gc, x, y, s, prop, angle, ismath='TeX!', mtext=None): + # docstring inherited self._draw_text_as_path(gc, x, y, s, prop, angle, ismath="TeX") def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + clipid = self._get_clip(gc) if clipid is not None: # Cannot apply clip-path directly to the text, because @@ -1204,12 +1216,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): self.writer.end('g') def flipy(self): + # docstring inherited return True def get_canvas_width_height(self): + # docstring inherited return self.width, self.height def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited return self._text2path.get_text_width_height_descent(s, prop, ismath) diff --git a/lib/matplotlib/backends/backend_template.py b/lib/matplotlib/backends/backend_template.py index a4ae747816f0..4551b949ffc0 100644 --- a/lib/matplotlib/backends/backend_template.py +++ b/lib/matplotlib/backends/backend_template.py @@ -113,15 +113,18 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): pass def flipy(self): + # docstring inherited return True def get_canvas_width_height(self): + # docstring inherited return 100, 100 def get_text_width_height_descent(self, s, prop, ismath): return 1, 1, 1 def new_gc(self): + # docstring inherited return GraphicsContextTemplate() def points_to_pixels(self, points): diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index cd5ae387d24d..7d764eb29b8f 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -212,12 +212,15 @@ def __init__(self, bitmap, dpi): self.gc = None def flipy(self): + # docstring inherited return True def offset_text_height(self): return True def get_text_width_height_descent(self, s, prop, ismath): + # docstring inherited + if ismath: s = cbook.strip_math(s) @@ -233,6 +236,7 @@ def get_text_width_height_descent(self, s, prop, ismath): return w, h, descent def get_canvas_width_height(self): + # docstring inherited return self.width, self.height def handle_clip_rectangle(self, gc): @@ -266,6 +270,7 @@ def convert_path(gfx_ctx, path, transform): return wxpath def draw_path(self, gc, path, transform, rgbFace=None): + # docstring inherited gc.select() self.handle_clip_rectangle(gc) gfx_ctx = gc.gfx_ctx @@ -297,6 +302,8 @@ def draw_image(self, gc, x, y, im): gc.unselect() def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): + # docstring inherited + if ismath: s = cbook.strip_math(s) DEBUG_MSG("draw_text()", 1, self) @@ -323,6 +330,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): gc.unselect() def new_gc(self): + # docstring inherited DEBUG_MSG('new_gc()', 2, self) self.gc = GraphicsContextWx(self.bitmap, self) self.gc.select() @@ -374,6 +382,7 @@ def get_wx_font(self, s, prop): return font def points_to_pixels(self, points): + # docstring inherited return points * (PIXELS_PER_INCH / 72.0 * self.dpi / 72.0) diff --git a/lib/matplotlib/patheffects.py b/lib/matplotlib/patheffects.py index a7ad39b17886..63ad0899f05a 100644 --- a/lib/matplotlib/patheffects.py +++ b/lib/matplotlib/patheffects.py @@ -94,6 +94,7 @@ def __init__(self, path_effects, renderer): self._renderer = renderer def new_gc(self): + # docstring inherited return self._renderer.new_gc() def copy_with_path_effect(self, path_effects): @@ -143,6 +144,7 @@ def draw_path_collection(self, gc, master_transform, paths, *args, *args, **kwargs) def points_to_pixels(self, points): + # docstring inherited return self._renderer.points_to_pixels(points) def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath):