文本属性和布局

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

matplotlib.text.Text 实例具有各种属性,可以通过关键字参数配置这些属性 set_titleset_xlabeltext 等。

财产 价值类型
阿尔法 float
背景色 任何地形图 color
口技 Rectangle Prop Dict Plus键 'pad' 哪一个是点垫
clip_box matplotlib.transform.bbox实例
clip_on 布尔
clip_path Path 实例和A Transform 实例,A Patch
颜色 任何地形图 color
家庭 [ 'serif' | 'sans-serif' | 'cursive' | 'fantasy' | 'monospace' ]
字体属性 FontProperties
水平信号或HA [ 'center' | 'right' | 'left' ]
标签 任何字符串
行间距 float
多重对准 ['left' | 'right' | 'center' ]
名称或字体名称 字符串,例如 ['Sans' | 'Courier' | 'Helvetica' ...]
捡拾器 [无|浮动|布尔|可调用]
位置 (x,y)
旋转 [ angle in degrees | 'vertical' | 'horizontal' ]
大小或字体大小 [ size in points | relative size, e.g., 'smaller', 'x-large' ]
样式或字体样式 [ 'normal' | 'italic' | 'oblique' ]
文本 字符串或任何可通过 '%s' 转换打印的内容
转型 Transform 子类
变体 [ 'normal' | 'small-caps' ]
垂直对准或VA [ 'center' | 'top' | 'bottom' | 'baseline' ]
看得见的 布尔
重量或字体重量 [ 'normal' | 'bold' | 'heavy' | 'light' | 'ultrabold' | 'ultralight']
X float
Y float
Z阶 任意数

可以使用对齐参数设置文本布局 horizontalalignmentverticalalignmentmultialignment . horizontalalignment 控制文本的X位置参数是指示文本边界框的左侧、中间还是右侧。 verticalalignment 控制文本的Y位置参数是指示文本边界框的底部、中心还是顶部。 multialignment ,仅用于换行分隔的字符串,控制不同的行是左对齐、居中还是右对齐。下面是一个使用 text() 显示各种对齐可能性的命令。使用 transform=ax.transAxes 整个代码表示坐标是相对于轴边界框给出的,(0,0)是轴的左下角,(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

默认字体

基本默认字体由一组RCPARAM控制。要设置数学表达式的字体,请使用以 mathtext (见 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' )根据该大小计算。

族别名之间的映射 ({{'cursive', 'fantasy', 'monospace', 'sans', 'sans serif', 'sans-serif', 'serif'}} )实际的字体名称由以下rcparams控制:

家庭别名 带映射的RCPARAM
'serif' 'font.serif'
'monospace' 'font.monospace'
'fantasy' 'font.fantasy'
'cursive' 'font.cursive'
{'sans', 'sans serif', 'sans-serif'} 'font.sans-serif'

它们是字体名称列表。

带非拉丁字符的文本

截至V2.0 default font ,DejaVu包含许多西方字母的字形,但不包含其他文字,如中文、韩语或日语。

要将默认字体设置为支持所需代码点的字体,请将字体名称预先设置为 'font.family' 或所需别名列表:

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

或者把它放在你的 .matplotlibrc 文件::

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

要控制每个艺术家使用的字体,请使用 'name''fontname''fontproperties' Kwargs文件 above .

在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

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

关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery