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’,其可選項如下:
在這裏插入圖片描述
②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’ ])

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章