Matplotlib图形内的文字注释、箭头(三)

import numpy as np
import matplotlib.pyplot as plt


''''
---text():在Axes对象的任意位置添加文字
---xlabel():为X轴添加标签
---ylabel():为Y轴添加标签
---title():为Axes对象添加标题
---legend():为Axes对象添加图例
---figtext():在Figure对象的任意位置添加文字
---suptitle():为Figgure对象添加中心化的标题
---annotate():为Axes对象添加注释(箭头可选)
所有的方法会返回一个matplotlib.text.Text对象
'''

# 图形内的文字text()
x = np.arange(0, 2*np.pi, 0.01)
plt.plot(np.sin(x))
plt.text(0, 0, "sin(0)=0")    # x,y代表坐标系中的数值
plt.show()

# 使用figtext()
x = np.arange(0, 2*np.pi, 0.01)
plt.plot(np.sin(x))
plt.figtext(0.5, 0.5, "sin(0)=0")  # 使用figtext时,x,y代表相对值,表示图片的宽高
plt.show()


'''注释
---annotate()参数设置箭头指示的位置,xytext参数设置注释文字的位置
---arrowprops参数以字典的形式设置箭头的样式
---width参数设置箭头长方形部分的宽度,headlength参数设置箭头尖端的长度
---headwidth参数设置箭头尖端底部的宽度,shrink参数设置箭头顶点、尾部与指示点、注释文字的距离(比例值)'''
plt.figure(figsize=(6, 6))
x = np.random.randint(0, 10, size=10)
x[5] = 30    # 对x中索引值为5的重新赋值
plt.plot(x)
plt.ylim([-2, 35])
# plt.annotate(s="this point is important", xy=(5, 30), xytext=(6, 31),arrowprops={"width": 2, "headlength": 5, "headwidth": #5, "shrink": 0.1})
plt.annotate(s="this point is important", xy=(5, 30), xytext=(6, 31),arrowprops={"arrowstyle":"->"})     # 如果arrowprops中有arrowstyle,就不应该有其他的属性,xy代表的是箭头的位置,xytext代表的是箭头文本的位置。
plt.show()
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值