用Python使用numpy库和matplotlib.pyplot库绘制y=e^(-x)*sin(2x)和 y=sin(2πx)的函数曲线
时间: 2024-11-22 16:40:17 浏览: 40
在Python中,我们可以使用NumPy库来生成数据,并结合Matplotlib库的`pyplot`模块来创建可视化图表。以下是绘制两个函数`y = e^(-x) * sin(2x)`和`y = sin(2πx)`的简单步骤:
首先,确保已经安装了这两个库,如果没有,可以使用pip安装:
```bash
pip install numpy matplotlib
```
接下来,编写Python代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义两个函数
def func1(x):
return np.exp(-x) * np.sin(2*x)
def func2(x):
return np.sin(2 * np.pi * x)
# 创建x轴的数据点
x_values = np.linspace(0, 5, 400) # 从0到5,包含400个等间距点
# 计算并存储每个函数在x值上的结果
y1_values = func1(x_values)
y2_values = func2(x_values)
# 绘制两条曲线
plt.plot(x_values, y1_values, label='e^(-x)*sin(2x)', color='blue')
plt.plot(x_values, y2_values, label='sin(2πx)', linestyle='--', color='red')
# 添加图例、标题和坐标轴标签
plt.title('Plot of Two Functions: e^(-x)*sin(2x) and sin(2πx)')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
# 显示图形
plt.show()
```
阅读全文
相关推荐


















