分享

Matplotlib库 标注点函数annotate()

 印度阿三17 2020-05-27

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
    ============ =============================================
bbox参数可以在文本周围增加外框,常用参数如下:
  • 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()

 

来源:https://www./content-4-701051.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多