Matplotlib库 标注点函数annotate()
作者:互联网
Matplotlib库 标注在工作有很大作用:
Annotate的构造函数为 :Annotation(s, xy, xytext=None, xycoords=‘data’, textcoords=None, arrowprops=None, annotation_clip=None, **kwargs)
关键参数:
- s 为注释文本内容
- xy 为被注释的坐标点,二维元组形如(x,y)
- xytext 为注释文本的坐标点,也是二维元组,默认与xy相同
- xycoords为被注释点的坐标系属性(通常xycoords值为’data’,即以被注释的坐标点xy为参考)
- textcoords 设置注释文本的坐标系属性(textcoords 选择为相对于被注释点xy的偏移量,‘offset points’或者’offset pixels’)
arrowprops为箭头的样式,dict(字典)型数据,如果该属性非空,则会在注释文本和被注释点之间画一个箭头。如果不设置’arrowstyle’关键字,则允许包含关键字width、headwidth、headlength、shrink,以下是arrowstyle的可选值
-
Name Attrs
============ =============================================
``'-'`` None
``'->'`` head_length=0.4,head_width=0.2
``'-['`` widthB=1.0,lengthB=0.2,angleB=None
``'|-|'`` widthA=1.0,widthB=1.0
``'-|>'`` head_length=0.4,head_width=0.2
``'<-'`` head_length=0.4,head_width=0.2
``'<->'`` head_length=0.4,head_width=0.2
``'<|-'`` head_length=0.4,head_width=0.2
``'<|-|>'`` head_length=0.4,head_width=0.2
``'fancy'`` head_length=0.4,head_width=0.4,tail_width=0.4
``'simple'`` head_length=0.5,head_width=0.5,tail_width=0.2
``'wedge'`` tail_width=0.3,shrink_factor=0.5
============ =============================================
- boxstyle方框外形,参数是框样式的名称与其作为关键字参数的属性
- facecolor(简写fc)背景颜色
- edgecolor(简写ec)边框线条颜色
- edgewidth边框线条大小
案例
import numpy as np import matplotlib.pylab as plt x = np.arange(0, 10, 0.005) y = np.exp(-x / 2.) * np.sin(2 * np.pi * x) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y) ax.set_xlim(0, 10) ax.set_ylim(-1, 1) xdata, ydata = 5, 0 xdisplay, ydisplay = ax.transData.transform_point((xdata, ydata)) bbox = dict(boxstyle="round", fc="0.8") arrowprops = dict(arrowstyle="<|-|>",connectionstyle="angle,angleA=0,angleB=90,rad=10") offset = 100 ax.annotate('data = (%.1f, %.1f)' % (xdata, ydata), xy=(xdata, ydata), xytext=(1 * offset, offset), textcoords='offset pixels', bbox=bbox, arrowprops=arrowprops) disp = ax.annotate('display = (%.1f, %.1f)' % (xdisplay, ydisplay), (xdisplay, ydisplay), xytext=(0.5 * offset, -offset), xycoords='figure pixels', textcoords='offset points', bbox=bbox, arrowprops=arrowprops) plt.show()
标签:head,width,0.4,Matplotlib,annotate,点函数,offset,ax,arrowprops 来源: https://www.cnblogs.com/tingxin/p/12977215.html