matplot--annotate()函数

函数功能:添加图形内容细节的指向型注释文本。

s:str, 注释信息内容

xy:(float,float), 箭头点所在的坐标位置

xytext:(float,float), 注释内容的坐标位置

weight: str or int, 设置字体线型,其中字符串从小到大可选项有{‘ultralight’, ‘light’, ‘normal’, ‘regular’, ‘book’, ‘medium’, ‘roman’, ‘semibold’, ‘demibold’, ‘demi’, ‘bold’, ‘heavy’, ‘extra bold’, ‘black’}

color: str or tuple, 设置字体颜色 ,单个字符候选项{‘b’, ‘g’, ‘r’, ‘c’, ‘m’, ‘y’, ‘k’, ‘w’},也可以’black’,'red’等,tuple时用[0,1]之间的浮点型数据,RGB或者RGBA, 如: (0.1, 0.2, 0.5)、(0.1, 0.2, 0.5, 0.3)等

arrowprops #箭头参数,参数类型为字典dict。参数如下:

width:the width of the arrow in points 箭头的宽度(以点为单位)
headwidth: the width of the base of the arrow head in points 箭头底部以点为单位的宽度 。
headlength: the length of the arrow head in points 箭头的长度(以点为单位) 。
shrink:fraction of total length to ‘shrink’ from both ends 总长度的一部分,从两端“收缩”。
facecolor:箭头颜色

bbox:dict,为注释文本添加边框,其key有①boxstyle,其格式类似’round,pad=0.5’,其可选项如下:
matplot--annotate()函数_第1张图片
②facecolor(可简写为fc)设置背景颜色
③ edgecolor(可简写为ec)设置边框线条颜色
④lineweight(可简写为lw)设置边框线型粗细
⑤alpha设置透明度,[0,1]之间的小数,0代表完全透明,即类似③颜色设置无效。
具体看下面例子

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.figure(figsize=(12, 9))
plt.plot(x, y, linestyle='-', color='blue')

 
plt.annotate(s='local min', 
             xy=(3*np.pi/2, -1.0),
             xytext=(3*np.pi/2, -0.5), 
             weight='bold', 
             color='aqua',
             arrowprops=dict(fc='black', shrink=0.05),
             bbox=dict(boxstyle='round, pad=0.2', fc='yellow', ec='k',lw=1 ,alpha=0.2),
             ha='center',
             fontsize=12) #ha='center'表示local min这串文字处于箭头中心(horizontalalignment:水平对齐方式 ,参数:[ ‘center’ | ‘right’ | ‘left’ ])

matplot--annotate()函数_第2张图片

你可能感兴趣的:(Python,matplot,matplot)