主要讲解以下几个参数:
# 文本显示内容在锚框的左上角显示
axes.text(rect.xy[0], rect.xy[1], labels[i],
va='center', ha='center', fontsize=9, color=text_color,
# facecolor就是锚框的填充色 lw即linewidth 就是锚框的边框的宽度
bbox=dict(facecolor=color, lw=0))
官方解释:
Signature: fig.axes.text(x, y, s, fontdict=None, **kwargs)
Docstring:
Add text to the Axes.
Add the text *s* to the Axes at location *x*, *y* in data coordinates.
Parameters
----------
x, y : float
The position to place the text. By default, this is in data
coordinates. The coordinate system can be changed using the
*transform* parameter. (文本内容放置的位置,默认情况放在数据轴,轴是可以通过transform属性改变的)
s : str
The text.(既然是文本,输入的一定是字符串)
fontdict : dict, default: None
A dictionary to override the default text properties. If fontdict
is None, the defaults are determined by `.rcParams`.(字体字典,字典类型,默认是none可以使用字典类型覆盖默认的文本属性,如果不设置则默认是.rcParams类型)
Returns
-------
`.Text`
The created `.Text` instance.
Other Parameters
----------------
**kwargs : `~matplotlib.text.Text` properties.
Other miscellaneous text parameters.
Properties:
agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha: scalar or None
animated: bool
backgroundcolor: color
bbox: dict with properties for `.patches.FancyBboxPatch`(patches.FancyBboxPatch`类型的字典属性,至于这个类型可以参考下边的链接)
clip_box: `.Bbox`
clip_on: bool
clip_path: Patch or (Path, Transform) or None
color or c: color(文本字体的颜色)
contains: unknown
figure: `.Figure`
fontfamily or family: {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
fontproperties or font or font_properties: `.font_manager.FontProperties` or `str` or `pathlib.Path`
fontsize or size: float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}(字体字号)
fontstretch or stretch: {a numeric value in range 0-1000, 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
fontstyle or style: {'normal', 'italic', 'oblique'}
fontvariant or variant: {'normal', 'small-caps'}
fontweight or weight: {a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'}
gid: str
horizontalalignment or ha: {'center', 'right', 'left'}(字体的水平排列方式(学过前端的都知道))
in_layout: bool
label: object
linespacing: float (multiple of font size)
math_fontfamily: str
multialignment or ma: {'left', 'right', 'center'}
path_effects: `.AbstractPathEffect`
picker: None or bool or float or callable
position: (float, float)
rasterized: bool
rotation: float or {'vertical', 'horizontal'}
rotation_mode: {None, 'default', 'anchor'}
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
text: object
transform: `.Transform`
transform_rotates_text: bool
url: str
usetex: bool or None
verticalalignment or va: {'center', 'top', 'bottom', 'baseline', 'center_baseline'}(字体的垂直排列方式「CSS样式」)
visible: bool
wrap: bool
x: float
y: float
zorder: float
Examples
--------
Individual keyword arguments can be used to override any given
parameter::
>>> text(x, y, s, fontsize=12)
The default transform specifies that text is in data coords,
alternatively, you can specify text in axis coords ((0, 0) is
lower-left and (1, 1) is upper-right). The example below places
text in the center of the Axes::
>>> text(0.5, 0.5, 'matplotlib', horizontalalignment='center',
... verticalalignment='center', transform=ax.transAxes)
You can put a rectangular box around the text instance (e.g., to
set a background color) by using the keyword *bbox*. *bbox* is
a dictionary of `~matplotlib.patches.Rectangle`
properties. For example::
>>> text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))
File: ~/miniforge3/envs/pytorchenv/lib/python3.8/site-packages/matplotlib/axes/_axes.py
Type: method
关于bbox参数的补充:
.patches.FancyBboxPatch的详细解释
另外 bbox中的内容其实可以用backgroundcolor=color替换即:
axes.text(rect.xy[0], rect.xy[1], labels[i],
va='center', ha='center', fontsize=9, color=text_color, backgroundcolor=color)
最后的效果是一样一样的