from matplotlib\text.py
@cbook._define_aliases({
"color": ["c"],
"fontfamily": ["family"],
"fontproperties": ["font_properties"],
"horizontalalignment": ["ha"],
"multialignment": ["ma"],
"fontname": ["name"],
"fontsize": ["size"],
"fontstretch": ["stretch"],
"fontstyle": ["style"],
"fontvariant": ["variant"],
"verticalalignment": ["va"],
"fontweight": ["weight"],
})
class Text(Artist):
"""Handle storing and drawing of text in window or data coordinates."""
在窗口或数据坐标中处理文本的存储和绘制
zorder = 3
_cached = cbook.maxdict(50)
def __repr__(self):
return "Text(%s, %s, %s)" % (self._x, self._y, repr(self._text))
def __init__(self,
x=0, y=0, text='',
color=None, # defaults to rc params
verticalalignment='baseline',
horizontalalignment='left',
multialignment=None,
fontproperties=None, # defaults to FontProperties()
rotation=None,
linespacing=None,
rotation_mode=None,
usetex=None, # defaults to rcParams['text.usetex']
wrap=False,
**kwargs
):
"""
Create a `.Text` instance at *x*, *y* with string *text*.
Valid kwargs are
%(Text)s
"""
Artist.__init__(self)
self._x, self._y = x, y
if color is None:
color = rcParams['text.color']
if fontproperties is None:
fontproperties = FontProperties()
elif isinstance(fontproperties, str):
fontproperties = FontProperties(fontproperties)
self._text = ''
self.set_text(text)
self.set_color(color)
self.set_usetex(usetex)
self.set_wrap(wrap)
self.set_verticalalignment(verticalalignment)
self.set_horizontalalignment(horizontalalignment)
self._multialignment = multialignment
self._rotation = rotation
self._fontproperties = fontproperties
self._bbox_patch = None # a FancyBboxPatch instance
self._renderer = None
if linespacing is None:
linespacing = 1.2 # Maybe use rcParam later.
self._linespacing = linespacing
self.set_rotation_mode(rotation_mode)
self.update(kwargs)
参考文章:matplotlib.pyplot.text()结构及用法||参数详解
参数详解
-
x, y : scalars 防止text的位置
-
s : str 内容text
-
fontdict : dictionary, optional, default: None 一个定义s格式的dict
-
withdash : boolean, optional, default: False。如果True则创建一个 TextWithDash实例。
以下为其他常用参数1:
- fontsize设置字体大小,默认12,可选参数 [‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’,‘x-large’, ‘xx-large’]
fontweight设置字体粗细,可选参数 [‘light’, ‘normal’, ‘medium’, ‘semibold’, ‘bold’, ‘heavy’, ‘black’]
-
fontstyle设置字体类型,可选参数[ ‘normal’ | ‘italic’ | ‘oblique’ ],italic斜体,oblique倾斜
-
verticalalignment设置水平对齐方式 ,可选参数 : ‘center’ , ‘top’ , ‘bottom’ ,‘baseline’
-
horizontalalignment设置垂直对齐方式,可选参数:left,right,center
-
rotation(旋转角度)可选参数为:vertical,horizontal 也可以为数字
-
alpha透明度,参数值0至1之间
-
backgroundcolor标题背景颜色
-
bbox给标题增加外框 ,常用参数如下:
-
boxstyle方框外形
-
facecolor(简写fc)背景颜色
-
edgecolor(简写ec)边框线条颜色
-
edgewidth边框线条大小