matplotlib.backends.backend_pdf#

一个 PDF Matplotlib 后端。

作者:Jouni K Seppänen <jks@iki.fi> 及其他。

matplotlib.backends.backend_pdf.FigureCanvas[source]#

别名 FigureCanvasPdf

class matplotlib.backends.backend_pdf.FigureCanvasPdf(figure=None)[source]#

基类:FigureCanvasBase

draw()[source]#

渲染 Figure

此方法必须遍历 Artist 树,即使没有产生输出,因为它会触发用户在将输出保存到磁盘之前可能希望访问的延迟工作。例如,计算限制、自动限制和刻度值。

filetypes = {'pdf': '可移植文档格式'}#
fixed_dpi = 72#
get_default_filetype()[source]#

返回 `savefig` 默认文件格式,该格式在 rcParams["savefig.format"] 中指定(默认:'png')。

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

print_pdf(filename, *, bbox_inches_restore=None, metadata=None)[source]#
class matplotlib.backends.backend_pdf.GraphicsContextPdf(file)[source]#

基类:GraphicsContextBase

alpha_cmd(alpha, forced, effective_alphas)[source]#
capstyle_cmd(style)[source]#
capstyles = {'butt': 0, 'projecting': 2, 'round': 1}#
clip_cmd(cliprect, clippath)[source]#

设置裁剪矩形。调用 pop()push()

commands = ((('_cliprect', '_clippath'), <function GraphicsContextPdf.clip_cmd>), (('_alpha', '_forced_alpha', '_effective_alphas'), <function GraphicsContextPdf.alpha_cmd>), (('_capstyle',), <function GraphicsContextPdf.capstyle_cmd>), (('_fillcolor',), <function GraphicsContextPdf.fillcolor_cmd>), (('_joinstyle',), <function GraphicsContextPdf.joinstyle_cmd>), (('_linewidth',), <function GraphicsContextPdf.linewidth_cmd>), (('_dashes',), <function GraphicsContextPdf.dash_cmd>), (('_rgb',), <function GraphicsContextPdf.rgb_cmd>), (('_hatch', '_hatch_color', '_hatch_linewidth'), <function GraphicsContextPdf.hatch_cmd>))#
copy_properties(other)[source]#

将其他对象的属性复制到自身。

dash_cmd(dashes)[source]#
delta(other)[source]#

将其他对象的属性复制到自身,并返回将 self 转换为 other 所需的 PDF 命令。

fill(*args)[source]#

判断:路径需要填充吗?

一个可选参数可用于指定替代的 `_fillcolor`,这是 `RendererPdf.draw_markers` 所需的。

fillcolor_cmd(rgb)[source]#
finalize()[source]#

确保每个压入的图形状态都被弹出。

hatch_cmd(hatch, hatch_color, hatch_linewidth)[source]#
joinstyle_cmd(style)[source]#
joinstyles = {'bevel': 2, 'miter': 0, 'round': 1}#
linewidth_cmd(width)[source]#
paint()[source]#

返回适当的 PDF 运算符,使路径被描边、填充或两者兼有。

pop()[source]#
push()[source]#
rgb_cmd(rgb)[source]#
stroke()[source]#

判断:路径需要描边(即绘制其轮廓)吗?这会检测禁用路径描边的各种条件,在这种情况下,路径会被填充。

class matplotlib.backends.backend_pdf.Name(name)[source]#

基类: object

PDF 名称对象。

name#
pdfRepr()[source]#
class matplotlib.backends.backend_pdf.Op(*values)[source]#

基类: Enum

PDF 运算符(非详尽列表)。

begin_text = b'BT'[source]#
clip = b'W'[source]#
close_fill_stroke = b'b'[source]#
close_stroke = b's'[source]#
closepath = b'h'[source]#
concat_matrix = b'cm'[source]#
curveto = b'c'[source]#
end_text = b'ET'[source]#
endpath = b'n'[source]#
fill = b'f'[source]#
fill_stroke = b'B'[source]#
grestore = b'Q'[source]#
gsave = b'q'[source]#
lineto = b'l'[source]#
moveto = b'm'[source]#
classmethod paint_path(fill, stroke)[source]#

返回用于绘制路径的 PDF 运算符。

参数:
fillbool

用填充颜色填充路径。

strokebool

用线条颜色描边路径的轮廓。

pdfRepr()[source]#
rectangle = b're'[source]#
selectfont = b'Tf'[source]#
setcolor_nonstroke = b'scn'[source]#
setcolor_stroke = b'SCN'[source]#
setcolorspace_nonstroke = b'cs'[source]#
setcolorspace_stroke = b'CS'[source]#
setdash = b'd'[source]#
setgray_nonstroke = b'g'[source]#
setgray_stroke = b'G'[source]#
setgstate = b'gs'[source]#
setlinecap = b'J'[source]#
setlinejoin = b'j'[source]#
setlinewidth = b'w'[source]#
setrgb_nonstroke = b'rg'[source]#
setrgb_stroke = b'RG'[source]#
shading = b'sh'[source]#
show = b'Tj'[source]#
showkern = b'TJ'[source]#
stroke = b'S'[source]#
textmatrix = b'Tm'[source]#
textpos = b'Td'[source]#
use_xobject = b'Do'[source]#
class matplotlib.backends.backend_pdf.PdfFile(filename, metadata=None)[source]#

基类: object

PDF 文件对象。

参数:
filenamestr 或 类路径 或 类文件

输出目标;如果为字符串,将打开文件进行写入。

metadatadict from strings to strings and dates

信息字典对象(参见 PDF 参考资料 10.2.1 节“文档信息字典”),例如:{'Creator': 'My software', 'Author': 'Me', 'Title': 'Awesome'}

标准键包括 'Title'、'Author'、'Subject'、'Keywords'、'Creator'、'Producer'、'CreationDate'、'ModDate' 和 'Trapped'。'Creator'、'Producer' 和 'CreationDate' 的值已预定义。可以通过将它们设置为 None 来移除。

addGouraudTriangles(points, colors)[source]#

添加一个高洛德三角形着色。

参数:
pointsnp.ndarray

三角形顶点,形状为 (n, 3, 2),其中 n = 三角形数量,3 = 顶点,2 = x, y。

colorsnp.ndarray

顶点颜色,形状为 (n, 3, 1) 或 (n, 3, 4),与点类似,但最后一维为 (灰度,) 或 (r, g, b, alpha)。

返回:
名称,引用
alphaState(alpha)[source]#

返回一个 ExtGState 的名称,该 ExtGState 将 alpha 设置为给定值。

beginStream(id, len, extra=None, png=None)[source]#
close()[source]#

刷新所有缓冲区并释放所有资源。

createType1Descriptor(t1font, fontfile)[source]#
dviFontName(dvifont)[source]#

给定一个 dvi 字体对象,返回一个适用于 Op.selectfont 的名称。如果尚未注册,此操作将在 self.dviFontInfo 中注册字体信息。

embedTTF(filename, characters)[source]#

将指定文件中的 TTF 字体嵌入到文档中。

endStream()[source]#
finalize()[source]#

写出各种延迟对象和 PDF 结束内容。

fontName(fontprop)[source]#

根据 fontprop 选择字体,并返回一个适用于 Op.selectfont 的名称。如果 fontprop 是一个字符串,它将被解释为字体文件名。

hatchPattern(hatch_style)[source]#
property hatchPatterns[source]#

[已弃用]

备注

自 3.10 版本起已弃用

imageObject(image)[source]#

返回表示给定图像的图像 XObject 的名称。

markerObject(path, trans, fill, stroke, lw, joinstyle, capstyle)[source]#

返回表示给定路径的标记 XObject 的名称。

newPage(width, height)[source]#
newTextnote(text, positionRect=[-100, -100, 0, 0])[source]#
output(*data)[source]#
outputStream(ref, data, *, extra=None)[source]#
pathCollectionObject(gc, path, trans, padding, filled, stroked)[source]#
static pathOperations(path, transform, clip=None, simplify=None, sketch=None)[source]#
recordXref(id)[source]#
reserveObject(name='')[source]#

为间接对象保留一个 ID。

该名称用于调试,以防我们忘记使用 writeObject 打印对象。

write(data)[source]#
writeExtGSTates()[source]#
writeFonts()[source]#
writeGouraudTriangles()[source]#
writeHatches()[source]#
writeImages()[source]#
writeInfoDict()[source]#

写出信息字典,并检查其格式是否正确。

writeMarkers()[source]#
writeObject(object, contents)[source]#
writePath(path, transform, clip=False, sketch=None)[source]#
writePathCollectionTemplates()[source]#
writeTrailer()[source]#

写出 PDF 尾部。

writeXref()[source]#

写出交叉引用表。

class matplotlib.backends.backend_pdf.PdfPages(filename, keep_empty=<deprecated parameter>, metadata=None)[source]#

基类: object

一个多页 PDF 文件。

备注

实际上,PdfPagesPdfFile 的一个薄包装器,旨在避免在使用 savefig 时忘记格式参数而引起的混淆。

示例

>>> import matplotlib.pyplot as plt
>>> # Initialize:
>>> with PdfPages('foo.pdf') as pdf:
...     # As many times as you like, create a figure fig and save it:
...     fig = plt.figure()
...     pdf.savefig(fig)
...     # When no figure is specified the current figure is saved
...     pdf.savefig()

创建一个新的 PdfPages 对象。

参数:
filenamestr 或 类路径 或 类文件

使用 PdfPages.savefig 绘制的图表将写入到此位置的文件中。当首次保存图形时,文件将被打开(覆盖同名旧文件)。

metadata字典,可选

信息字典对象(参见 PDF 参考资料 10.2.1 节“文档信息字典”),例如:{'Creator': 'My software', 'Author': 'Me', 'Title': 'Awesome'}

标准键包括 'Title'、'Author'、'Subject'、'Keywords'、'Creator'、'Producer'、'CreationDate'、'ModDate' 和 'Trapped'。'Creator'、'Producer' 和 'CreationDate' 的值已预定义。可以通过将它们设置为 None 来移除。

attach_note(text, positionRect=[-100, -100, 0, 0])[source]#

为下一页添加一个新的文本注释。可选的 positionRect 指定新注释在页面上的位置。默认情况下,它位于页面外部,以确保在打印输出中不可见。

close()[source]#

最终确定此对象,使其底层文件成为完整的 PDF 文件。

get_pagecount()[source]#

返回多页 PDF 文件中当前的页数。

infodict()[source]#

返回一个可修改的信息字典对象(参见 PDF 参考资料 10.2.1 节“文档信息字典”)。

savefig(figure=None, **kwargs)[source]#

将一个 Figure 保存为新页面到此文件。

任何其他关键字参数都将传递给 savefig

参数:
figureFigure or int, default: the active figure

将保存到文件的图形或图形索引。

class matplotlib.backends.backend_pdf.Reference(id)[source]#

基类: object

PDF 引用对象。

使用 PdfFile.reserveObject() 创建引用。

pdfRepr()[source]#
write(contents, file)[source]#
class matplotlib.backends.backend_pdf.RendererPdf(file, image_dpi, height, width)[source]#

基类:RendererPDFPSBase

check_gc(gc, fillcolor=None)[source]#
draw_gouraud_triangles(gc, points, colors, trans)[source]#

绘制一系列 Gouraud 三角形。

参数:
gcGraphicsContextBase

图形上下文。

triangles_array(N, 3, 2) 类数组

N个三角形的 (x, y) 点数组。

colors_array(N, 3, 4) 类数组

N个三角形每个点的 RGBA 颜色数组。

transformTransform

应用于点的仿射变换。

draw_image(gc, x, y, im, transform=None)[source]#

绘制 RGBA 图像。

参数:
gcGraphicsContextBase

带有裁剪信息的图形上下文。

x浮点数

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

y浮点数

从画布底部开始的物理单位(即点或像素)距离。

im(N, M, 4) array of numpy.uint8

RGBA 像素数组。

transformAffine2DBase

当且仅当具体后端在编写时使 option_scale_image 返回 True 时,仿射变换(即 Affine2DBase)_可能_会传递给 draw_image。变换的平移向量以物理单位(即点或像素)给出。请注意,变换不会覆盖 _x_ 和 _y_,并且必须在通过 _x_ 和 _y_ 平移结果_之前_应用(这可以通过将 _x_ 和 _y_ 添加到由 _transform_ 定义的平移向量来完成)。

draw_markers(gc, marker_path, marker_trans, path, trans, rgbFace=None)[source]#

path 的每个顶点(不包括控制点)绘制一个标记。

基本(回退)实现会多次调用 draw_path。后端可能希望覆盖此方法,以便只绘制标记一次并多次重用它。

参数:
gcGraphicsContextBase

图形上下文。

marker_pathPath

标记的路径。

marker_transTransform

应用于标记的仿射变换。

pathPath

绘制标记的位置。

transTransform

应用于路径的仿射变换。

rgbFace颜色,可选
draw_mathtext(gc, x, y, s, prop, angle)[source]#
draw_path(gc, path, transform, rgbFace=None)[source]#

使用给定的仿射变换绘制 Path 实例。

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

绘制一组 paths

每个路径首先由 _all_transforms_ 中对应的条目(一个 (3, 3) 矩阵列表)变换,然后由 _master_transform_ 变换。接着,它们由 _offsets_ 中对应的条目平移,该条目首先由 _offset_trans_ 变换。

facecolorsedgecolorslinewidthslinestylesantialiased 是设置相应属性的列表。

offset_position 现在未使用,但参数保留是为了向后兼容。

基本(回退)实现会多次调用 draw_path。后端可能希望覆盖此方法,以便仅渲染每组路径数据一次,然后使用不同的偏移量、颜色、样式等多次引用该路径。提供了生成器方法 _iter_collection_raw_paths_iter_collection,以帮助(并标准化)跨后端实现。强烈建议使用这些生成器,以便可以全局更改 draw_path_collection 的行为。

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

绘制一个 TeX 实例。

参数:
gcGraphicsContextBase

图形上下文。

x浮点数

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

y浮点数

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

sstr

TeX 文本字符串。

propFontProperties

字体属性。

angle浮点数

逆时针旋转角度(度)。

mtextText

要渲染的原始文本对象。

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

绘制文本实例。

参数:
gcGraphicsContextBase

图形上下文。

x浮点数

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

y浮点数

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

sstr

文本字符串。

propFontProperties

字体属性。

angle浮点数

逆时针旋转角度(度)。

ismath布尔值 或 "TeX"

如果为 True,则使用 mathtext 解析器。

mtextText

要渲染的原始文本对象。

备注

后端实现者注意事项

RendererBase.draw_text 也支持将“TeX”传递给 _ismath_ 参数以使用 TeX 渲染,但这对实际的渲染后端来说不是必需的,并且许多内置后端确实不支持此功能。相反,TeX 渲染由 draw_tex 提供。

encode_string(s, fonttype)[source]#
finalize()[source]#
get_image_magnification()[source]#

获取传递给 draw_image 的图像的放大因子。允许后端以与其它艺术家不同的分辨率处理图像。

new_gc()[source]#

返回 GraphicsContextBase 的实例。

class matplotlib.backends.backend_pdf.Stream(id, len, file, extra=None, png=None)[source]#

基类: object

PDF 流对象。

此对象没有 pdfRepr 方法。相反,请调用 begin(),然后通过调用 write() 输出流内容,最后调用 end()。

参数:
idint

流的对象 ID。

lenReference or None

一个未使用的 Reference 对象,用于表示流的长度;None 表示使用内存缓冲区,以便长度可以内联。

filePdfFile

写入流的底层对象。

extradict from Name to anything, or None

要包含在流头中的额外键值对。

pngdict or None

如果数据已进行 png 编码,则为解码参数。

compressobj#
end()[source]#

最终确定流。

extra#
file#
id#
len#
pdfFile#
pos#
write(data)[source]#

向流中写入一些数据。

class matplotlib.backends.backend_pdf.Verbatim(x)[source]#

基类: object

存储逐字 PDF 命令内容,以便稍后包含在流中。

pdfRepr()[source]#
matplotlib.backends.backend_pdf.pdfRepr(obj)[source]#

将 Python 对象映射到 PDF 语法。