matplotlib.backends.backend_agg#

AnAnti-Grain Geometry (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

Still TODO:

  • integrate screen dpi w/ ppi and text

matplotlib.backends.backend_agg.FigureCanvas[source]#

alias ofFigureCanvasAgg

classmatplotlib.backends.backend_agg.FigureCanvasAgg(figure=None)[source]#

Bases:FigureCanvasBase

buffer_rgba()[source]#

Get the image as amemoryview to the renderer's buffer.

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

copy_from_bbox(bbox)[source]#
draw()[source]#

Render theFigure.

This method must walk the artist tree, even if no output is produced,because it triggers deferred work that users may want to accessbefore saving output to disk. For example computing limits,auto-limits, and tick values.

get_renderer()[source]#
print_avif(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a AVIF file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_gif(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a GIF file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_jpeg(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a JPEG file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_jpg(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a JPEG file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_png(filename_or_obj,*,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-1encodable strings.According to the PNG specification, keys must be shorter than 79chars.

ThePNG specification defines some common keywords that may beused 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 Matplotlibwill be used. This can be removed by setting it toNone.

For more details see thePNG specification.

pil_kwargsdict, optional

Keyword arguments passed toPIL.Image.Image.save.

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

print_raw(filename_or_obj,*,metadata=None)[source]#
print_rgba(filename_or_obj,*,metadata=None)[source]#
print_tif(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a TIFF file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_tiff(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a TIFF file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

print_to_buffer()[source]#
print_webp(filename_or_obj,*,metadata=None,pil_kwargs=None)[source]#

Write the figure to a WebP file.

Parameters:
filename_or_objstr or path-like or file-like

The file to write to.

metadataNone

Unused for pillow-based writers. All supported optionscan be passed viapil_kwargs.

pil_kwargsdict, optional

Additional keyword arguments that are passed toPIL.Image.Image.save when saving the figure.

restore_region(region,bbox=None,xy=None)[source]#
tostring_argb()[source]#

Get the image as ARGBbytes.

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

classmatplotlib.backends.backend_agg.RendererAgg(width,height,dpi)[source]#

Bases:RendererBase

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

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

Draw mathtext usingmatplotlib.mathtext.

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

Draw aPath instance using the given affine transform.

draw_tex(gc,x,y,s,prop,angle,*,mtext=None)[source]#

Draw a TeX 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 TeX text string.

propFontProperties

The font properties.

anglefloat

The rotation angle in degrees anti-clockwise.

mtextText

The original text object to be rendered.

draw_text(gc,x,y,s,prop,angle,ismath=False,mtext=None)[source]#

Draw a 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.

propFontProperties

The font properties.

anglefloat

The rotation angle in degrees anti-clockwise.

ismathbool or "TeX"

If True, use mathtext parser.

mtextText

The original text object to be rendered.

Notes

Notes for backend implementers:

RendererBase.draw_text also supports passing "TeX" to theismathparameter to use TeX rendering, but this is not required for actualrendering backends, and indeed many builtin backends do not supportthis. Rather, TeX rendering is provided bydraw_tex.

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), indisplay coords, of the strings withFontPropertiesprop.

Whitespace at the start and the end ofs is included in the reported width.

option_image_nocomposite()[source]#

Return whether image composition by Matplotlib should be skipped.

Raster backends should usually return False (letting the C-levelrasterizer take care of image composition); vector backends shouldusually returnnotrcParams["image.composite_image"].

option_scale_image()[source]#

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

points_to_pixels(points)[source]#

Convert points to display units.

You need to override this function (unless your backenddoesn't have a dpi, e.g., postscript or svg). Some imagingsystems assume some value for pixels per inch:

pointstopixels=points*pixels_per_inch/72*dpi/72
Parameters:
pointsfloat or array-like
Returns:
Points converted to pixels
restore_region(region,bbox=None,xy=None)[source]#

Restore the saved region. If bbox (instance of BboxBase, orits extents) is given, only the region specified by the bboxwill be restored.xy (a pair of floats) optionallyspecifies 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 creates a new canvas (the old one is saved).

stop_filter(post_processing)[source]#

Save the current canvas as an image and apply post processing.

Thepost_processing function:

defpost_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 imagereturnnew_image,offset_x,offset_y

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

tostring_argb()[source]#
matplotlib.backends.backend_agg.get_hinting_flag()[source]#
On this page