matplotlib用于标注文字的函数_python annotate

matplotlib用于标注文字的函数_python annotateMatplotlib库标注在工作有很大作用:Annotate的构造函数为:Annotation(s,xy,xytext=None,xycoords=‘data’,textcoords=None,arrowprops=None,annotation_clip=None,**kwargs)用于绘制连接图上两点的箭头。关键参数:s为注释文本内容xy为被注释的坐标点,二维元组形如(…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

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为参考)

Value Description

================= =============================================

‘figure points’ Points from the lower left of the figure

‘figure pixels’ Pixels from the lower left of the figure

‘figure fraction’ Fraction of figure from lower left

‘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

================= =============================================

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

“’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

============ =============================================

两个点之间的连接路径。 这由connectionstyle键值控制。

Angle(angleA=90, angleB=0, rad=0.0)[source]

Angle3(angleA=90, angleB=0)

Arc(angleA=0, angleB=0, armA=None, armB=None, rad=0.0)

Arc3(rad=0.0)[source]

Bar(armA=0.0, armB=0.0, fraction=0.3, angle=None)[source]

在pyplot模块(或Axes类的text方法get_bbox_patch/set_boxstyle(“rarrow”, pad=0.6))中的text()函数接受bbox关键字参数,并且在提供时,在文本周围绘制一个框。常用参数如下:

matplotlib用于标注文字的函数_python annotate

matplotlib用于标注文字的函数_python annotate

importmatplotlib.pyplot as pltimportnumpy as npimportmatplotlib.path as mpathimportmatplotlib.lines as mlinesimportmatplotlib.patches as mpatchesfrom matplotlib.collections importPatchCollectiondeflabel(xy, text):

y= xy[1] – 0.15 #shift y-value for label so that it’s below the artist

plt.text(xy[0], y, text, ha=”center”, family=’sans-serif’, size=14)

fig, ax=plt.subplots()#create 3×3 grid to plot the artists

grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T

patches=[]#add a circle

circle = mpatches.Circle(grid[0], 0.1, ec=”none”)

patches.append(circle)

label(grid[0],”Circle”)#add a rectangle

rect = mpatches.Rectangle(grid[1] – [0.025, 0.05], 0.05, 0.1, ec=”none”)

patches.append(rect)

label(grid[1], “Rectangle”)#add a wedge

wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec=”none”)

patches.append(wedge)

label(grid[2], “Wedge”)#add a Polygon

polygon = mpatches.RegularPolygon(grid[3], 5, 0.1)

patches.append(polygon)

label(grid[3], “Polygon”)#add an ellipse

ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)

patches.append(ellipse)

label(grid[4], “Ellipse”)#add an arrow

arrow = mpatches.Arrow(grid[5, 0] – 0.05, grid[5, 1] – 0.05, 0.1, 0.1,

width=0.1)

patches.append(arrow)

label(grid[5], “Arrow”)#add a path patch

Path =mpath.Path

path_data=[

(Path.MOVETO, [0.018, -0.11]),

(Path.CURVE4, [-0.031, -0.051]),

(Path.CURVE4, [-0.115, 0.073]),

(Path.CURVE4, [-0.03, 0.073]),

(Path.LINETO, [-0.011, 0.039]),

(Path.CURVE4, [0.043, 0.121]),

(Path.CURVE4, [0.075, -0.005]),

(Path.CURVE4, [0.035, -0.027]),

(Path.CLOSEPOLY, [0.018, -0.11])]

codes, verts= zip(*path_data)

path= mpath.Path(verts + grid[6], codes)

patch=mpatches.PathPatch(path)

patches.append(patch)

label(grid[6], “PathPatch”)#add a fancy box

fancybox =mpatches.FancyBboxPatch(

grid[7] – [0.025, 0.05], 0.05, 0.1,

boxstyle=mpatches.BoxStyle(“Round”, pad=0.02))

patches.append(fancybox)

label(grid[7], “FancyBboxPatch”)#add a line

x, y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]])

line= mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)

label(grid[8], “Line2D”)

colors= np.linspace(0, 1, len(patches))

collection= PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)

collection.set_array(np.array(colors))

ax.add_collection(collection)

ax.add_line(line)

plt.axis(‘equal’)

plt.axis(‘off’)

plt.tight_layout()

plt.show()

View Code

boxstyle方框外形,参数是框样式的名称与其作为关键字参数的属性

facecolor(简写fc)背景颜色

edgecolor(简写ec)边框线条颜色

edgewidth边框线条大小

box类        名称            属性

Circle         circle         pad=0.3

DArrow         darrow         pad=0.3

LArrow         larrow         pad=0.3

RArrow         rarrow         pad=0.3

Round         round         pad=0.3,rounding_size=None

Round4         round4         pad=0.3,rounding_size=None

Roundtooth     roundtooth     pad=0.3,tooth_size=None

Sawtooth     sawtooth     pad=0.3,tooth_size=None

Square         square         pad=0.3

案例

importnumpy as npimportmatplotlib.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= 100ax.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()

matplotlib用于标注文字的函数_python annotate

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234355.html原文链接:https://javaforall.net

(0)
上一篇 2025年6月6日 下午4:43
下一篇 2025年6月6日 下午5:15


相关推荐

  • winhttp 发送 get 请求「建议收藏」

    winhttp 发送 get 请求「建议收藏」由于微端要和服务器交互,而服务器又只有http协议的处理,所以需要用C++来模拟get或post请求。这是使用windowsapi来模拟get请求的,使用到的库有“winhttp”,头文件有“winhttp.h”,下面的代码来源于http://msdn.microsoft.com:12345678

    2022年7月27日
    10
  • 微型计算机对应的英文名,跟中文名匹配的英文名

    微型计算机对应的英文名,跟中文名匹配的英文名相信大多数人们在查找英文名字之时 都会不知所措 不知道到底怎样的英文名字才合适自己 并得到一定的认知与了解 但其实在英文名字中 也会有着与中文汉字有着相同发音的单词 跟中文名匹配的英文名而这些也是大家可以运用且借鉴的地方 从而得到即与自身贴切 又让他人有着一定的记忆点的英文名字 如 大家可以借助于英文名跟中文名发音一致来查找匹配的英文名字 或可以根据英文名与中文名局部发音一致 以及借助中文名直接音

    2026年3月17日
    2
  • pycharm安装教程,超详细

    pycharm安装教程,超详细在安装 pycharm 之前 确保你的电脑配置好了 python 没有下载安装的可以去看我的文章 gt gt gt python 安装详细教程 pycharmwindo 版本下载地址 https www jetbrains com pycharm download section windows 下载社区版本 日常学习使用够用了 专业版是收费的哦 功能更强大 下载完后打开安装程序 点击 next 选择一个路径然后 next 把这些选项都勾选上 点击 Install 安装完后点击 Finish

    2026年3月27日
    2
  • Linq语法详细

    Linq语法详细每一种搜索结果集 我都以三种方式变现出来 为了更好的理解 1 简单的 linq 语法 1varss fromrindb Am recProScheme 2varss1 db Am recProScheme 3stringsssql select fromAm recProScheme

    2026年3月16日
    2
  • 免备案cdn_cdn贝安装

    免备案cdn_cdn贝安装免备案CDN,符码CDN应该是使用CDN贝搭建,该CDN系统相对来说比较成熟,功能支持比较丰富。今天对符码免备案CDN进行简单的测试,仅供大家参考。首先,就是简单粗暴的多地区PING大法,简单看下CDN延迟效果:如果要是对比那么多免备案CDN延迟来说,符码CDN平均可以做到45ms的延迟,无疑是测试的所有免备案CDN中多地区PING平均延迟最小的一个,甚至一些国内CDN也达不到这个低延迟效果,延迟…

    2025年8月30日
    8
  • java面试题笔试常见选择题大全含答案

    java面试题笔试常见选择题大全含答案1、9道常见的java笔试选择题2、java多线程面试题选择题大全含答案3、java异常处理面试题常见选择题含答案4、java笔试常见的选择题(坑你没商量)5、java笔试题大全之IO流常见选择题6、java集合类笔试选择题整理含答案7、常见的javaweb笔试选择题含答案8、常见的SQL笔试选择题含答…

    2022年4月12日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号