matplotlib.text#

用于在图中包含文本的类。

class matplotlib.text.Text(x=0, y=0, text='', *, color=None, verticalalignment='baseline', horizontalalignment='left', multialignment=None, fontproperties=None, rotation=None, linespacing=None, rotation_mode=None, usetex=None, wrap=False, transform_rotates_text=False, parse_math=None, antialiased=None, **kwargs)[source]#

基类: Artist

处理在窗口或数据坐标中存储和绘制文本。

在坐标 x, y 处创建一个 Text 实例,并使用字符串 text

文本根据 horizontalalignment(默认值:'left')和 verticalalignment(默认值:'baseline')相对于锚点 (x, y) 进行对齐。另请参阅 文本对齐

虽然 Text 接受 'label' 关键字参数,但默认情况下它不会添加到图例的句柄中。

有效的关键字参数有:

属性

描述

agg_filter

一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量

alpha

浮点数或 None

animated

布尔值

antialiased

布尔值

backgroundcolor

color

bbox

包含 patches.FancyBboxPatch 属性的字典

clip_box

未知

clip_on

未知

clip_path

未知

colorc

color

figure

FigureSubFigure

fontfamilyfamilyfontname

{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}

fontpropertiesfontfont_properties

font_manager.FontPropertiesstrpathlib.Path

fontsizesize

float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}

fontstretchstretch

{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}

fontstylestyle

{'normal', 'italic', 'oblique'}

fontvariantvariant

{'normal', 'small-caps'}

fontweightweight

{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}

gid

str

horizontalalignmentha

{'left', 'center', 'right'}

in_layout

布尔值

label

对象

linespacing

float (字体大小的倍数)

math_fontfamily

str

mouseover

布尔值

multialignmentma

{'left', 'right', 'center'}

parse_math

布尔值

path_effects

AbstractPathEffect 的列表

picker

None 或 布尔值 或 浮点数 或 可调用对象

position

(浮点数, 浮点数)

rasterized

布尔值

rotation

float 或 {'vertical', 'horizontal'}

rotation_mode

{None, 'default', 'anchor'}

sketch_params

(scale: 浮点数, length: 浮点数, randomness: 浮点数)

snap

布尔值或 None

text

对象

transform

变换

transform_rotates_text

布尔值

url

str

usetex

布尔值,默认值:rcParams["text.usetex"](默认值:False

verticalalignmentva

{'baseline', 'bottom', 'center', 'center_baseline', 'top'}

visible

布尔值

wrap

布尔值

x

浮点数

y

浮点数

zorder

浮点数

contains(mouseevent)[source]#

返回鼠标事件是否发生在文本的轴对齐边界框内。

draw(renderer)[source]#

使用给定的渲染器绘制 Artist(及其子对象)。

如果 artist 不可见(Artist.get_visible 返回 False),则此操作无效。

参数:
rendererRendererBase 子类。

备注

此方法在 Artist 子类中被覆盖。

get_antialiased()[source]#

返回是否使用抗锯齿渲染。

get_bbox_patch()[source]#

返回 bbox 补丁,如果未创建 patches.FancyBboxPatch 则返回 None。

get_c()[source]#

get_color 的别名。

get_color()[source]#

返回文本的颜色。

get_family()[source]#

get_fontfamily 的别名。

get_font()[source]#

get_fontproperties 的别名。

get_font_properties()[source]#

get_fontproperties 的别名。

get_fontfamily()[source]#

返回用于字体查找的字体系列列表。

get_fontname()[source]#

将字体名称作为字符串返回。

get_fontproperties()[source]#

返回 font_manager.FontProperties

get_fontsize()[source]#

将字体大小作为整数返回。

get_fontstyle()[source]#

将字体样式作为字符串返回。

get_fontvariant()[source]#

将字体变体作为字符串返回。

get_fontweight()[source]#

将字体粗细作为字符串或数字返回。

get_ha()[source]#

get_horizontalalignment 的别名。

get_horizontalalignment()[source]#

将水平对齐方式作为字符串返回。将是 'left'、'center' 或 'right' 之一。

get_math_fontfamily()[source]#

返回 Matplotlib 渲染的数学文本的字体系列名称。

默认值为 rcParams["mathtext.fontset"](默认值:'dejavusans')。

另请参阅

set_math_fontfamily
get_name()[source]#

get_fontname 的别名。

get_parse_math()[source]#

返回此 Text 是否考虑数学文本解析。

get_position()[source]#

返回文本的 (x, y) 位置。

get_rotation()[source]#

返回文本角度,介于 0 到 360 度之间。

get_rotation_mode()[source]#

返回文本旋转模式。

get_size()[source]#

get_fontsize 的别名。

get_stretch()[source]#

将字体拉伸作为字符串或数字返回。

get_style()[source]#

get_fontstyle 的别名。

get_text()[source]#

返回文本字符串。

get_transform_rotates_text()[source]#

返回变换的旋转是否影响文本方向。

get_unitless_position()[source]#

返回文本的 (x, y) 无单位位置。

get_usetex()[source]#

返回此 Text 对象是否使用 TeX 进行渲染。

get_va()[source]#

get_verticalalignment 的别名。

get_variant()[source]#

get_fontvariant 的别名。

get_verticalalignment()[source]#

将垂直对齐方式作为字符串返回。将是 'top'、'center'、'bottom'、'baseline' 或 'center_baseline' 之一。

get_weight()[source]#

get_fontweight 的别名。

get_window_extent(renderer=None, dpi=None)[source]#

返回边界文本的 Bbox,以显示单位表示。

除了内部使用外,这对于在网页上的 png 文件中指定可点击区域也很有用。

参数:
renderer渲染器,可选

需要渲染器来计算边界框。如果 artist 已经绘制,则渲染器会被缓存;因此,只有在第一次绘制之前调用 get_window_extent 时才需要传递此参数。实际上,通常更容易先触发一次绘制,例如通过调用 draw_without_renderingplt.show()

dpifloat, 可选

用于计算边界框的 dpi 值,默认为 self.get_figure(root=True).dpi不是渲染器 dpi);例如,如果需要与以自定义 dpi 值保存的图形区域匹配,则应进行设置。

get_wrap()[source]#

返回文本是否可以换行。

set(*, agg_filter=<UNSET>, alpha=<UNSET>, animated=<UNSET>, antialiased=<UNSET>, backgroundcolor=<UNSET>, bbox=<UNSET>, clip_box=<UNSET>, clip_on=<UNSET>, clip_path=<UNSET>, color=<UNSET>, fontfamily=<UNSET>, fontproperties=<UNSET>, fontsize=<UNSET>, fontstretch=<UNSET>, fontstyle=<UNSET>, fontvariant=<UNSET>, fontweight=<UNSET>, gid=<UNSET>, horizontalalignment=<UNSET>, in_layout=<UNSET>, label=<UNSET>, linespacing=<UNSET>, math_fontfamily=<UNSET>, mouseover=<UNSET>, multialignment=<UNSET>, parse_math=<UNSET>, path_effects=<UNSET>, picker=<UNSET>, position=<UNSET>, rasterized=<UNSET>, rotation=<UNSET>, rotation_mode=<UNSET>, sketch_params=<UNSET>, snap=<UNSET>, text=<UNSET>, transform=<UNSET>, transform_rotates_text=<UNSET>, url=<UNSET>, usetex=<UNSET>, verticalalignment=<UNSET>, visible=<UNSET>, wrap=<UNSET>, x=<UNSET>, y=<UNSET>, zorder=<UNSET>)[source]#

一次性设置多个属性。

支持的属性包括:

属性

描述

agg_filter

一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量

alpha

浮点数或 None

animated

布尔值

antialiased

布尔值

backgroundcolor

color

bbox

包含 patches.FancyBboxPatch 属性的字典

clip_box

未知

clip_on

未知

clip_path

未知

color

color

figure

FigureSubFigure

fontfamilyfontname

{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}

fontproperties

font_manager.FontPropertiesstrpathlib.Path

fontsize

float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}

fontstretch

{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}

fontstyle

{'normal', 'italic', 'oblique'}

fontvariant

{'normal', 'small-caps'}

fontweight

{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}

gid

str

horizontalalignment

{'left', 'center', 'right'}

in_layout

布尔值

label

对象

linespacing

float (字体大小的倍数)

math_fontfamily

str

mouseover

布尔值

multialignment

{'left', 'right', 'center'}

parse_math

布尔值

path_effects

AbstractPathEffect 的列表

picker

None 或 布尔值 或 浮点数 或 可调用对象

position

(浮点数, 浮点数)

rasterized

布尔值

rotation

float 或 {'vertical', 'horizontal'}

rotation_mode

{None, 'default', 'anchor'}

sketch_params

(scale: 浮点数, length: 浮点数, randomness: 浮点数)

snap

布尔值或 None

text

对象

transform

变换

transform_rotates_text

布尔值

url

str

usetex

布尔值,默认值:rcParams["text.usetex"](默认值:False

verticalalignment

{'baseline', 'bottom', 'center', 'center_baseline', 'top'}

visible

布尔值

wrap

布尔值

x

浮点数

y

浮点数

zorder

浮点数

set_antialiased(antialiased)[source]#

设置是否使用抗锯齿渲染。

参数:
antialiasedbool

备注

抗锯齿将由 rcParams["text.antialiased"](默认值:True)决定,如果文本包含数学表达式,则参数 antialiased 将不起作用。

set_backgroundcolor(color)[source]#

通过更新边界框(bbox)来设置文本的背景颜色。

参数:
color颜色

另请参阅

set_bbox

用于更改边界框的位置

set_bbox(rectprops)[source]#

在自身周围绘制一个边界框。

参数:
rectpropsdict with properties for patches.FancyBboxPatch

默认的 boxstyle 为 'square'。`patches.FancyBboxPatch` 的变异比例设置为字体大小。

示例

t.set_bbox(dict(facecolor='red', alpha=0.5))
set_c(color)[source]#

set_color 的别名。

set_clip_box(clipbox)[source]#

设置艺术家的裁剪Bbox

参数:
clipboxBboxBase or None

通常从 TransformedBbox 创建。例如,TransformedBbox(Bbox([[0, 0], [1, 1]]), ax.transAxes) 是添加到 Axes 的 Artist 的默认裁剪。

set_clip_on(b)[source]#

设置艺术家是否使用裁剪。

当为False时,艺术家将会在Axes外部可见,这可能导致意外结果。

参数:
bbool
set_clip_path(path, transform=None)[source]#

设置艺术家的裁剪路径。

参数:
pathPatch or Path or TransformedPath or None

裁剪路径。如果给定一个 Path,则必须同时提供 transform。如果为 *None*,则移除之前设置的裁剪路径。

transformTransform, optional

仅当 *path* 是 Path 时使用,在这种情况下,给定的 Path 将使用 *transform* 转换为 TransformedPath

备注

为了提高效率,如果 *path* 是 Rectangle,此方法会将裁剪框设置为对应的矩形,并将裁剪路径设置为 None

出于技术原因(支持 set),也可以接受元组(*path*,*transform*)作为单个位置参数。

set_color(color)[source]#

设置文本的前景色

参数:
color颜色
set_family(fontname)[source]#

set_fontfamily 的别名。

set_font(fp)[source]#

set_fontproperties 的别名。

set_font_properties(fp)[source]#

set_fontproperties 的别名。

set_fontfamily(fontname)[source]#

设置字体系列。可以是一个字符串,也可以是按优先级递减的字符串列表。每个字符串可以是实际的字体名称,也可以是通用字体类名称。如果是后者,则将在相应的 rcParams 中查找具体的字体名称。

如果使用 fontfamily=None 构造一个 Text 实例,则字体将设置为 rcParams["font.family"](默认值:['sans-serif']),在现有 Text 实例上调用 set_fontfamily() 时也会进行相同的操作。

参数:
fontname{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
set_fontname(fontname)[source]#

set_fontfamily 的别名。

仅为单向别名:getter 不同。

参数:
fontname{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
set_fontproperties(fp)[source]#

设置控制文本的字体属性。

参数:
fpfont_manager.FontProperties or str or pathlib.Path

如果为 str,则将其解释为由 FontProperties 解析的 fontconfig 模式。如果为 pathlib.Path,则将其解释为字体文件的绝对路径。

set_fontsize(fontsize)[source]#

设置字体大小。

参数:
fontsizefloat or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}

如果为浮点数,则表示字体大小(以磅为单位)。字符串值表示相对于默认字体大小的尺寸。

set_fontstretch(stretch)[source]#

设置字体拉伸(水平凝缩或扩展)。

参数:
stretch{a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
set_fontstyle(fontstyle)[source]#

设置字体样式。

参数:
fontstyle{'normal', 'italic', 'oblique'}
set_fontvariant(variant)[source]#

设置字体变体。

参数:
variant{'normal', 'small-caps'}
set_fontweight(weight)[source]#

设置字体粗细。

参数:
weight{a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
set_ha(align)[source]#

set_horizontalalignment 的别名。

set_horizontalalignment(align)[source]#

设置相对于锚点的水平对齐方式。

另请参阅 Text alignment

参数:
align{'left', 'center', 'right'}
set_linespacing(spacing)[source]#

将行间距设置为字体大小的倍数。

默认行间距为 1.2。

参数:
spacingfloat (multiple of font size)
set_ma(align)[source]#

set_multialignment 的别名。

set_math_fontfamily(fontfamily)[source]#

设置 Matplotlib 渲染的数学文本的字体系列。

这仅影响 Matplotlib 自带的数学渲染器。使用 TeX 渲染时(usetex=True)无效。

参数:
fontfamilystr

字体系列的名称。

可用的字体系列在默认的 matplotlibrc 文件中定义。

另请参阅

get_math_fontfamily
set_multialignment(align)[source]#

设置多行文本的文本对齐方式。

所有行的边界框布局由 horizontalalignment 和 verticalalignment 属性决定。此属性控制文本行在该框内的对齐方式。

参数:
align{'left', 'right', 'center'}
set_name(fontname)[source]#

set_fontname 的别名。

set_parse_math(parse_math)[source]#

覆盖开关,禁用此 Text 的任何数学文本解析。

参数:
parse_mathbool

如果为 False,此 Text 将从不使用数学文本。如果为 True,并且存在偶数个未转义的美元符号,则将使用数学文本。

set_position(xy)[source]#

设置文本的 (x, y) 位置。

参数:
xy(float, float)
set_rotation(s)[source]#

设置文本的旋转角度。

参数:
sfloat or {'vertical', 'horizontal'}

旋转角度,以度为单位,数学正方向(逆时针)。'horizontal' 等于 0,'vertical' 等于 90。

set_rotation_mode(m)[source]#

设置文本旋转模式。

参数:
m{None, 'default', 'anchor'}

如果为 "default",文本将首先旋转,然后根据其水平和垂直对齐方式进行对齐。如果为 "anchor",则在旋转之前进行对齐。传递 None 将把旋转模式设置为 "default"

set_size(fontsize)[source]#

set_fontsize 的别名。

set_stretch(stretch)[source]#

set_fontstretch 的别名。

set_style(fontstyle)[source]#

set_fontstyle 的别名。

set_text(s)[source]#

设置文本字符串 s

它可以包含换行符(\n)或 LaTeX 语法中的数学表达式。

参数:
s对象

任何对象都会转换为其 str 表示形式,但 None 会被转换为一个空字符串。

set_transform_rotates_text(t)[source]#

变换的旋转是否影响文本方向。

参数:
tbool
set_usetex(usetex)[source]#
参数:
usetexbool or None

是否使用 TeX 渲染,None 表示使用 rcParams["text.usetex"](默认值:False)。

set_va(align)[source]#

set_verticalalignment 的别名。

set_variant(variant)[source]#

set_fontvariant 的别名。

set_verticalalignment(align)[source]#

设置相对于锚点的垂直对齐方式。

另请参阅 Text alignment

参数:
align{'baseline', 'bottom', 'center', 'center_baseline', 'top'}
set_weight(weight)[source]#

set_fontweight 的别名。

set_wrap(wrap)[source]#

设置文本是否可以换行。

换行可确保文本限制在(子)图形框内。它不考虑任何其他艺术家(artist)。

参数:
wrapbool

备注

换行不适用于 savefig(..., bbox_inches='tight')(这也由 IPython/Jupyter 中的 %matplotlib inline 在内部使用)。'tight' 设置会重新缩放画布以适应所有内容,并且发生在换行之前。

set_x(x)[source]#

设置文本的 x 位置。

参数:
x浮点数
set_y(y)[source]#

设置文本的 y 位置。

参数:
y浮点数
update(kwargs)[source]#

从字典props更新此艺术家的属性。

参数:
propsdict
update_bbox_position_size(renderer)[source]#

更新边界框的位置和大小。

当在实际绘制边界框之前需要更新边界框的位置和大小时,应使用此方法。

update_from(other)[source]#

other复制属性到self

= 3#
class matplotlib.text.Annotation(text, xy, xytext=None, xycoords='data', textcoords=None, arrowprops=None, annotation_clip=None, **kwargs)[source]#

Bases: Text, _AnnotationBase

一个 Annotation 是一个可以引用特定位置 xyText。可以选择绘制一个从文本指向 xy 的箭头。

属性:
xy

批注的位置。

xycoords

xy 的坐标系。

arrow_patch

一个 FancyArrowPatch,用于从 xytext 指向 xy

用文本 text 注释点 xy

最简单的形式是,文本放置在 *xy* 处。

可选地,文本可以显示在另一个位置 *xytext*。然后可以通过定义 *arrowprops* 来添加一个从文本指向被标注点 *xy* 的箭头。

参数:
textstr

标注的文本内容。

xy(float, float)

要标注的点 *(x, y)*。坐标系统由 *xycoords* 决定。

xytext(float, float),默认值: *xy*

放置文本的位置 *(x, y)*。坐标系统由 *textcoords* 决定。

xycoordssingle or two-tuple of str or Artist or Transform or callable, default: 'data'

给出 *xy* 的坐标系统。支持以下类型的值:

  • 以下字符串之一

    描述

    'figure points'

    从图形左下角开始的点

    'figure pixels'

    从图形左下角开始的像素

    'figure fraction'

    从图形左下角开始的比例

    'subfigure points'

    从子图左下角开始的点

    'subfigure pixels'

    从子图左下角开始的像素

    'subfigure fraction'

    从子图左下角开始的比例

    'axes points'

    从坐标系左下角开始的点

    'axes pixels'

    从坐标系左下角开始的像素

    'axes fraction'

    从坐标系左下角开始的比例

    'data'

    使用被标注对象的坐标系统(默认)

    'polar'

    如果不是原生 'data' 坐标,则为 *(theta, r)*

    请注意,对于父图形,'subfigure pixels' 和 'figure pixels' 是相同的,因此希望代码可在子图中使用的用户可以使用 'subfigure pixels'。

  • An Artist: xy is interpreted as a fraction of the artist's Bbox. E.g. (0, 0) would be the lower left corner of the bounding box and (0.5, 1) would be the center top of the bounding box.

  • A Transform to transform xy to screen coordinates.

  • 具有以下签名之一的函数

    def transform(renderer) -> Bbox
    def transform(renderer) -> Transform
    

    where renderer is a RendererBase subclass.

    The result of the function is interpreted like the Artist and Transform cases above.

  • 一个元组 *(xcoords, ycoords)*,为 *x* 和 *y* 指定独立的坐标系统。*xcoords* 和 *ycoords* 都必须是上述类型之一。

有关更多详细信息,请参阅高级批注

textcoordssingle or two-tuple of str or Artist or Transform or callable, default: value of xycoords

给出 *xytext* 的坐标系统。

所有 *xycoords* 值均有效,以及以下字符串:

描述

'offset points'

与 *xy* 值之间的偏移量(单位:点)

'offset pixels'

与 *xy* 值之间的偏移量(单位:像素)

'offset fontsize'

与 *xy* 值之间的偏移量(相对于字体大小)

arrowpropsdict,可选

用于在位置 xyxytext 之间绘制 FancyArrowPatch 箭头的属性。默认为 None,即不绘制箭头。

出于历史原因,有两种不同的方式指定箭头:“简单”和“花式”

简单箭头

如果 *arrowprops* 不包含键 'arrowstyle',则允许的键为:

描述

width

箭头的宽度(单位:点)

headwidth

箭头头部底部的宽度(单位:点)

headlength

箭头头部的长度(单位:点)

shrink

从两端收缩的总长度比例

?

Any FancyArrowPatch property

箭头附着在文本框的边缘,确切位置(角或中心)取决于其指向。

花式箭头

如果 *arrowprops* 中提供了 'arrowstyle',则使用此选项。

有效键是以下 FancyArrowPatch 参数

描述

arrowstyle

箭头样式

connectionstyle

连接样式

relpos

见下文;默认值为 (0.5, 0.5)

patchA

默认为文本的包围盒

patchB

默认为 None

shrinkA

单位:点。默认值:2 点

shrinkB

单位:点。默认值:2 点

mutation_scale

默认为文本大小(单位:点)

mutation_aspect

默认值为 1

?

Any FancyArrowPatch property

箭头的精确起始点位置由 *relpos* 定义。它是一个文本框相对坐标的元组,其中 (0, 0) 是左下角,(1, 1) 是右上角。支持小于 0 和大于 1 的值,它们指定文本框外部的点。默认为 (0.5, 0.5),因此起始点位于文本框的中心。

annotation_clip布尔值或 None,默认值: None

当标注点 *xy* 在坐标系区域之外时,是否剪裁(即不绘制)标注。

  • 如果为 *True*,当 *xy* 在坐标系之外时,标注将被剪裁。

  • 如果为 *False*,标注将始终被绘制。

  • 如果为 *None*,当 *xy* 在坐标系之外且 *xycoords* 为 'data' 时,标注将被剪裁。

**kwargs

额外的 kwargs 将传递给 Text

返回:
Annotation

另请参阅

注解
property anncoords#

用于 Annotation.xyann 的坐标系。

contains(mouseevent)[source]#

返回鼠标事件是否发生在文本的轴对齐边界框内。

draw(renderer)[source]#

使用给定的渲染器绘制 Artist(及其子对象)。

如果 artist 不可见(Artist.get_visible 返回 False),则此操作无效。

参数:
rendererRendererBase 子类。

备注

此方法在 Artist 子类中被覆盖。

get_anncoords()[source]#

返回用于 Annotation.xyann 的坐标系。

另请参阅 Annotation 中的 xycoords

get_tightbbox(renderer=None)[source]#

类似于 Artist.get_window_extent,但包含任何裁剪。

参数:
rendererRendererBase subclass, optional

用于绘制图形的渲染器(即 fig.canvas.get_renderer()

返回:
Bbox or None

封闭的边界框(以图形像素坐标表示)。如果剪裁导致没有交集,则返回 None。

get_window_extent(renderer=None)[source]#

返回边界文本的 Bbox,以显示单位表示。

除了内部使用外,这对于在网页上的 png 文件中指定可点击区域也很有用。

参数:
renderer渲染器,可选

计算边界框需要一个渲染器。如果 Artist 已经绘制,渲染器会被缓存;因此,只有在第一次绘制之前调用 get_window_extent 时才需要传递此参数。实践中,通常更容易先触发一次绘制,例如通过调用 draw_without_renderingplt.show()

dpifloat, 可选

用于计算边界框的 dpi 值,默认为 self.get_figure(root=True).dpi不是渲染器 dpi);例如,如果需要与以自定义 dpi 值保存的图形区域匹配,则应进行设置。

set(*, agg_filter=<UNSET>, alpha=<UNSET>, animated=<UNSET>, anncoords=<UNSET>, annotation_clip=<UNSET>, antialiased=<UNSET>, backgroundcolor=<UNSET>, bbox=<UNSET>, clip_box=<UNSET>, clip_on=<UNSET>, clip_path=<UNSET>, color=<UNSET>, fontfamily=<UNSET>, fontproperties=<UNSET>, fontsize=<UNSET>, fontstretch=<UNSET>, fontstyle=<UNSET>, fontvariant=<UNSET>, fontweight=<UNSET>, gid=<UNSET>, horizontalalignment=<UNSET>, in_layout=<UNSET>, label=<UNSET>, linespacing=<UNSET>, math_fontfamily=<UNSET>, mouseover=<UNSET>, multialignment=<UNSET>, parse_math=<UNSET>, path_effects=<UNSET>, picker=<UNSET>, position=<UNSET>, rasterized=<UNSET>, rotation=<UNSET>, rotation_mode=<UNSET>, sketch_params=<UNSET>, snap=<UNSET>, text=<UNSET>, transform=<UNSET>, transform_rotates_text=<UNSET>, url=<UNSET>, usetex=<UNSET>, verticalalignment=<UNSET>, visible=<UNSET>, wrap=<UNSET>, x=<UNSET>, y=<UNSET>, zorder=<UNSET>)[source]#

一次性设置多个属性。

支持的属性包括:

属性

描述

agg_filter

一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量

alpha

浮点数或 None

animated

布尔值

anncoords

未知

annotation_clip(标注裁剪)

布尔值或 None

antialiased

布尔值

backgroundcolor

color

bbox

包含 patches.FancyBboxPatch 属性的字典

clip_box

BboxBase 或 None

clip_on

布尔值

clip_path

Patch 或 (Path, Transform) 或 None

colorc

color

figure

未知

fontfamilyfamilyfontname

{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}

fontpropertiesfontfont_properties

font_manager.FontPropertiesstrpathlib.Path

fontsizesize

float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}

fontstretchstretch

{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}

fontstylestyle

{'normal', 'italic', 'oblique'}

fontvariantvariant

{'normal', 'small-caps'}

fontweightweight

{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}

gid

str

horizontalalignmentha

{'left', 'center', 'right'}

in_layout

布尔值

label

对象

linespacing

float (字体大小的倍数)

math_fontfamily

str

mouseover

布尔值

multialignmentma

{'left', 'right', 'center'}

parse_math

布尔值

path_effects

AbstractPathEffect 的列表

picker

None 或 布尔值 或 浮点数 或 可调用对象

position

(浮点数, 浮点数)

rasterized

布尔值

rotation

float 或 {'vertical', 'horizontal'}

rotation_mode

{None, 'default', 'anchor'}

sketch_params

(scale: 浮点数, length: 浮点数, randomness: 浮点数)

snap

布尔值或 None

text

对象

transform

变换

transform_rotates_text

布尔值

url

str

usetex

布尔值,默认值:rcParams["text.usetex"](默认值:False

verticalalignmentva

{'baseline', 'bottom', 'center', 'center_baseline', 'top'}

visible

布尔值

wrap

布尔值

x

浮点数

y

浮点数

zorder

浮点数

set_anncoords(coords)[source]#

设置用于 Annotation.xyann 的坐标系。

另请参阅 Annotation 中的 xycoords

set_figure(fig)[source]#

设置 Artist 所属的 FigureSubFigure 实例。

参数:
figFigure or SubFigure
update_positions(renderer)[source]#

更新批注文本和箭头补丁的像素位置。

property xyann#

文本位置。

另请参阅 Annotation 中的 xytext

property xycoords#
class matplotlib.text.OffsetFrom(artist, ref_coord, unit='points')[source]#

基类: object

用于处理 Annotation 的可调用辅助类。

参数:
artistArtist or BboxBase or Transform

用于计算偏移量的对象。

ref_coord(float, float)

如果 *artist* 是 ArtistBboxBase,则此值表示偏移原点在 *artist* 边界框分数中的位置。

如果 *artist* 是一个变换,则偏移原点是应用于此值的变换。

unit{'points, 'pixels'}, default: 'points'

用于偏移输入的屏幕单位(像素或点)。

get_unit()[source]#

返回由 __call__ 使用的变换的输入单位。

set_unit(unit)[source]#

设置由 __call__ 使用的变换的输入单位。

参数:
unit{'points', 'pixels'}
class matplotlib.text.TextPath(xy, s, size=None, prop=None, _interpolation_steps=1, usetex=False)[source]#

Bases: Path

从文本创建路径。

从文本创建路径。请注意,它只是一个路径,而不是一个 Artist。你需要使用 PathPatch(或其他 Artist)将此路径绘制到画布上。

参数:
xytuple or array of two float values

文本的位置。如果不需要偏移,请使用 xy=(0, 0)

sstr

要转换为路径的文本。

sizefloat, optional

字体大小(以磅为单位)。默认为通过字体属性 prop 指定的大小。

propFontProperties, optional

字体属性。如果未提供,将使用一个默认的 FontProperties,其参数来自 rcParams

_interpolation_stepsint, optional

(当前被忽略)

usetexbool, default: False

是否使用 TeX 渲染。

示例

以下代码使用 Helvetica 字体从字符串“ABC”创建路径;并从 LaTeX 分数 1/2 创建另一个路径。

from matplotlib.text import TextPath
from matplotlib.font_manager import FontProperties

fp = FontProperties(family="Helvetica", style="italic")
path1 = TextPath((12, 12), "ABC", size=12, prop=fp)
path2 = TextPath((0, 0), r"$\frac{1}{2}$", size=12, usetex=True)

另请参阅将文本用作路径

property codes#

返回代码

get_size()[source]#

获取文本大小。

set_size(size)[source]#

设置文本大小。

property vertices#

如果需要,更新缓存路径后返回。

class matplotlib.text.TextToPath[source]#

基类: object

将字符串转换为路径的类。

DPI = 72#
FONT_SCALE = 100.0#
get_glyphs_mathtext(prop, s, glyph_map=None, return_new_glyphs_only=False)[source]#

解析数学文本字符串 s 并将其转换为(顶点,代码)对。

get_glyphs_tex(prop, s, glyph_map=None, return_new_glyphs_only=False)[source]#

使用 usetex 模式将字符串 s 转换为顶点和代码。

get_glyphs_with_font(font, s, glyph_map=None, return_new_glyphs_only=False)[source]#

使用提供的 TTF 字体将字符串 s 转换为顶点和代码。

get_text_path(prop, s, ismath=False)[source]#

将文本 s 转换为路径(matplotlib.path.Path 的顶点和代码元组)。

参数:
propFontProperties

文本的字体属性。

sstr

要转换的文本。

ismath{False, True, "TeX"}

如果为 True,使用数学文本解析器。如果为“TeX”,使用 TeX 进行渲染。

返回:
vertslist

包含顶点 (x, y) 坐标的数组列表。

codeslist

路径代码列表。

示例

从文本创建顶点和代码列表,并从中创建 Path

from matplotlib.path import Path
from matplotlib.text import TextToPath
from matplotlib.font_manager import FontProperties

fp = FontProperties(family="Comic Neue", style="italic")
verts, codes = TextToPath().get_text_path(fp, "ABC")
path = Path(verts, codes, closed=False)

另请参阅 TextPath,了解从文本创建路径的更直接方法。

get_text_width_height_descent(s, prop, ismath)[source]#