可视化y = e^(1/x)函数:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
fig, ax = plt.subplots(figsize=(12, 8))
def e_to_1_over_x(x):
return np.where(x != 0, np.exp(1/x), np.nan)
# 线性刻度图
x_linear = np.linspace(-10, 10, 10000)
y_linear = e_to_1_over_x(x_linear)
ax.plot(x_linear, y_linear, 'b')
ax.set_title('y = e^(1/x)')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.grid(True, alpha=0.3)
ax.set_ylim(-1, 5)
ax.set_xlim(-10, 10)
# 添加红色的 X=0 和 Y=0 轴
ax.axhline(y=0, color='r', linewidth=1.5)
ax.axvline(x=0, color='r', linewidth=1.5)
# 添加水平渐近线
ax.axhline(y=1, color='g', linestyle='--', alpha=0.7)
ax.text(5, 1.1, 'y = 1 (水平渐近线)', color='g')
# 添加注释
ax.annotate('x → 0+, y → +∞', xy=(0.5, 4.5), xytext=(2, 4),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate('x → 0-, y → 0', xy=(-0.5, 0.1), xytext=(-3, 0.5),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate('x → +∞, y → 1', xy=(8, 1.1), xytext=(6, 2),
arrowprops=dict(facecolor='black', shrink=0.05))
ax.annotate('x → -∞, y → 1', xy=(-8, 1.1), xytext=(-6, 2),
arrowprops=dict(facecolor='black', shrink=0.05))
plt.tight_layout()
plt.show()