Matplotlib 中文用戶指南 4.3 文本屬性及布局

文本屬性及布局

原文:Text properties and layout

譯者:飛龍

協議:CC BY-NC-SA 4.0

matplotlib.text.Text實例有各種屬性,可以通過關鍵字參數配置文本命令(例如,title()xlabel()text())。

屬性 值類型
alpha 浮點
backgroundcolor 任何 matplotlib 顏色
bbox rectangle prop dict plus key 'pad' which is a pad in points
clip_box matplotlib.transform.Bbox 實例
clip_on [True / False]
clip_path PathTransformPatch 實例
color 任何 matplotlib 顏色
family [ 'serif' / 'sans-serif' / 'cursive' / 'fantasy' / 'monospace' ]
fontproperties matplotlib.font_manager.FontProperties 實例
horizontalalignment or ha [ 'center' / 'right' / 'left' ]
label 任何字符串
linespacing 浮點
multialignment ['left' / 'right' / 'center' ]
name or fontname 字符串,例如 ['Sans' / 'Courier' / 'Helvetica' ...]
picker [None / 浮點 / 布爾值 / 可調用對象]`
position (x,y)
rotation [ 角度制的角度 / 'vertical' / 'horizontal'
size or fontsize [ 點的尺寸 相對尺寸,例如 ['smaller', 'x-large' ]
style or fontstyle [ 'normal' / 'italic' / 'oblique']
text 字符串或任何可使用'%s'打印的東西
transform matplotlib.transform 實例
variant [ 'normal' / 'small-caps' ]
verticalalignment or va [ 'center' / 'top' / 'bottom' / 'baseline' ]
visible [True / False]
weight or fontweight [ 'normal' / 'bold' / 'heavy' / 'light' / 'ultrabold' / 'ultralight']
x 浮點
y 浮點
zorder 任意數值

你可以使用對齊參數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 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()
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容