我们相信:世界是美好的,你是我也是。平行空间的世界里面,不同版本的生活也在继续...

python里面用于画图的库叫做pyplot,在上一篇文章里面,苏南大叔用plot画了基于两个维度的鸢尾花散点图。那么,本文中将对plotannotate文字注释功能做初步的探讨。

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - plot文字注释
python画图,matplotlib如何添加annotate文字注释?(图7-1)

大家好,这里是苏南大叔的平行空间笔记本博客,记录苏南大叔的编程经验文章。本文测试环境:win10python@3.11.0pandas@1.5.3numpy@1.24.2matplotlib@3.7.1。本文主要讲plot的文字注释功能。本文并不追求尽善尽美,仅仅是做个相关概念的索引,毕竟用到这么多参数的概率也不大,等着苏南大叔用到哪里再详细展开。

最简单的例子(足够日常使用了)

这里先用一个最简单的例子,来引出本文的讨论内容annotate。代码如下:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 3)
y = x * x + 0.5
plt.plot(x, y, marker='o')
for xy in zip(x, y):
    plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points')
plt.show()

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - 文本注释
python画图,matplotlib如何添加annotate文字注释?(图7-2)

这里没有批量添加文字注释的方法,只能在循环里面,一个一个添加的。

函数原型总述

这个plot.annotate()函数定义如下:

pyplot.annatate(s, xy, xytext=None, xycoords='data',textcoords='data', arrowprops=None, annotation_clip=None, **kwargs)

它有很多参数,列表如下:

参数名称解释
s注释文本内容
xy被注释的坐标点
xytext注释文字的坐标位置
xycoords注释文本位置
weight设置字体线型
color设置字体颜色
arrowprops箭头参数
bbox增加外框

参数xycoords注释文本相对位置

参数解释
figure points相对于图像左下角的点数(points)
figure pixels相对于图像左下角的像素数(pixels)
figure fraction相对于图像左下角的比例,(0, 0) 为图像左下角; (1, 1) 为右上角
subfigure points相对于子图左下角的点数(points)
subfigure pixels相对于子图左下角的像素数(pixels)
subfigure fraction相对于子图左下角的比例
axes points相对图坐标轴的点数(points)
axes pixels相对图坐标轴的像素数(pixels)
axes fraction相对图坐标轴的比例
data默认值,使用被注解对象的坐标系
polar极坐标系
plt.annotate("(%s,%s)offset points" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points')
plt.annotate("(%s,%s)figure points" % xy, xy=xy, xytext=(-15, 10), textcoords='figure points')

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - 坐标相对值
python画图,matplotlib如何添加annotate文字注释?(图7-3)

可以看到,范例中textcoords的前一部分是坐标的相对值,后面的值是坐标尺度单位。

参数weight字体粗细

可以取值如下的值:
'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'

plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points', weight='heavy')

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - 字体对比
python画图,matplotlib如何添加annotate文字注释?(图7-4)

参数color字体颜色

可以是以下的值:

  • {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}
  • 'black','red',green
  • [0,1]之间的浮点型数据
  • RGB或者RGBA, 如: (0.1, 0.2, 0.5)、(0.1, 0.2, 0.5, 0.3)

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - 颜色设置
python画图,matplotlib如何添加annotate文字注释?(图7-5)

plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 20), textcoords='offset points', color='red')
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points', color='g')
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 0), textcoords='offset points', color='#ff8401')
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, -10), textcoords='offset points', color="0.66")
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, -20), textcoords='offset points', color=(0.52,0.52,0.52,1))

参数arrowprops箭头设置

参数释义
width箭头的宽度(以点为单位)
headwidth箭头底部以点为单位的宽度
headlength箭头的长度(以点为单位)
shrink总长度的一部分,从两端“收缩”
facecolor箭头颜色,简写fc
plt.annotate('string', xy=(1.75, 4), xytext=(1.5, 3.6), arrowprops=dict(fc='black', shrink=2))
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points', arrowprops = dict(facecolor = "r", headlength = 25, headwidth = 30, width = 20))

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - 箭头dict
python画图,matplotlib如何添加annotate文字注释?(图7-6)

注意这个arrowprops属性右侧值类型为dict()类型。

参数bbox外框

参数简写释义
boxstyle方框外形
facecolorfc背景颜色
edgecolorec边框线条颜色
edgewidth边框线条大小
plt.annotate("(%s,%s)" % xy, xy=xy, xytext=(-15, 10), textcoords='offset points', bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k', lw=1, alpha=0.5))

苏南大叔:python画图,matplotlib如何添加annotate文字注释? - bbox盒子
python画图,matplotlib如何添加annotate文字注释?(图7-7)

相关链接

结束语

matplotlib画图系列,内容很多很复杂。由于大部分的内容,对于苏南大叔来说,都是用不到的。所以,关于matplotlib画图的经验文章,就只是用到哪里就写到哪里了。

如果本文对您有帮助,或者节约了您的时间,欢迎打赏瓶饮料,建立下友谊关系。
本博客不欢迎:各种镜像采集行为。请尊重原创文章内容,转载请保留作者链接。

 【福利】 腾讯云最新爆款活动!1核2G云服务器首年50元!

 【源码】本文代码片段及相关软件,请点此获取更多信息

 【绝密】秘籍文章入口,仅传授于有缘之人   python    plot