matplotlib.backends.backend_agg

An agg backend.

Features that are implemented:

  • capstyles and join styles
  • dashes
  • linewidth
  • lines, rectangles, ellipses
  • clipping to a rectangle
  • output to RGBA and Pillow-supported image formats
  • alpha blending
  • DPI scaling properly - everything scales properly (dashes, linewidths, etc)
  • draw polygon
  • freetype2 w/ ft2font

TODO:

  • integrate screen dpi w/ ppi and text
matplotlib.backends.backend_agg.FigureCanvas

alias of matplotlib.backends.backend_agg.FigureCanvasAgg

class matplotlib.backends.backend_agg.FigureCanvasAgg(figure)[source]

Bases: matplotlib.backend_bases.FigureCanvasBase

buffer_rgba(self)[source]

Get the image as a memoryview to the renderer's buffer.

draw must be called at least once before this function will work and to update the renderer for any subsequent changes to the Figure.

copy_from_bbox(self, bbox)[source]
draw(self)[source]

Render the Figure.

get_renderer(self, cleared=False)[source]
print_jpeg(self, filename_or_obj, *args, dryrun=<deprecated parameter>, pil_kwargs=None, **kwargs)

Write the figure to a JPEG file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

Other Parameters:
qualityint, default: rcParams["savefig.jpeg_quality"] (default: 95)

The image quality, on a scale from 1 (worst) to 95 (best). Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality. This parameter is deprecated.

optimizebool, default: False

Whether the encoder should make an extra pass over the image in order to select optimal encoder settings. This parameter is deprecated.

progressivebool, default: False

Whether the image should be stored as a progressive JPEG file. This parameter is deprecated.

pil_kwargsdict, optional

Additional keyword arguments that are passed to PIL.Image.Image.save when saving the figure. These take precedence over quality, optimize and progressive.

print_jpg(self, filename_or_obj, *args, dryrun=<deprecated parameter>, pil_kwargs=None, **kwargs)[source]

Write the figure to a JPEG file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

Other Parameters:
qualityint, default: rcParams["savefig.jpeg_quality"] (default: 95)

The image quality, on a scale from 1 (worst) to 95 (best). Values above 95 should be avoided; 100 disables portions of the JPEG compression algorithm, and results in large files with hardly any gain in image quality. This parameter is deprecated.

optimizebool, default: False

Whether the encoder should make an extra pass over the image in order to select optimal encoder settings. This parameter is deprecated.

progressivebool, default: False

Whether the image should be stored as a progressive JPEG file. This parameter is deprecated.

pil_kwargsdict, optional

Additional keyword arguments that are passed to PIL.Image.Image.save when saving the figure. These take precedence over quality, optimize and progressive.

print_png(self, filename_or_obj, *args, metadata=None, pil_kwargs=None)[source]

Write the figure to a PNG file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadatadict, optional

Metadata in the PNG file as key-value pairs of bytes or latin-1 encodable strings. According to the PNG specification, keys must be shorter than 79 chars.

The PNG specification defines some common keywords that may be used as appropriate:

  • Title: Short (one line) title or caption for image.
  • Author: Name of image's creator.
  • Description: Description of image (possibly long).
  • Copyright: Copyright notice.
  • Creation Time: Time of original image creation (usually RFC 1123 format).
  • Software: Software used to create the image.
  • Disclaimer: Legal disclaimer.
  • Warning: Warning of nature of content.
  • Source: Device used to create the image.
  • Comment: Miscellaneous comment; conversion from other image format.

Other keywords may be invented for other purposes.

If 'Software' is not given, an autogenerated value for Matplotlib will be used. This can be removed by setting it to None.

For more details see the PNG specification.

pil_kwargsdict, optional

Keyword arguments passed to PIL.Image.Image.save.

If the 'pnginfo' key is present, it completely overrides metadata, including the default 'Software' key.

print_raw(self, filename_or_obj, *args)[source]
print_rgba(self, filename_or_obj, *args)
print_tif(self, filename_or_obj, *, dryrun=<deprecated parameter>, pil_kwargs=None)[source]
print_tiff(self, filename_or_obj, *, dryrun=<deprecated parameter>, pil_kwargs=None)
print_to_buffer(self)[source]
restore_region(self, region, bbox=None, xy=None)[source]
tostring_argb(self)[source]

Get the image as ARGB bytes.

draw must be called at least once before this function will work and to update the renderer for any subsequent changes to the Figure.

tostring_rgb(self)[source]

Get the image as RGB bytes.

draw must be called at least once before this function will work and to update the renderer for any subsequent changes to the Figure.

class matplotlib.backends.backend_agg.RendererAgg(width, height, dpi)[source]

Bases: matplotlib.backend_bases.RendererBase

The renderer handles all the drawing primitives using a graphics context instance that controls the colors/styles

buffer_rgba(self)[source]
clear(self)[source]
draw_mathtext(self, gc, x, y, s, prop, angle)[source]

Draw mathtext using matplotlib.mathtext.

draw_path(self, gc, path, transform, rgbFace=None)[source]

Draw a Path instance using the given affine transform.

draw_path_collection(self, gc, master_transform, paths, all_transforms, offsets, offsetTrans, facecolors, edgecolors, linewidths, linestyles, antialiaseds, urls, offset_position)[source]

Draw a collection of paths selecting drawing properties from the lists facecolors, edgecolors, linewidths, linestyles and antialiaseds. offsets is a list of offsets to apply to each of the paths. The offsets in offsets are first transformed by offsetTrans before being applied.

offset_position may be either "screen" or "data" depending on the space that the offsets are in; "data" is deprecated.

This provides a fallback implementation of draw_path_collection() that makes multiple calls to draw_path(). Some backends may want to override this in order to render each set of path data only once, and then reference that path multiple times with the different offsets, colors, styles etc. The generator methods _iter_collection_raw_paths() and _iter_collection() are provided to help with (and standardize) the implementation across backends. It is highly recommended to use those generators, so that changes to the behavior of draw_path_collection() can be made globally.

draw_tex(self, gc, x, y, s, prop, angle, ismath=<deprecated parameter>, mtext=None)[source]
draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None)[source]

Draw the text instance.

Parameters:
gcGraphicsContextBase

The graphics context.

xfloat

The x location of the text in display coords.

yfloat

The y location of the text baseline in display coords.

sstr

The text string.

propmatplotlib.font_manager.FontProperties

The font properties.

anglefloat

The rotation angle in degrees anti-clockwise.

mtextmatplotlib.text.Text

The original text object to be rendered.

Notes

Note for backend implementers:

When you are trying to determine if you have gotten your bounding box right (which is what enables the text layout/alignment to work properly), it helps to change the line in text.py:

if 0: bbox_artist(self, renderer)

to if 1, and then the actual bounding box will be plotted along with your text.

get_canvas_width_height(self)[source]

Return the canvas width and height in display coords.

get_text_width_height_descent(self, s, prop, ismath)[source]

Get the width, height, and descent (offset from the bottom to the baseline), in display coords, of the string s with FontProperties prop.

lock = <unlocked _thread.RLock object owner=0 count=0>
option_image_nocomposite(self)[source]

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"].

option_scale_image(self)[source]

Return whether arbitrary affine transformations in draw_image() are supported (True for most vector backends).

points_to_pixels(self, points)[source]

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 systems assume some value for pixels per inch:

points to pixels = points * pixels_per_inch/72 * dpi/72
Parameters:
pointsfloat or array-like

a float or a numpy array of float

Returns:
Points converted to pixels
restore_region(self, region, bbox=None, xy=None)[source]

Restore the saved region. If bbox (instance of BboxBase, or its extents) is given, only the region specified by the bbox will be restored. xy (a pair of floats) optionally specifies the new position (the LLC of the original region, not the LLC of the bbox) where the region will be restored.

>>> region = renderer.copy_from_bbox()
>>> x1, y1, x2, y2 = region.get_extents()
>>> renderer.restore_region(region, bbox=(x1+dx, y1, x2, y2),
...                         xy=(x1-dx, y1))
start_filter(self)[source]

Start filtering. It simply create a new canvas (the old one is saved).

stop_filter(self, post_processing)[source]

Save the plot in the current canvas as a image and apply the post_processing function.

def post_processing(image, dpi):
# ny, nx, depth = image.shape # image (numpy array) has RGBA channels and has a depth of 4. ... # create a new_image (numpy array of 4 channels, size can be # different). The resulting image may have offsets from # lower-left corner of the original image return new_image, offset_x, offset_y

The saved renderer is restored and the returned image from post_processing is plotted (using draw_image) on it.

tostring_argb(self)[source]
tostring_rgb(self)[source]
tostring_rgba_minimized(self)[source]
matplotlib.backends.backend_agg.get_hinting_flag()[source]