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

简介: 文本属性及布局 原文:Text properties and layout 译者:飞龙 协议:CC BY-NC-SA 4.0matplotlib.text.Text实例有各种属性,可以通过关键字参数配置文本命令(例如,title(),xlabel()和text())。

文本属性及布局

原文: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 [ 点的尺寸
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()

相关文章
|
4月前
|
人工智能 算法 API
Matplotlib从入门到精通03-布局格式定方圆
Matplotlib从入门到精通03-布局格式定方圆
Matplotlib从入门到精通03-布局格式定方圆
|
6月前
|
数据可视化 关系型数据库 索引
【100天精通Python】Day63:Python可视化_Matplotlib绘制子图,子图网格布局属性设置等示例+代码
【100天精通Python】Day63:Python可视化_Matplotlib绘制子图,子图网格布局属性设置等示例+代码
93 0
|
API Python
python matplotlib.axes相关属性设置(绘图方式、坐标轴、坐标刻度、文本等)
为什么要用 ax ,而不是 plt 呢? 因为在绘制子图过程中,对于每一个子图的不同设置,ax 可以直接实现对于单个子图的设定,因此掌握必要的 ax 设置命令尤为重要!
python matplotlib.axes相关属性设置(绘图方式、坐标轴、坐标刻度、文本等)
|
数据可视化 数据挖掘 Python
数据分析三剑客【AIoT阶段一(下)】(十万字博文 保姆级讲解)—Matplotlib—数据可视化高级—多图布局(1)(五)
你好,感谢你能点进来本篇博客,请不要着急退出,相信我,如果你有一定的 Python 基础,想要学习 Python数据分析的三大库:numpy,pandas,matplotlib;这篇文章不会让你失望,本篇博客是 【AIoT阶段一(下)】 的内容:Python数据分析,
148 0
数据分析三剑客【AIoT阶段一(下)】(十万字博文 保姆级讲解)—Matplotlib—数据可视化高级—多图布局(1)(五)
|
数据可视化 数据挖掘 Python
数据分析三剑客【AIoT阶段一(下)】(十万字博文 保姆级讲解)—Matplotlib—数据可视化高级—多图布局(2)(六)
你好,感谢你能点进来本篇博客,请不要着急退出,相信我,如果你有一定的 Python 基础,想要学习 Python数据分析的三大库:numpy,pandas,matplotlib;这篇文章不会让你失望,本篇博客是 【AIoT阶段一(下)】 的内容:Python数据分析,
96 0
数据分析三剑客【AIoT阶段一(下)】(十万字博文 保姆级讲解)—Matplotlib—数据可视化高级—多图布局(2)(六)
|
Web App开发 Python
01 matplotlib - 折线图、绘图属性、Web安全色、子图、保存画板
=== 折线图 - 头文件 === 解决: 1、中文显示问题 2、图像行内外显示 3、处理闪退问题 import matplotlib.pyplot as plt import numpy as np import pandas as pd # 解决中文显示问题 import matplotlib as mpl mpl.
1900 0
|
Python
Matplotlib 中文用户指南 3.7 变换教程
变换教程 原文:Transformations Tutorial 译者:飞龙 协议:CC BY-NC-SA 4.0 像任何图形包一样,matplotlib 建立在变换框架之上,以便在坐标系,用户数据坐标系,轴域坐标系,图形坐标系和显示坐标系之间轻易变换。
1121 0
|
Python 数据可视化 Shell
Matplotlib 中文用户指南 3.2 图像教程
图像教程 原文:Image tutorial 译者:飞龙 协议:CC BY-NC-SA 4.0 启动命令 首先,让我们启动 IPython。
1142 0
|
索引 Python
Matplotlib 中文用户指南 3.3 使用 GridSpec 自定义子图位置
使用 GridSpec 自定义子图位置 原文:Customizing Location of Subplot Using GridSpec 译者:飞龙 协议:CC BY-NC-SA 4.0 GridSpec 指定子图将放置的网格的几何位置。
1266 0
|
Python 算法 API
Matplotlib 中文用户指南 3.5 密致布局指南
密致布局指南 原文:Tight Layout guide 译者:飞龙 协议:CC BY-NC-SA 4.0 tight_layout会自动调整子图参数,使之填充整个图像区域。
957 0