1.注释
代码如下
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10,11,1)
y = x*x
plt.plot(x,y)
# 添加注释 xy 箭头坐标,xytext 注释文字坐标,arrowprops:frac 箭头占比例,headwidth 箭头宽度,width 箭身宽度
plt.annotate("thi is the bottom",xy=(0,1),xytext=(0,20),arrowprops=dict(facecolor='r',frac=0.2,headwidth=30,width=10))
运行结果
2.文字(只画纯文字,不画箭头)
代码如下
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-10,11,1)
y = x*x
plt.plot(x,y)
# family 设置字体,size 设置文字大小,style设置是否斜体,weight设置粗细
plt.text(-2,40,"fucntion:y=x*x",family="serif",size=12,color='r',style='italic',weight='black')
plt.text(-2,20,"fucntion:y=x*x",family="fantacy",size=12,color='g',style='oblique',weight='light')
运行结果
3.txt公式
matplotlib自带mathtext引擎,不需安装Txt系统
代码如下
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([1,7])
ax.set_ylim([1,5])
# 也可以用plt.text()
ax.text(2,4,r"$ \alpha_i \beta_j \pi \lambda \omega $",size=16)
ax.text(4,4,r"$ \sin(0)=\cos(\frac{\pi}{2}) $",size=16)
ax.text(2,2,r"$ \lim_{x \rightarrow y} \frac{1}{x^3} $",size=16)
ax.text(4,2,r"$ \sqrt[4]{x}=\sqrt{y} $",size=16)
运行结果