利用matplotlib和numpy库绘制三角函数图像,包括正弦函数、余弦函数、正切函数、余切函数图像
代码如下:
import numpy as np
import matplotlib as mp
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
pi = np.pi
font = FontProperties(fname=r'c:\windows\fonts\simsun.ttc', size=15)
def test():
x1 = np.arange(- pi, pi, 0.01)
y1 = np.sin(x1)
x2 = x1
y2 = np.cos(x2)
x3 = np.arange(-0.5 * pi + 0.01, 0.5 * pi - 0.01, 0.01)
y3 = np.tan(x3)
x4 = np.arange(0.01, pi - 0.01, 0.01)
y4 = np.divide(1, np.tan(x4))
plt.subplot(2, 2, 1)
plt.title('y=sin(x)')
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top')
plt.plot(x1, y1, '+:r')
plt.grid()
plt.subplot(2, 2, 2)
plt.title('y=cos(x)')
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top')
plt.plot(x2, y2, '+:g')
plt.grid()
plt.subplot(2, 2, 3)
plt.title('y=tan(x)')
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top')
plt.plot(x3, y3, '+:b')
plt.grid()
plt.subplot(2, 2, 4)
plt.title('y=cot(x)')
plt.xlabel('x', loc='right')
plt.ylabel('y', loc='top')
plt.plot(x4, y4, '+:y')
plt.grid()
plt.suptitle('三角函数图像', color='k', fontproperties=font)
plt.show()
成果
|