Version 3.0.3
matplotlib
Fork me on GitHub

matplotlib.backends.backend_agg

An agg http://antigrain.com/ backend

Features that are implemented

  • capstyles and join styles
  • dashes
  • linewidth
  • lines, rectangles, ellipses
  • clipping to a rectangle
  • output to RGBA and PNG, optionally JPEG and TIFF
  • 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

The canvas the figure renders into. Calls the draw and print fig methods, creates the renderers, etc...

Attributes:
figure : matplotlib.figure.Figure

A high-level Figure instance

buffer_rgba()[source]

Get the image as an RGBA byte string.

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

Returns:
bytes
copy_from_bbox(bbox)[source]
draw()[source]

Draw the figure using the renderer.

get_renderer(cleared=False)[source]
print_jpeg(filename_or_obj, *args, dryrun=False, **kwargs)

Write the figure to a JPEG file.

Parameters:
filename_or_obj : str or PathLike or file-like object

The file to write to.

Other Parameters:
quality : int

The image quality, on a scale from 1 (worst) to 100 (best). The default is rcParams["savefig.jpeg_quality"]. Values above 95 should be avoided; 100 completely disables the JPEG quantization stage.

optimize : bool

If present, indicates that the encoder should make an extra pass over the image in order to select optimal encoder settings.

progressive : bool

If present, indicates that this image should be stored as a progressive JPEG file.

print_jpg(filename_or_obj, *args, dryrun=False, **kwargs)[source]

Write the figure to a JPEG file.

Parameters:
filename_or_obj : str or PathLike or file-like object

The file to write to.

Other Parameters:
quality : int

The image quality, on a scale from 1 (worst) to 100 (best). The default is rcParams["savefig.jpeg_quality"]. Values above 95 should be avoided; 100 completely disables the JPEG quantization stage.

optimize : bool

If present, indicates that the encoder should make an extra pass over the image in order to select optimal encoder settings.

progressive : bool

If present, indicates that this image should be stored as a progressive JPEG file.

print_png(filename_or_obj, *args, **kwargs)[source]

Write the figure to a PNG file.

Parameters:
filename_or_obj : str or PathLike or file-like object

The file to write to.

metadata : dict, 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.

For more details see the PNG specification.

print_raw(filename_or_obj, *args, **kwargs)[source]
print_rgba(filename_or_obj, *args, **kwargs)
print_tif(filename_or_obj, *args, dryrun=False, **kwargs)[source]
print_tiff(filename_or_obj, *args, dryrun=False, **kwargs)
print_to_buffer()[source]
restore_region(region, bbox=None, xy=None)[source]
tostring_argb()[source]

Get the image as an ARGB byte string

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

Returns:
bytes
tostring_rgb()[source]

Get the image as an RGB byte string.

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

Returns:
bytes
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()[source]
clear()[source]
draw_mathtext(gc, x, y, s, prop, angle)[source]

Draw the math text using matplotlib.mathtext

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

Draw the path

draw_tex(gc, x, y, s, prop, angle, ismath='TeX!', mtext=None)[source]
draw_text(gc, x, y, s, prop, angle, ismath=False, mtext=None)[source]

Render the text

get_canvas_width_height()[source]

return the canvas width and height in display coords

get_text_width_height_descent(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()[source]

override this method for renderers that do not necessarily always want to rescale and composite raster images. (like SVG, PDF, or PS)

option_scale_image()[source]

agg backend doesn't support arbitrary scaling of image.

points_to_pixels(points)[source]

convert point measures to pixes using dpi and the pixels per inch of the display

restore_region(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 tuple of two floasts) 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()[source]

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

stop_filter(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()[source]
tostring_rgb()[source]
tostring_rgba_minimized()[source]
matplotlib.backends.backend_agg.get_hinting_flag()[source]