前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Matplotlib 中文用户指南 4.3 文本属性及布局

Matplotlib 中文用户指南 4.3 文本属性及布局

作者头像
ApacheCN_飞龙
发布2022-12-01 15:40:16
3280
发布2022-12-01 15:40:16
举报
文章被收录于专栏:信数据得永生

文本属性及布局

原文: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

Path,Transform或Patch 实例

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

[ 点的尺寸

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是右上角。

代码语言:javascript
复制
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()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-01-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文本属性及布局
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档