matplotlib.backends.backend_template

一个完全功能化的,不做任何事情的后端,作为后端编写器的模板。它功能齐全,您可以选择它作为后端,例如:

import matplotlib
matplotlib.use("template")

你的程序会(应该!)运行时不会出错,但不会产生任何输出。这为后端编写器提供了一个起点;您可以有选择地实现绘图方法 (draw_pathdraw_image 等等)慢慢地看到你的形象变得栩栩如生,而不是在得到任何结果之前必须有一个全面的实现。

将此文件复制到Matplotlib源代码树之外的某个目录中,Python可以将其导入该目录(通过将该目录添加到 sys.path 或者将其打包为普通的Python包);如果后端可以作为 import my.backend 然后可以使用以下命令选择它:

import matplotlib
matplotlib.use("module://my.backend")

如果您的后端实现了对保存数字的支持(即 print_xyz 方法),可以将其注册为给定文件类型的默认处理程序:

from matplotlib.backend_bases import register_backend
register_backend('xyz', 'my_backend', 'XYZ File Format')
...
plt.savefig("figure.xyz")
matplotlib.backends.backend_template.FigureCanvas

alias of matplotlib.backends.backend_template.FigureCanvasTemplate

class matplotlib.backends.backend_template.FigureCanvasTemplate(figure)[源代码]

基类:matplotlib.backend_bases.FigureCanvasBase

图形渲染到的画布。调用draw和print fig方法,创建渲染器等。

注意:GUI模板希望将按钮按下、鼠标移动和按键事件连接到调用基类方法button_press_事件、button_release_事件、motion_notify_事件、key_press_事件和key_release_事件的函数。有关示例,请参见交互式后端的实现。

属性:
图形matplotlib.figure.Figurematplotlib.figure.Figure

高级图形实例

draw()[源代码]

使用渲染器绘制图形。

filetypes = {'eps': 'Encapsulated Postscript', 'foo': 'My magic Foo format', 'jpeg': 'Joint Photographic Experts Group', 'jpg': 'Joint Photographic Experts Group', 'pdf': 'Portable Document Format', 'pgf': 'PGF code for LaTeX', 'png': 'Portable Network Graphics', 'ps': 'Postscript', 'raw': 'Raw RGBA bitmap', 'rgba': 'Raw RGBA bitmap', 'svg': 'Scalable Vector Graphics', 'svgz': 'Scalable Vector Graphics', 'tif': 'Tagged Image File Format', 'tiff': 'Tagged Image File Format'}
get_default_filetype()[源代码]

返回中指定的默认savefig文件格式 rcParams["savefig.format"] (default: 'png') .

返回的字符串不包含句点。此方法在仅支持单一文件类型的后端中被重写。

print_foo(filename, *args, **kwargs)[源代码]

写出foo格式。dpi、facecolor和edgecolor在这个调用之后会恢复到它们的原始值,因此您不需要保存和恢复它们。

matplotlib.backends.backend_template.FigureManager

alias of matplotlib.backends.backend_template.FigureManagerTemplate

class matplotlib.backends.backend_template.FigureManagerTemplate(canvas, num)[源代码]

基类:matplotlib.backend_bases.FigureManagerBase

pyplot模式的Helper类,将所有内容打包成一个整洁的包。

对于非交互式后端,基类就足够了。

class matplotlib.backends.backend_template.GraphicsContextTemplate[源代码]

基类:matplotlib.backend_bases.GraphicsContextBase

图形上下文提供颜色、线条样式等。。。有关将图形上下文属性(cap样式、连接样式、线宽、颜色)映射到特定后端的示例,请参阅cairo和postscript后端。在开罗,这是通过包装开罗。上下文对象,并使用字典将样式映射到gdk常量来转发相应的调用。在Postscript中,所有的工作都由渲染器完成,将线条样式映射到Postscript调用。

如果在呈现器级别(如postscript后端)执行映射更合适,则不需要重写任何GC方法。如果包装一个实例(如在cairo后端)并在这里进行映射更合适,则需要重写几个setter方法。

基本GraphicsContext将颜色存储为单位间隔上的RGB元组,例如(0.5、0.0、1.0)。您可能需要将其映射到适合您后端的颜色。

class matplotlib.backends.backend_template.RendererTemplate(dpi)[源代码]

基类:matplotlib.backend_bases.RendererBase

渲染器处理绘制/渲染操作。

这是一个最小的什么都不做的类,可以用来开始编写新的后端。参考 backend_bases.RendererBase 方法文件。

draw_image(gc, x, y, im)[源代码]

绘制RGBA图像。

参数:
gcGraphicsContextBaseGraphicsContextBase

包含剪辑信息的图形上下文。

x标量

距离画布左侧的物理单位(即点或像素)。

y标量

以物理单位(即点或像素)表示的与画布底部的距离。

im类数组,shape=(N,M,4),数据类型=np.uint8公司

一组rgba像素。

转型matplotlib.transforms.Affine2DBasematplotlib.transforms.Affine2DBase

如果并且仅当具体的后端被写为 option_scale_image() 收益率 True ,仿射变换(即 Affine2DBasemay 被传授给 draw_image() . 变换的平移向量以物理单位(即点或像素)给出。请注意,转换不会重写 xy ,必须应用 之前 将结果转换为 xy (这可以通过添加 xy 到由定义的翻译矢量 转型

draw_path(gc, path, transform, rgbFace=None)[源代码]

画一个 Path 使用给定仿射变换的实例。

draw_text(gc, x, y, s, prop, angle, ismath=False, mtext=None)[源代码]

绘制文本实例。

参数:
gcGraphicsContextBaseGraphicsContextBase

图形上下文。

x浮动

文本在显示坐标中的x位置。

y浮动

文本基线在显示坐标中的y位置。

sSTR

文本字符串。

propmatplotlib.font_manager.FontPropertiesmatplotlib.font_manager.FontProperties

字体属性。

angle浮动

以度为单位的逆时针旋转角度。

多行文本matplotlib.text.Textmatplotlib.text.Text

要呈现的原始文本对象。

笔记

后端实现者注意:

当您试图确定边界框是否正确时(这是使文本布局/对齐方式正常工作的原因),它有助于更改文本中的线条。py::

if 0: bbox_artist(self, renderer)

如果为1,则实际边界框将与文本一起打印。

flipy()[源代码]

返回y值是否从上到下递增。

请注意,这只影响文本和图像的绘制。

get_canvas_width_height()[源代码]

返回显示坐标中的画布宽度和高度。

get_text_width_height_descent(s, prop, ismath)[源代码]

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

new_gc()[源代码]

返回的实例 GraphicsContextBase .

points_to_pixels(points)[源代码]

将点转换为显示单位。

您需要重写这个函数(除非您的后端没有dpi,例如postscript或svg)。一些成像系统假定每英寸像素的值为:

points to pixels = points * pixels_per_inch/72 * dpi/72
参数:
points浮点数或类似数组的

浮点数或浮点数数组

返回:
转换为像素的点
matplotlib.backends.backend_template.draw_if_interactive()[源代码]

对于映像后端-不需要。对于GUI后端-如果绘图应在交互式python模式下完成,则应重写此选项。

matplotlib.backends.backend_template.new_figure_manager(num, *args, FigureClass=<class 'matplotlib.figure.Figure'>, **kwargs)[源代码]

创建新的地物管理器实例。

matplotlib.backends.backend_template.new_figure_manager_given_figure(num, figure)[源代码]

为给定的图形创建新的图形管理器实例。

matplotlib.backends.backend_template.show(*, block=None)[源代码]

对于映像后端-不需要。对于GUI后端-show()通常是pyplot脚本的最后一行,它告诉后端是时候绘制了。在交互模式下,这不应该起任何作用。