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' 关键字参数,但默认情况下它不会添加到图例的句柄中。
有效的关键字参数有:
属性
描述
一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量
浮点数或 None
布尔值
布尔值
包含
patches.FancyBboxPatch
属性的字典未知
未知
未知
fontfamily
或family
或fontname
{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
{'normal', 'italic', 'oblique'}
{'normal', 'small-caps'}
{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
str
{'left', 'center', 'right'}
布尔值
对象
float (字体大小的倍数)
str
布尔值
{'left', 'right', 'center'}
布尔值
None 或 布尔值 或 浮点数 或 可调用对象
(浮点数, 浮点数)
布尔值
float 或 {'vertical', 'horizontal'}
{None, 'default', 'anchor'}
(scale: 浮点数, length: 浮点数, randomness: 浮点数)
布尔值或 None
对象
布尔值
str
布尔值,默认值:
rcParams["text.usetex"]
(默认值:False
){'baseline', 'bottom', 'center', 'center_baseline', 'top'}
布尔值
布尔值
浮点数
浮点数
浮点数
- draw(renderer)[source]#
使用给定的渲染器绘制 Artist(及其子对象)。
如果 artist 不可见(
Artist.get_visible
返回 False),则此操作无效。- 参数:
- renderer
RendererBase
子类。
- renderer
备注
此方法在 Artist 子类中被覆盖。
- get_bbox_patch()[source]#
返回 bbox 补丁,如果未创建
patches.FancyBboxPatch
则返回 None。
- get_family()[source]#
get_fontfamily
的别名。
- get_font()[source]#
get_fontproperties
的别名。
- get_font_properties()[source]#
get_fontproperties
的别名。
- get_math_fontfamily()[source]#
返回 Matplotlib 渲染的数学文本的字体系列名称。
默认值为
rcParams["mathtext.fontset"]
(默认值:'dejavusans'
)。另请参阅
- get_name()[source]#
get_fontname
的别名。
- get_size()[source]#
get_fontsize
的别名。
- get_style()[source]#
get_fontstyle
的别名。
- 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_rendering
或plt.show()
。- dpifloat, 可选
用于计算边界框的 dpi 值,默认为
self.get_figure(root=True).dpi
(不是渲染器 dpi);例如,如果需要与以自定义 dpi 值保存的图形区域匹配,则应进行设置。
- 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]#
一次性设置多个属性。
支持的属性包括:
属性
描述
一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量
浮点数或 None
布尔值
布尔值
包含
patches.FancyBboxPatch
属性的字典未知
未知
未知
{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
{'normal', 'italic', 'oblique'}
{'normal', 'small-caps'}
{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
str
{'left', 'center', 'right'}
布尔值
对象
float (字体大小的倍数)
str
布尔值
{'left', 'right', 'center'}
布尔值
None 或 布尔值 或 浮点数 或 可调用对象
(浮点数, 浮点数)
布尔值
float 或 {'vertical', 'horizontal'}
{None, 'default', 'anchor'}
(scale: 浮点数, length: 浮点数, randomness: 浮点数)
布尔值或 None
对象
布尔值
str
布尔值,默认值:
rcParams["text.usetex"]
(默认值:False
){'baseline', 'bottom', 'center', 'center_baseline', 'top'}
布尔值
布尔值
浮点数
浮点数
浮点数
- set_antialiased(antialiased)[source]#
设置是否使用抗锯齿渲染。
- 参数:
- antialiasedbool
备注
抗锯齿将由
rcParams["text.antialiased"]
(默认值:True
)决定,如果文本包含数学表达式,则参数 antialiased 将不起作用。
- set_bbox(rectprops)[source]#
在自身周围绘制一个边界框。
- 参数:
- rectpropsdict with properties for
patches.FancyBboxPatch
默认的 boxstyle 为 'square'。`patches.FancyBboxPatch` 的变异比例设置为字体大小。
- rectpropsdict with properties for
示例
t.set_bbox(dict(facecolor='red', alpha=0.5))
- set_clip_box(clipbox)[source]#
设置艺术家的裁剪
Bbox
。- 参数:
- clipbox
BboxBase
or None 通常从
TransformedBbox
创建。例如,TransformedBbox(Bbox([[0, 0], [1, 1]]), ax.transAxes)
是添加到 Axes 的 Artist 的默认裁剪。
- clipbox
- set_clip_path(path, transform=None)[source]#
设置艺术家的裁剪路径。
- 参数:
- path
Patch
orPath
orTransformedPath
or None 裁剪路径。如果给定一个
Path
,则必须同时提供 transform。如果为 *None*,则移除之前设置的裁剪路径。- transform
Transform
, optional 仅当 *path* 是
Path
时使用,在这种情况下,给定的Path
将使用 *transform* 转换为TransformedPath
。
- path
备注
为了提高效率,如果 *path* 是
Rectangle
,此方法会将裁剪框设置为对应的矩形,并将裁剪路径设置为None
。出于技术原因(支持
set
),也可以接受元组(*path*,*transform*)作为单个位置参数。
- 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]#
设置控制文本的字体属性。
- 参数:
- fp
font_manager.FontProperties
orstr
orpathlib.Path
如果为
str
,则将其解释为由FontProperties
解析的 fontconfig 模式。如果为pathlib.Path
,则将其解释为字体文件的绝对路径。
- fp
- 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_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_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 文件中定义。
另请参阅
- 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_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_usetex(usetex)[source]#
- 参数:
- usetexbool or None
是否使用 TeX 渲染,
None
表示使用rcParams["text.usetex"]
(默认值:False
)。
- 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' 设置会重新缩放画布以适应所有内容,并且发生在换行之前。
- = 3#
- class matplotlib.text.Annotation(text, xy, xytext=None, xycoords='data', textcoords=None, arrowprops=None, annotation_clip=None, **kwargs)[source]#
Bases:
Text
,_AnnotationBase
一个
Annotation
是一个可以引用特定位置 xy 的Text
。可以选择绘制一个从文本指向 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
orTransform
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'sBbox
. 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
andTransform
cases above.一个元组 *(xcoords, ycoords)*,为 *x* 和 *y* 指定独立的坐标系统。*xcoords* 和 *ycoords* 都必须是上述类型之一。
有关更多详细信息,请参阅高级批注。
- textcoordssingle or two-tuple of str or
Artist
orTransform
or callable, default: value of xycoords 给出 *xytext* 的坐标系统。
所有 *xycoords* 值均有效,以及以下字符串:
值
描述
'offset points'
与 *xy* 值之间的偏移量(单位:点)
'offset pixels'
与 *xy* 值之间的偏移量(单位:像素)
'offset fontsize'
与 *xy* 值之间的偏移量(相对于字体大小)
- arrowpropsdict,可选
用于在位置 xy 和 xytext 之间绘制
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
。
- 返回:
另请参阅
- property anncoords#
用于
Annotation.xyann
的坐标系。
- draw(renderer)[source]#
使用给定的渲染器绘制 Artist(及其子对象)。
如果 artist 不可见(
Artist.get_visible
返回 False),则此操作无效。- 参数:
- renderer
RendererBase
子类。
- renderer
备注
此方法在 Artist 子类中被覆盖。
- get_anncoords()[source]#
返回用于
Annotation.xyann
的坐标系。另请参阅
Annotation
中的 xycoords。
- get_tightbbox(renderer=None)[source]#
类似于
Artist.get_window_extent
,但包含任何裁剪。- 参数:
- renderer
RendererBase
subclass, optional 用于绘制图形的渲染器(即
fig.canvas.get_renderer()
)
- renderer
- 返回:
Bbox
or None封闭的边界框(以图形像素坐标表示)。如果剪裁导致没有交集,则返回 None。
- get_window_extent(renderer=None)[source]#
返回边界文本的
Bbox
,以显示单位表示。除了内部使用外,这对于在网页上的 png 文件中指定可点击区域也很有用。
- 参数:
- renderer渲染器,可选
计算边界框需要一个渲染器。如果 Artist 已经绘制,渲染器会被缓存;因此,只有在第一次绘制之前调用
get_window_extent
时才需要传递此参数。实践中,通常更容易先触发一次绘制,例如通过调用draw_without_rendering
或plt.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]#
一次性设置多个属性。
支持的属性包括:
属性
描述
一个过滤函数,它接受一个 (m, n, 3) 浮点数组和一个 dpi 值,并返回一个 (m, n, 3) 数组以及图像左下角的两个偏移量
浮点数或 None
布尔值
未知
annotation_clip(标注裁剪)
布尔值或 None
布尔值
包含
patches.FancyBboxPatch
属性的字典BboxBase
或 None布尔值
Patch 或 (Path, Transform) 或 None
未知
fontfamily
或family
或fontname
{FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
float 或 {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}
{0-1000 范围内的数值, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
{'normal', 'italic', 'oblique'}
{'normal', 'small-caps'}
{0-1000 范围内的数值, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
str
{'left', 'center', 'right'}
布尔值
对象
float (字体大小的倍数)
str
布尔值
{'left', 'right', 'center'}
布尔值
None 或 布尔值 或 浮点数 或 可调用对象
(浮点数, 浮点数)
布尔值
float 或 {'vertical', 'horizontal'}
{None, 'default', 'anchor'}
(scale: 浮点数, length: 浮点数, randomness: 浮点数)
布尔值或 None
对象
布尔值
str
布尔值,默认值:
rcParams["text.usetex"]
(默认值:False
){'baseline', 'bottom', 'center', 'center_baseline', 'top'}
布尔值
布尔值
浮点数
浮点数
浮点数
- set_anncoords(coords)[source]#
设置用于
Annotation.xyann
的坐标系。另请参阅
Annotation
中的 xycoords。
- property xyann#
文本位置。
另请参阅
Annotation
中的 xytext。
- property xycoords#
- class matplotlib.text.OffsetFrom(artist, ref_coord, unit='points')[source]#
基类:
object
用于处理
Annotation
的可调用辅助类。- 参数:
- 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 指定的大小。
- prop
FontProperties
, 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#
返回代码
- 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 的顶点和代码元组)。
- 参数:
- prop
FontProperties
文本的字体属性。
- sstr
要转换的文本。
- ismath{False, True, "TeX"}
如果为 True,使用数学文本解析器。如果为“TeX”,使用 TeX 进行渲染。
- prop
- 返回:
- 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
,了解从文本创建路径的更直接方法。