文本屬性及布局
譯者:飛龍
協(xié)議:CC BY-NC-SA 4.0
matplotlib.text.Text
實(shí)例有各種屬性,可以通過關(guān)鍵字參數(shù)配置文本命令(例如,title()
砸逊,xlabel()
和text()
)篡帕。
屬性 | 值類型 | |
---|---|---|
alpha | 浮點(diǎn) | |
backgroundcolor | 任何 matplotlib 顏色 | |
bbox | rectangle prop dict plus key 'pad' which is a pad in points | |
clip_box |
matplotlib.transform.Bbox 實(shí)例 |
|
clip_on | [True / False] |
|
clip_path |
Path 漫雕,Transform 或Patch 實(shí)例 |
|
color | 任何 matplotlib 顏色 | |
family | [ 'serif' / 'sans-serif' / 'cursive' / 'fantasy' / 'monospace' ] |
|
fontproperties |
matplotlib.font_manager.FontProperties 實(shí)例 |
|
horizontalalignment or ha | [ 'center' / 'right' / 'left' ] |
|
label | 任何字符串 | |
linespacing | 浮點(diǎn) | |
multialignment | ['left' / 'right' / 'center' ] |
|
name or fontname | 字符串还绘,例如 ['Sans' / 'Courier' / 'Helvetica' ...]
|
|
picker | [None / 浮點(diǎn) / 布爾值 / 可調(diào)用對象]` |
|
position | (x,y) |
|
rotation | [ 角度制的角度 / 'vertical' / 'horizontal' | |
size or fontsize | [ 點(diǎn)的尺寸 | 相對尺寸窗价,例如 ['smaller', 'x-large' ]
|
style or fontstyle | [ 'normal' / 'italic' / 'oblique'] |
|
text | 字符串或任何可使用'%s' 打印的東西 |
|
transform |
matplotlib.transform 實(shí)例 |
|
variant | [ 'normal' / 'small-caps' ] |
|
verticalalignment or va | [ 'center' / 'top' / 'bottom' / 'baseline' ] |
|
visible | [True / False] |
|
weight or fontweight | [ 'normal' / 'bold' / 'heavy' / 'light' / 'ultrabold' / 'ultralight'] |
|
x | 浮點(diǎn) | |
y | 浮點(diǎn) | |
zorder | 任意數(shù)值 |
你可以使用對齊參數(shù)horizontalalignment
捌刮,verticalalignment
和multialignment
來布置文本碰煌。 horizontalalignment
控制文本的x
位置參數(shù)表示文本邊界框的左邊,中間或右邊绅作。 verticalalignment
控制文本的y
位置參數(shù)表示文本邊界框的底部芦圾,中心或頂部。 multialignment
俄认,僅對于換行符分隔的字符串个少,控制不同的行是左,中還是右對齊眯杏。 這里是一個使用text()
命令顯示各種對齊方式的例子夜焦。 在整個代碼中使用transform = ax.transAxes
,表示坐標(biāo)相對于軸邊界框給出岂贩,其中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 are 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()