如何在 Python 中将 y=1/x 绘制为单个图形?
matplotlibpythondata visualization更新于 2025/7/11 4:28:17
要在 Python 中将 y=1/x 绘制为单个图形,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 使用 numpy 创建数据点。
- 使用 plot() 方法绘制 x 和 1/x 数据点。
- 在图形上放置图例。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-10, 10, 101)
plt.plot(x, 1/x, label='$f(x)=\frac{1}{x}$')
plt.legend(loc='upper left')
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

