import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig, axs = plt.subplots(2, 3, figsize=(15, 10))
# 1. 幂函数
x = np.linspace(-2, 2, 200)
axs[0, 0].plot(x, x**2, label='y = x^2')
axs[0, 0].plot(x, x**3, label='y = x^3')
axs[0, 0].plot(x, x**0.5, label='y = x^(1/2)')
axs[0, 0].set_title('幂函数')
axs[0, 0].legend(loc='upper left')
axs[0, 0].grid(True)
axs[0, 0].set_ylim(-2, 4)
# 2. 指数函数
x = np.linspace(-2, 2, 200)
axs[0, 1].plot(x, 2**x, label='y = 2^x')
axs[0, 1].plot(x, 0.5**x, label='y = (1/2)^x')
axs[0, 1].set_title('指数函数')
axs[0, 1].legend(loc='upper left')
axs[0, 1].grid(True)
axs[0, 1].set_ylim(0, 4)
# 3. 对数函数
x = np.linspace(0.1, 5, 200)
axs[0, 2].plot(x, np.log2(x), label='y = log_2(x)')
axs[0, 2].plot(x, np.log(x), label='y = ln(x)')
axs[0, 2].set_title('对数函数')
axs[0, 2].legend(loc='upper left')
axs[0, 2].grid(True)
axs[0, 2].set_ylim(-3, 3)
# 4. 三角函数
x = np.linspace(-2*np.pi, 2*np.pi, 200)
axs[1, 0].plot(x, np.sin(x), label='y = sin(x)')
axs[1, 0].plot(x, np.cos(x), label='y = cos(x)')
axs[1, 0].set_title('三角函数')
axs[1, 0].legend(loc='upper left')
axs[1, 0].set_ylim(-1.5, 1.5)
axs[1, 0].grid(True)
axs[1, 0].set_xticks([-2*np.pi, -np.pi, 0, np.pi, 2*np.pi])
axs[1, 0].set_xticklabels(['-2π', '-π', '0', 'π', '2π'])
# 5. 反三角函数
x = np.linspace(-1, 1, 200)
axs[1, 1].plot(x, np.arcsin(x), label='y = arcsin(x)')
axs[1, 1].plot(x, np.arccos(x), label='y = arccos(x)')
axs[1, 1].plot(x, np.arctan(x), label='y = arctan(x)')
axs[1, 1].set_title('反三角函数')
axs[1, 1].legend(loc='upper left')
axs[1, 1].grid(True)
axs[1, 1].set_ylim(-2, 2)
# 移除多余的子图
fig.delaxes(axs[1, 2])
plt.tight_layout()
plt.show()