matplotlib.pyplot.text#

matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)[源代码][源代码]#

向 Axes 添加文本。

在数据坐标中的位置 x, y 处向 Axes 添加文本 s,默认的 horizontalalignmentleftverticalalignmentbaseline。参见 文本对齐

参数:
x, y浮动

文本放置的位置。默认情况下,这是在数据坐标中。可以使用 transform 参数更改坐标系。

sstr

文本。

fontdict字典, 默认: None

不鼓励

不鼓励使用 fontdict。参数应作为单独的关键字参数传递,或使用字典解包 text(..., **fontdict)

一个用于覆盖默认文本属性的字典。如果 fontdict 为 None,则默认值由 rcParams 决定。

返回:
Text

创建的 Text 实例。

其他参数:
**kwargs : Text 属性。文本属性。

其他杂项文本参数。

属性

描述

agg_filter

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

alpha

标量或无

animated

布尔

抗锯齿

布尔

backgroundcolor

color

bbox

带有 patches.FancyBboxPatch 属性的字典

clip_box

未知

clip_on

未知

clip_path

未知

颜色 或 c

color

figure

FigureSubFigure

fontfamily 或 family 或 fontname

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

fontproperties 或 font 或 font_properties

font_manager.FontPropertiesstrpathlib.Path

字体大小 或大小

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

fontstretch 或 stretch

{一个数值范围在0-1000之间, '超压缩', '特压缩', '压缩', '半压缩', '正常', '半扩展', '扩展', '特扩展', '超扩展'}

fontstyle 或样式

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

fontvariant 或 variant

{'normal', 'small-caps'}

fontweight 或 weight

{一个数值范围在0-1000,'超轻','轻','正常','常规','书','中等','罗马','半粗','半粗','半','粗','重','特粗','黑'}

gid

str

horizontalalignment 或 ha

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

in_layout

布尔

标签

对象

行间距

浮动(字体大小的倍数)

math_fontfamily

str

鼠标悬停

布尔

multialignment 或 ma

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

parse_math

布尔

path_effects

AbstractPathEffect 的列表

picker

None 或 bool 或 float 或 callable

位置

(浮点数, 浮点数)

光栅化

布尔

旋转

浮点数或 {'垂直', '水平'}

rotation_mode

{None, 'default', 'anchor'}

sketch_params

(scale: float, length: float, randomness: float)

snap

布尔值或无

文本

对象

transform

Transform

transform_rotates_text

布尔

url

str

usetex

bool, 默认值: rcParams["text.usetex"] (default: False)

垂直对齐 或 va

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

可见

布尔

wrap

布尔

x

浮动

y

浮动

zorder

浮动

注释

备注

这是 axes.Axes.textpyplot 包装器

示例

可以使用单独的关键字参数来覆盖任何给定的参数:

>>> text(x, y, s, fontsize=12)

默认的变换指定文本在数据坐标中,或者,您可以指定文本在轴坐标中((0, 0) 是左下角,(1, 1) 是右上角)。下面的示例将文本放置在 Axes 的中心:

>>> text(0.5, 0.5, 'matplotlib', horizontalalignment='center',
...      verticalalignment='center', transform=ax.transAxes)

你可以通过使用关键字 bbox 在文本实例周围放置一个矩形框(例如,设置背景颜色)。bbox 是一个 Rectangle 属性的字典。例如:

>>> text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))

使用 matplotlib.pyplot.text 的示例#

不同单位中的图形尺寸

Figure size in different units

自动换行文本

Auto-wrap text

文本旋转模式

Text rotation mode

文本框样式

Styling text boxes

使用字典控制文本和标签的样式

Controlling style of text and labels using a dictionary

使用 pyplot 的文本和数学文本

Text and mathtext using pyplot

关闭事件

Close event

transforms.offset_copy

transforms.offset_copy

安斯科姆四重奏

Anscombe's quartet

Pyplot 教程

Pyplot tutorial

路径效果指南

Path effects guide