import numpy as np
import matplotlib.pyplot as plt
# 定义一个函数,例如 y = sin(x)
def f(x):
return np.sin(x)
# 生成 x 的数据点,从 -2π 到 2π,共 400 个点
x = np.linspace(-2 * np.pi, 2 * np.pi, 400)
# 计算对应的 y 值
y = f(x)
# 创建图像
plt.figure(figsize=(8, 6))
# 绘制函数图像
plt.plot(x, y, label='sin(x)', color='b')
# 添加标题和标签
plt.title('Function Plot: y = sin(x)')
plt.xlabel('x')
plt.ylabel('y')
# 显示图例
plt.legend()
# 显示网格
plt.grid(True)
# 显示图像
plt.show()
numpy 来生成数据点,并使用 matplotlib.pyplot 来绘制图像。f(x) = sin(x)。np.linspace 生成从 -2π 到 2π 的 400 个等间距的 x 值。f(x) 计算对应的 y 值。plt.figure 创建一个新的图形窗口。plt.plot 绘制函数图像,并指定颜色和标签。plt.title, plt.xlabel, 和 plt.ylabel 添加图像的标题和坐标轴标签。plt.legend 显示图例。plt.grid 显示网格线。plt.show 显示图像。上一篇:python ord和chr
下一篇:python 私有函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站