文本属性和布局#

使用 Matplotlib 控制文本及其布局的属性。

matplotlib.text.Text 实例有多种属性,可以通过 set_titleset_xlabeltext 等的关键字参数进行配置。

属性

值类型

alpha

float

backgroundcolor

任何 matplotlib 颜色

bbox

Rectangle 属性字典加上键 'pad',这是一个以点为单位的填充。

clip_box

一个 matplotlib.transform.Bbox 实例

clip_on

布尔值

clip_path

一个 Path 实例和一个 Transform 实例,一个 Patch

颜色

任何 matplotlib 颜色

家庭

[ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]

fontproperties

FontProperties

horizontalalignment 或 ha

[ 'center' | 'right' | 'left' ]

标签

任意字符串

行间距

float

多重对齐

['左' | '右' | '中' ]

名称或字体名称

字符串,例如,['Sans' | 'Courier' | 'Helvetica' ...]

选择器

[None|float|bool|callable]

位置

(x, y)

旋转

[ 角度(度) | '垂直' | '水平' ]

大小或字体大小

[ 以点为单位的大小 | 相对大小,例如 'smaller''x-large' ]

风格或字体风格

[ 'normal' | 'italic' | 'oblique' ]

文本

字符串或任何可以用 '%s' 转换的打印内容

变换

Transform 子类

变体

[ 'normal' | 'small-caps' ]

垂直对齐或va

[ 'center' | 'top' | 'bottom' | 'baseline' ]

可见

布尔值

weight 或 fontweight

[ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']

x

float

y

float

zorder

任意数量

你可以使用对齐参数 horizontalalignmentverticalalignmentmultialignment 来布局文本。horizontalalignment 控制文本的 x 位置参数指示文本边界框的左侧、中心还是右侧。verticalalignment 控制文本的 y 位置参数指示文本边界框的底部、中心还是顶部。multialignment 仅适用于换行分隔的字符串,控制不同行是左对齐、居中还是右对齐。以下是一个示例,使用 text() 命令展示各种对齐可能性。代码中使用 transform=ax.transAxes 表示坐标是相对于 Axes 边界框给出的,其中 (0, 0) 是 Axes 的左下角,(1, 1) 是右上角。

import matplotlib.pyplot as plt

import matplotlib.patches as patches

# build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])

# axes coordinates: (0, 0) is bottom left and (1, 1) is upper right
p = patches.Rectangle(
    (left, bottom), width, height,
    fill=False, transform=ax.transAxes, clip_on=False
    )

ax.add_patch(p)

ax.text(left, bottom, 'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, bottom, 'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right, bottom, 'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, 0.5*(bottom+top), 'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, 0.5*(bottom+top), 'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
        horizontalalignment='center',
        verticalalignment='center',
        fontsize=20, color='red',
        transform=ax.transAxes)

ax.text(right, 0.5*(bottom+top), 'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, top, 'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

ax.set_axis_off()
plt.show()
text props

默认字体#

基础默认字体由一组 rcParams 控制。要设置数学表达式的字体,请使用以 mathtext 开头的 rcParams(参见 mathtext)。

rcParam

用法

'font.family'

字体族列表(安装在用户机器上)和/或 {'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'}

'font.style'

默认样式,例如 'normal''italic'

'font.variant'

默认变体,例如 'normal'``'small-caps'``(未测试)

'font.stretch'

默认拉伸,例如 'normal'``'condensed'``(未完成)

'font.weight'

默认权重。可以是字符串或整数。

'font.size'

默认字体大小(以点为单位)。相对字体大小('large', 'x-small')是根据此大小计算的。

Matplotlib 可以使用用户计算机上安装的字体族,例如 Helvetica、Times 等。字体族也可以通过通用族别名来指定,如 ({'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'})。

备注

要访问可用字体的完整列表::

matplotlib.font_manager.get_font_names()

通用族别名与实际字体族之间的映射(在 默认 rcParams 中提到)由以下 rcParams 控制:

基于CSS的通用字体族别名

带有映射的 rcParam

'serif'

'font.serif'

'monospace'

'font.monospace'

'fantasy'

'font.fantasy'

'cursive'

'font.cursive'

{'sans', 'sans serif', 'sans-serif'}

'font.sans-serif'

如果在 'font.family' 中出现任何通用字体族名称,我们将用相应的 rcParam 映射中的所有条目替换该条目。例如:

matplotlib.rcParams['font.family'] = ['Family1', 'serif', 'Family2']
matplotlib.rcParams['font.serif'] = ['SerifFamily1', 'SerifFamily2']

# This is effectively translated to:
matplotlib.rcParams['font.family'] = ['Family1', 'SerifFamily1', 'SerifFamily2', 'Family2']

带有非拉丁字形的文本#

自 v2.0 版本起,默认字体,DejaVu,包含了许多西方字母的字形,但不包括其他脚本,如中文、韩文或日文。

要将默认字体设置为支持所需代码点的字体,请将字体名称添加到 'font.family' (推荐),或添加到所需的别名列表中。:

# first method
matplotlib.rcParams['font.family'] = ['Source Han Sans TW', 'sans-serif']

# second method
matplotlib.rcParams['font.family'] = ['sans-serif']
matplotlib.rcParams['sans-serif'] = ['Source Han Sans TW', ...]

通用字体族别名列表包含的字体要么是与 Matplotlib 一起提供的(因此它们有 100% 的找到机会),要么是那些在大多数系统中极有可能存在的字体。

在设置自定义字体系列时,一个好的做法是在字体系列列表的末尾添加一个通用系列作为最后的手段。

你也可以在你的 .matplotlibrc 文件中设置它:

font.family: Source Han Sans TW, Arial, sans-serif

要在每个艺术家基础上控制使用的字体,请使用 文本属性和布局 中记录的 namefontnamefontproperties 关键字参数。

在Linux上,fc-list 可以是一个有用的工具来发现字体名称;例如

$ fc-list :lang=zh family
Noto to Sans Mono CJK TC,Noto Sans Mono CJK TC Bold
Noto Sans CJK TC,Noto Sans CJK TC Medium
Noto Sans CJK TC,Noto Sans CJK TC DemiLight
Noto Sans CJK KR,Noto Sans CJK KR Black
Noto Sans CJK TC,Noto Sans CJK TC Black
Noto Sans Mono CJK TC,Noto Sans Mono CJK TC Regular
Noto Sans CJK SC,Noto Sans CJK SC Light

列出所有支持中文的字体。

由 Sphinx-Gallery 生成的图库