文字图例
Figure和Axes上的文本
Matplotlib具有广泛的文本支持,包括对数学表达式的支持、对栅格和矢量输出的TrueType支持、具有任意旋转的换行分隔文本以及Unicode支持。
下面的命令是介绍了通过pyplot API和objected-oriented API分别创建文本的方式。
pyplot API |
OO API | description |
---|---|---|
text |
text |
在 Axes 的任意位置添加text。 |
title |
set_title |
在 Axes 添加title |
figtext |
text |
在Figure 的任意位置添加text. |
suptitle |
suptitle |
在 Figure 添加title |
xlabel |
set_xlabel |
在Axes 的x-axis添加label |
ylabel |
set_ylabel |
在Axes 的y-axis添加label |
annotate |
annotate |
向Axes 的任意位置添加带有可选箭头的标注. |
1. text
pyplot API:matplotlib.pyplot.text(x, y, s, fontdict=None, **kwargs)
OO API:Axes.text(self, x, y, s, fontdict=None, **kwargs)
- 参数:此方法接受以下描述的参数:
- s:此参数是要添加的文本。
- xy:此参数是放置文本的点(x,y)。
- fontdict:此参数是一个可选参数,并且是一个覆盖默认文本属性的字典。如果fontdict为None,则由rcParams确定默认值。
返回值:此方法返回作为创建的文本实例的文本。
fontdict主要参数具体介绍,更多参数请参考官网说明:
Property | Description |
---|---|
alpha |
float or None 该参数指透明度,越接近0越透明,越接近1越不透明 |
backgroundcolor |
color 该参数指文本的背景颜色,具体matplotlib支持颜色如下 |
bbox |
dict with properties for patches.FancyBboxPatch 这个是用来设置text周围的box外框 |
color or c |
color 指的是字体的颜色 |
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’} 该参数是指字体的样式是否倾斜等 |
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’} |
horizontalalignment or ha |
{‘center’, ‘right’, ‘left’} 该参数是指选择文本左对齐右对齐还是居中对齐 |
label |
object |
linespacing |
float (multiple of font size) |
position |
(float, float) |
rotation |
float or {‘vertical’, ‘horizontal’} 该参数是指text逆时针旋转的角度,“horizontal”等于0,“vertical”等于90。我们可以根据自己设定来选择合适角度 |
verticalalignment or va |
{‘center’, ‘top’, ‘bottom’, ‘baseline’, ‘center_baseline’} |
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import numpy as np
#fontdict学习的案例
#学习的过程中请尝试更换不同的fontdict字典的内容,以便于更好的掌握
#---------设置字体样式,分别是字体,颜色,宽度,大小
font1 = {
'family': 'SimSun',#华文楷体
'alpha':0.7,#透明度
'color': 'purple',
'weight': 'normal',
'size': 16,
}
font2 = {
'family': 'Times New Roman',
'color': 'red',
'weight': 'normal',
'size': 16,
}
font3 = {
'family': 'serif',
'color': 'blue',
'weight': 'bold',
'size': 14,
}
font4 = {
'family': 'Calibri',
'color': 'navy',
'weight': 'normal',
'size': 17,
}
#-----------四种不同字体显示风格-----
#-------建立函数----------
x = np.linspace(0.0, 5.0, 100)
y = np.cos(2*np.pi*x) * np.exp(-x/3)
#-------绘制图像,添加标注----------
plt.plot(x, y, '--')
plt.title('震荡曲线', fontdict=font1)
#------添加文本在指定的坐标处------------
plt.text(2, 0.65, r'$\cos(2 \pi x) \exp(-x/3)$', fontdict=