matplotlib.backends.backend_template
#
A fully functional, do-nothing backend intended as a template for backendwriters. It is fully functional in that you can select it as a backend e.g.with
importmatplotlibmatplotlib.use("template")
and your program will (should!) run without error, though no output isproduced. This provides a starting point for backend writers; you canselectively implement drawing methods (draw_path
,draw_image
, etc.) and slowly see your figure come to lifeinstead having to have a full-blown implementation before getting any results.
Copy this file to a directory outside the Matplotlib source tree, somewherewhere Python can import it (by adding the directory to yoursys.path
or bypackaging it as a normal Python package); if the backend is importable asimportmy.backend
you can then select it using
importmatplotlibmatplotlib.use("module://my.backend")
If your backend implements support for saving figures (i.e. has aprint_xyz
method),you can register it as the default handler for a given file type:
frommatplotlib.backend_basesimportregister_backendregister_backend('xyz','my_backend','XYZ File Format')...plt.savefig("figure.xyz")
- matplotlib.backends.backend_template.FigureCanvas[source]#
alias of
FigureCanvasTemplate
- classmatplotlib.backends.backend_template.FigureCanvasTemplate(figure=None)[source]#
Bases:
FigureCanvasBase
The canvas the figure renders into. Calls the draw and print figmethods, creates the renderers, etc.
Note: GUI templates will want to connect events for button presses,mouse movements and key presses to functions that call the baseclass methods button_press_event, button_release_event,motion_notify_event, key_press_event, and key_release_event. See theimplementations of the interactive backends for examples.
- Attributes:
- figure
Figure
A high-level Figure instance
- figure
- draw()[source]#
Draw the figure using the renderer.
It is important that this method actually walk the artist treeeven if not output is produced because this will triggerdeferred work (like computing limits auto-limits and tickvalues) that users may want access to before saving to disk.
- filetypes={'eps':'EncapsulatedPostscript','foo':'MymagicFooformat','jpeg':'JointPhotographicExpertsGroup','jpg':'JointPhotographicExpertsGroup','pdf':'PortableDocumentFormat','pgf':'PGFcodeforLaTeX','png':'PortableNetworkGraphics','ps':'Postscript','raw':'RawRGBAbitmap','rgba':'RawRGBAbitmap','svg':'ScalableVectorGraphics','svgz':'ScalableVectorGraphics','tif':'TaggedImageFileFormat','tiff':'TaggedImageFileFormat','webp':'WebPImageFormat'}#
- get_default_filetype()[source]#
Return the default savefig file format as specified in
rcParams["savefig.format"]
(default:'png'
).The returned string does not include a period. This method isoverridden in backends that only support a single file type.
- manager_class[source]#
alias of
FigureManagerTemplate
- print_foo(filename,**kwargs)[source]#
Write out format foo.
This method is normally called via
Figure.savefig
andFigureCanvasBase.print_figure
, which take care of setting the figurefacecolor, edgecolor, and dpi to the desired output values, and willrestore them to the original values. Therefore,print_foo
does notneed to handle these settings.
- matplotlib.backends.backend_template.FigureManager[source]#
alias of
FigureManagerTemplate
- classmatplotlib.backends.backend_template.FigureManagerTemplate(canvas,num)[source]#
Bases:
FigureManagerBase
Helper class for pyplot mode, wraps everything up into a neat bundle.
For non-interactive backends, the base class is sufficient. Forinteractive backends, see the documentation of the
FigureManagerBase
class for the list of methods that can/should be overridden.
- classmatplotlib.backends.backend_template.GraphicsContextTemplate[source]#
Bases:
GraphicsContextBase
The graphics context provides the color, line styles, etc. See the cairoand postscript backends for examples of mapping the graphics contextattributes (cap styles, join styles, line widths, colors) to a particularbackend. In cairo this is done by wrapping a cairo.Context object andforwarding the appropriate calls to it using a dictionary mapping stylesto gdk constants. In Postscript, all the work is done by the renderer,mapping line styles to postscript calls.
If it's more appropriate to do the mapping at the renderer level (as inthe postscript backend), you don't need to override any of the GC methods.If it's more appropriate to wrap an instance (as in the cairo backend) anddo the mapping here, you'll need to override several of the settermethods.
The base GraphicsContext stores colors as an RGB tuple on the unitinterval, e.g., (0.5, 0.0, 1.0). You may need to map this to colorsappropriate for your backend.
- classmatplotlib.backends.backend_template.RendererTemplate(dpi)[source]#
Bases:
RendererBase
The renderer handles drawing/rendering operations.
This is a minimal do-nothing class that can be used to get started whenwriting a new backend. Refer to
backend_bases.RendererBase
fordocumentation of the methods.- draw_image(gc,x,y,im)[source]#
Draw an RGBA image.
- Parameters:
- gc
GraphicsContextBase
A graphics context with clipping information.
- xfloat
The distance in physical units (i.e., dots or pixels) from the lefthand side of the canvas.
- yfloat
The distance in physical units (i.e., dots or pixels) from thebottom side of the canvas.
- im(N, M, 4) array of
numpy.uint8
An array of RGBA pixels.
- transform
Affine2DBase
If and only if the concrete backend is written such that
option_scale_image
returnsTrue
, an affine transformation(i.e., anAffine2DBase
)may be passed todraw_image
. Thetranslation vector of the transformation is given in physical units(i.e., dots or pixels). Note that the transformation does notoverridex andy, and has to be appliedbefore translatingthe result byx andy (this can be accomplished by addingxandy to the translation vector defined bytransform).
- gc
- draw_path(gc,path,transform,rgbFace=None)[source]#
Draw a
Path
instance using the given affine transform.
- draw_text(gc,x,y,s,prop,angle,ismath=False,mtext=None)[source]#
Draw a text instance.
- Parameters:
- gc
GraphicsContextBase
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.
- prop
FontProperties
The font properties.
- anglefloat
The rotation angle in degrees anti-clockwise.
- ismathbool or "TeX"
If True, use mathtext parser.
- mtext
Text
The original text object to be rendered.
- gc
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
.
- flipy()[source]#
Return whether y values increase from top to bottom.
Note that this only affects drawing of texts.
- 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 with
FontProperties
prop.Whitespace at the start and the end ofs is included in the reported width.
- new_gc()[source]#
Return an instance of a
GraphicsContextBase
.
- 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