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,就不应该有其他的属性
plt.show()

猜你喜欢

转载自blog.csdn.net/xiao_pingping/article/details/82082364