【3.3】matplotlib文本标注(text、annotate)

一、text

添加文本

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

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

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

位置调整:

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()

二、annotate

2.1 参数说明

plt.annotate(s, xy, xytext=None, xycoords=None, textcoords)

在图表中进行标注,重要参数说明如下:

s:标注的字符串
xy:标注所在的位置
xytext:如果有规定,则文字位置以该参数为准,通过一个箭头指向 xy 所在位置
xycoords:xy 坐标系统
‘data’:沿用所标注对象的坐标方式
‘figure points’:从画布的左下角,按照“点”来计算
‘figure pixels’:从画布的左下角,按照“像素”来计算
‘figure fraction’:从画布的左下角,按照“比例”来计算
‘axes points’:从图表的左下角,按照“点”来计算
‘axes pixels’:从图表的左下角,按照“像素”来计算
‘axes fraction’:从图表的左下角,按照“比例”来计算
‘polar’:极坐标
textcoords:xytext 的坐标体系
包括所有 xycoords 的方式
‘offset points’:从 xy 处,按照“点”来计算
‘offset pixels’:从 xy 处,按照“像素”来计算

2.2 具体例子

内容固定在左上角的位置

plt.annotate('something', xy=(0.05, 0.9), xycoords='axes fraction',color='red')

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学