matplotlib的annotate用法小结
这个是matplotlib自定义的annotate方法的文本:
一、
1 def annotate(self, s, xy, *args, **kwargs): 2 a = mtext.Annotation(s, xy, *args, **kwargs) 3 a.set_transform(mtransforms.IdentityTransform()) 4 if 'clip_on' in kwargs: 5 a.set_clip_path(self.patch) 6 self._add_text(a) 7 return a
这个方法里面定义了annotate方法所使用的变量,其中*args是tuple,**kwargs是dict
二、
def __init__(self, s, xy, xytext=None, xycoords='data', textcoords=None, arrowprops=None, annotation_clip=None, **kwargs): """ Annotate the point *xy* with text *s*. 在点xy:(x,y)处注释文本 s In the simplest form, the text is placed at *xy*. 在最简单的形式中,文本是处在xy的坐标点处 Optionally, the text can be displayed in another position *xytext*.实际应用中,文本用xytext属性来随意定义文本所在的位置 An arrow pointing from the text to the annotated point *xy* can then be added by defining *arrowprops*. 可以增加从文本到注释点xy的箭头指向,箭头样式通过arrowprops属性定义 Parameters ---------- s : str The text of the annotation. s:str类型,注释的文本 xy : (float, float) The point *(x,y)* to annotate. xy: (float, float)坐标点,所要注释所在的坐标点 xytext : (float, float), optional The position *(x,y)* to place the text at. If *None*, defaults to *xy*. xytext:(float, float)坐标点,可选,注释文本所在的坐标点,默认是上面xy的坐标点 xycoords : str, `.Artist`, `.Transform`, callable or tuple, optional xycoords :str类型,Artist和Transform的matplotlib基类,元组,可选 The coordinate system that *xy* is given in. The following types of values are supported: 给定的xy坐标系统是由属性xycoords来提供支持,该属性以下值 - One of the following strings: ================= ============================================= Value Description ================= ============================================= 'figure points' Points from the lower left of the figure 距离图层左下角的点数量,比如xy=(10,10)说明距离图层左下方各10个点的位置 'figure pixels' Pixels from the lower left of the figure 距离图层左下角的像素,比如xy=(10,10)说明距离图层左下方各10个像素的位置 'figure fraction' Fraction of figure from lower left 距离图层左下角的数字分数,也就是百分比。左下角是(0,0),右上角是(1,1)。如果xy=(0.5,0.6),也就是距离左下角横坐标50%,纵坐标60% 'axes points' Points from lower left corner of axes 距离轴坐标左下角的点数量 'axes pixels' Pixels from lower left corner of axes 距离轴坐标左下角的像素 'axes fraction' Fraction of axes from lower left 距离轴坐标左下角的数字分数 'data' Use the coordinate system of the object being annotated (default) 使用实际的轴坐标的数据(默认) 'polar' *(theta,r)* if not native 'data' coordinates 使用极坐标系来处理,(弧度,长度) ================= ============================================= - An `.Artist`: *xy* is interpreted as a fraction of the artists `~matplotlib.transforms.Bbox`. E.g. *(0, 0)* would be the lower left corner of the bounding box and *(0.5, 1)* would be the center top of the bounding box. - A `.Transform` to transform *xy* to screen coordinates. - A function with one of the following signatures:: def transform(renderer) -> Bbox def transform(renderer) -> Transform where *renderer* is a `.RendererBase` subclass. The result of the function is interpreted like the `.Artist` and `.Transform` cases above. - A tuple *(xcoords, ycoords)* specifying separate coordinate systems for *x* and *y*. *xcoords* and *ycoords* must each be of one of the above described types. See :ref:`plotting-guide-annotation` for more details. Defaults to 'data'. textcoords : str, `.Artist`, `.Transform`, callable or tuple, optional The coordinate system that *xytext* is given in. textcoords :str类型,Artist和Transform的matplotlib基类,元组,可选,其值是与'xycoords'一致,并且也可以使用下面两个值《主要定义偏移量的》,如果没有说明,则与'xycoords'使用的坐标系统一致 All *xycoords* values are valid as well as the following strings: ================= ========================================= Value Description ================= ========================================= 'offset points' Offset (in points) from the *xy* value 'offset pixels' Offset (in pixels) from the *xy* value ================= ========================================= Defaults to the value of *xycoords*, i.e. use the same coordinate system for annotation point and text position. arrowprops : dict, optional The properties used to draw a `~matplotlib.patches.FancyArrowPatch` arrow between the positions *xy* and *xytext*. arrowprops : 字典类,可选。这个属性主要是用来画出xytext的文本坐标点到xy注释点坐标点的箭头指向线段 If *arrowprops* does not contain the key 'arrowstyle' the allowed keys are: 如果arrowprops 字典类中不包含arrowstyle的key,则可以使用下列的KEY ========== ====================================================== Key Description ========== ====================================================== 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 缩小:移动提示,并使其离注释点和文本一些距离 ? Any key to :class:`matplotlib.patches.FancyArrowPatch` 其他的matplotlib.patches.FancyArrowPatch基类中属性 ========== ====================================================== If *arrowprops* contains the key 'arrowstyle' the above keys are forbidden. The allowed values of ``'arrowstyle'`` are: 如果arrowprops 字典类中包含arrowstyle的key,则可以使用下列的赋值 ============ ============================================= 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 ============ ============================================= Valid keys for `~matplotlib.patches.FancyArrowPatch` are: =============== ================================================== Key Description =============== ================================================== arrowstyle the arrow style 箭头的样式 connectionstyle the connection style 连接线的样式 relpos default is (0.5, 0.5) 箭头起始点相对注释文本的位置,默认为 (0.5, 0.5),即文本的中心, (0,0)表示左下角,(1,1)表示右上角 patchA default is bounding box of the text 箭头起点处的图形(matplotlib.patches对象),默认是注释文字框 patchB default is None 箭头终点处的图形(matplotlib.patches对象),默认为空 shrinkA default is 2 points 箭头起点的缩进点数,默认为2 shrinkB default is 2 points 箭头终点的缩进点数,默认为2 mutation_scale default is text size (in points) mutation_aspect default is 1. ? any key for :class:`matplotlib.patches.PathPatch` 其他matplotlib.patches.PathPatch的属性 =============== ================================================== Defaults to None, i.e. no arrow is drawn. 默认是NONE,没有任何箭头绘制 annotation_clip : bool or None, optional annotation_clip : 布尔值或者NONE,可选 Whether to draw the annotation when the annotation point *xy* is 当注释点xy在子图区域外面是否要绘制注释 outside the axes area. - If *True*, the annotation will only be drawn when *xy* is 设为True时,只有被注释点在子图区内时才绘制注释; within the axes. - If *False*, the annotation will always be drawn. 设为False时,无论被注释点在哪里都绘制注释。 - If *None*, the annotation will only be drawn when *xy* is NONE:仅当xycoords为‘data’时,相当于True。 within the axes and *xycoords* is 'data'. Defaults to *None*. **kwargs Additional kwargs are passed to `~matplotlib.text.Text`. Returns ------- annotation : `.Annotation` See Also -------- :ref:`plotting-guide-annotation`. """ _AnnotationBase.__init__(self, xy, xycoords=xycoords, annotation_clip=annotation_clip) # warn about wonky input data