annotate()函数--matplotlib

1. 函数功能

对点(x,y)添加带箭头的注释文本。

2. 函数语法

Axes.annotate(text, xy, xytext=None, xycoords='data', textcoords=None, 
arrowprops=None, annotation_clip=None

3. 函数参数与示例

3.1 标注点与标注文本

参数含义
text注释文本
xy(float,float),浮点数组成的元组,被注释点的坐标
xytext(float,float),浮点数组成的元组,放置注释文本的坐标
xycoords被注释点的坐标系属性 ,不太理解
textcoords注释文本的坐标系属性 ,不太理解

3.2 箭头属性

3.2.1 箭头类型

参数含义
arrowstyle箭头类型,常见类型如下

在这里插入图片描述

在这里插入图片描述

3.2.2 连接类型

参数含义
connectionstyle连接类型,常见类型如下:

在这里插入图片描述
在这里插入图片描述

3.2.3 示例代码

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x)

plt.plot(x, y, c='m')

plt.xlim(0, 2 * np.pi)
plt.ylim(-1, 1)

plt.annotate('max', xy=(np.pi / 2, 1), xytext=(np.pi, 0.8),
             arrowprops=dict(arrowstyle='-|>',
                             connectionstyle='angle3',color='red'))

plt.grid()
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 500)
y = np.sin(x)

plt.plot(x, y, c='m')

plt.xlim(0, 2 * np.pi)
plt.ylim(-1, 1)

plt.annotate('max', xy=(np.pi / 2, 1), xytext=(np.pi, 0.8),
             arrowprops=dict(arrowstyle='fancy',
                             connectionstyle='arc3',color='red'))

plt.grid()
plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']  # 配置显示中文,字体为楷体
mpl.rcParams['axes.unicode_minus'] = False  # 配置正常显示负号: '-'

plt.text(0.5, 2.0, '101亿元\n  0.8%',
         bbox=dict(boxstyle='Circle', facecolor='snow', alpha=0.6),
         fontsize=10, color='m')

plt.annotate('', xy=(3, 5), xytext=(1.2, 2.3),
             arrowprops=dict(arrowstyle='-|>,head_length=1.0,head_width=0.6',
                             connectionstyle='angle,angleA=-30,angleB=60,rad=80',
                             color='red'))

plt.text(3, 5, '300亿元\n约2.0%',
         bbox=dict(boxstyle='Round', facecolor='snow', alpha=0.8),
         fontsize=12, color='m')

plt.annotate('', xy=(5.8, 8), xytext=(3.7, 5.5),
             arrowprops=dict(arrowstyle='simple,head_length=1.2,head_width=1,tail_width=0.2',
                             connectionstyle=('arc3, rad=0.3'), color='red'))
plt.text(6, 7.6, '超千亿元\n约5.0%',
         bbox=dict(boxstyle='Sawtooth', facecolor='snow'),
         fontsize=14, color='m')
plt.xlim(0, 8.5)
plt.ylim(0, 12)

plt.show()


在这里插入图片描述

3.3 注释字体属性

参数含义
fontsize注释文本字体大小
color注释文本字体颜色
alpha注释文本字体透明度
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']  # 配置显示中文,字体为楷体
mpl.rcParams['axes.unicode_minus'] = False  # 配置正常显示负
plt.annotate('300亿元\n约2.0%', xy=(6, 8), xytext=(3, 5),
             fontsize=14, color='b', alpha=0.5,
             bbox=dict(boxstyle='Sawtooth', facecolor='snow'),
             arrowprops=dict(arrowstyle='<|-,head_length=1.0,head_width=0.6',
                             connectionstyle='angle,angleA=-30,angleB=60,rad=10',
                                                          color='red'))


plt.xlim(0, 8.5)
plt.ylim(0, 12)

plt.show()

在这里插入图片描述

3.4 注释文本框设置

annotate()函数中的注释文本框设置同text()函数中的注释文本框设置,使用bbox参数设置文本框属性,参见text函数的bbox部分text函数

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']  # 配置显示中文,字体为楷体
mpl.rcParams['axes.unicode_minus'] = False  # 配置正常显示负号: '-'

plt.annotate('101亿元\n  0.8%', xy=(2.8, 4.8), xytext=(1.2, 2.3),
             bbox=dict(boxstyle='Circle', facecolor='y', alpha=0.2),
             arrowprops=dict(arrowstyle='-|>,head_length=1.0,head_width=0.6',
                             connectionstyle='angle,angleA=-30,angleB=60,rad=80',
                             color='red'))

plt.annotate('300亿元\n约2.0%', xy=(6, 8), xytext=(3, 5),
            fontsize=14, color='m', bbox=dict(boxstyle='Sawtooth', facecolor='snow'),
             arrowprops=dict(arrowstyle='simple,head_length=1.2,head_width=1,tail_width=0.2',
                             connectionstyle=('arc3, rad=0.3'), color='red'))



plt.text(6.2, 7.6, '超千亿元\n 约5%',
         bbox=dict(boxstyle='Circle', facecolor='r', alpha=0.3),
         fontsize=16, color='k')

plt.xlim(0, 8.5)
plt.ylim(0, 12)

plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值