学习内容
matplotlib的注释操作
重点
1.注释plt.annotate
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(-10, 10, 0.1)
b = a ** 2
plt.plot(a, b)
plt.annotate("this is bottom", xy=(0, 1), xytext=(-1.87, 20),
arrowprops=dict(facecolor='b',
headwidth=15, width=10))
#标题的文本,点头所指的位置,文本的位置,arrowprops是更改指针的数据
plt.show()
2文本plt.text()
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(-10, 10, 0.1)
b = a ** 2
plt.plot(a, b)
plt.annotate("this is bottom", xy=(0, 1), xytext=(-1.87, 20),
arrowprops=dict(facecolor='b',
headwidth=15, width=10))
plt.text(-3.5,40,'function:y=x²',family='serif',size=20,color='r',
style='italic',weight=20,
bbox=dict(facecolor='r',alpha=0.2))
#坐标位置,显示内容,字体格式,字体大小,颜色,字体风格,字体类型,背景设置(颜色,透明度)
plt.show()
3.Tex公式(数学公式)
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim([-5, 5])
ax.set_ylim([-3, 3])
ax.text(-4,2,r'$\alpha_i\beta_j\lambda\omega$',
size=30,family='serif')
ax.text(-4,-2,r'$\sin(0)=\cos(\frac{\pi}{2})$',size=20,
family='serif')
ax.text(0.5,2,r'$\lim_{x\rightarrow y}\frac{1}{x^3}$',size=20,
family='serif')
ax.text(0.5,-2,r'$\sqrt[4]{x}=\sqrt{y}$',size=20,
family='serif')
plt.show()
4.填充颜色fill,fill.between
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(-np.pi,np.pi,0.01)
b1=np.sin(a)
b2=np.sin(2*a)
plt.fill(a,b1,'r',alpha=0.3)#填充
plt.fill(a,b2,'b',alpha=0.3)#填充
plt.show()
fill_between
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(-np.pi,np.pi,0.01)
b1=np.sin(a)
b2=np.sin(2*a)
fig=plt.figure()
ax=fig.add_subplot(1,1,1)
ax.plot(a,b1,'r')
ax.plot(a,b2,'b')
ax.fill_between(a,b1,b2,where=b1>=b2,facecolor='y',interpolate=True)
ax.fill_between(a,b1,b2,where=b2>=b1,facecolor='G',interpolate=True)
plt.show()