text()
若要给画布对象或者Figure对象添加注释,常用的为text()方法,分别可以用Figure对象.text()或者画布对象.text()添加文字注释
x, y, s, fontdict=None
上述为text()函数的参数x,y为注释所在的坐标,s为要添加的注释,fontdict为字体样式字典,键包括常见的样式,如‘color’,‘fontsize’
axes.text()
若要给画布对象添加注释,使用的x,y为画布中的绝对位置,也就是按刻度计算
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
y1 = np.cos(x)
figure1 = plt.figure(figsize=(6, 4))
axes1 = plt.subplot(1, 2, 1)
axes1.plot(x, y)
axes1.text(x=1,y=0,s='this is a sin curve',fontdict={'fontsize':15,'color':'red'})
axes2 = plt.subplot(1, 2, 2)
figure1.savefig('plot1.png')
plt.show()
Figure.text()
由于Figure.text()作用在整个Figure对象上,所以x,y对应的为整个Figure的相对位置,也就是x和y的范围为[0,1]
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
y1 = np.cos(x)
figure1 = plt.figure(figsize=(6, 4))
axes1 = plt.subplot(1, 2, 1)
axes1.plot(x, y)
axes1.text(x=1,y=0,s='this is a sin curve',fontdict={'fontsize':15,'color':'red'})
axes2 = plt.subplot(1, 2, 2)
figure1.text(x=0.5,y=0.5,s='this is my figure',fontdict={'fontsize':20,'color':'blue'})
figure1.savefig('plot1.png')
plt.show()
suptitle()
suptitle()也就是supertitle,与之前通过plt.title()或者axex.title()不同,这里的title是Figure对象的title
def suptitle(self, t, **kwargs): # docstring from _suplabels... info = {'name': '_suptitle', 'x0': 0.5, 'y0': 0.98, 'ha': 'center', 'va': 'top', 'rotation': 0, 'size': 'figure.titlesize', 'weight': 'figure.titleweight'} return self._suplabels(t, info, **kwargs)
这里常用的就是t(title内容),x和y分别为Figure对象中的相对位置,范围[0,1],依然可以配置fontdict,但是需要注意,fontsize参数不可以在fontdict中配置,要单独设置,否则会报错
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
y1 = np.cos(x)
figure1 = plt.figure(figsize=(6, 4))
axes1 = plt.subplot(1, 2, 1)
axes1.plot(x, y)
axes1.text(x=1,y=0,s='this is a sin curve',fontdict={'fontsize':15,'color':'red'})
axes2 = plt.subplot(1, 2, 2)
figure1.text(x=0.5,y=0.5,s='this is my figure',fontdict={'fontsize':20,'color':'blue'})
figure1.suptitle(t='my title',x = 0.5,y=1,fontsize = 20,fontdict={'color':'y'})
figure1.savefig('plot1.png')
plt.show()
annotate()
annotate()的作用是给出带箭头指向的注释参数包括xy(进行注释的点的坐标),xytext(注释文字的坐标),text(注释的内容),arrowprops(箭头属性设置)主要包括以下四种属性和arrowstyle用于直接设置配置好的箭头属性,但是配置了arrowstyle就不能选用下面的自定义属性配置了
- width参数设置箭头长方形部分的宽度
- headlength参数设置箭头尖端的长度,
- headwidth参数设置箭头尖端底部的宽度
- shrink参数设置箭头顶点、尾部与指示点、注释文字的距离(比例值),可以理解为控制箭头的长度
arrowstyle
``'->'`` 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
``'<|-'`` 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
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
y1 = np.cos(x)
figure1 = plt.figure(figsize=(6, 4))
axes1 = plt.subplot(1, 1, 1)
axes1.plot(x, y)
axes1.annotate(xy=(np.pi/2,1),xytext=(np.pi/2,0.2),text='max',arrowprops={'arrowstyle':'simple'})
figure1.savefig('plot1.png')
plt.show()