函数图形 2 绘制.py
import numpy as npimport matplotlib.pyplot as plt
# 定义函数
def f1(x):
return x ** 2
def f2(x):
return np.cos(2 * x)
def f3(x):
return f1(x) * f2(x)
# 生成X轴数据
x = np.linspace(0, 10, 500)
# 绘制函数图形
plt.plot(x, f1(x), 'b-.', label='y1=x^2')
plt.plot(x, f2(x), 'g--', label='y2=cos(2x)')
plt.plot(x, f3(x), 'r-', label='y3=y1*y2')
plt.legend()
plt.show()
fig, axes = plt.subplots(nrows=3,ncols=1)
axes[0].plot(x, f1(x), 'b-.')
axes[0].set_title('y1=x^2')
axes[1].plot(x, f2(x), 'g--')
axes[1].set_title('y2=cos(2x)')
axes[2].plot(x, f3(x), 'r-')
axes[2].set_title('y3=y1*y2')
plt.tight_layout()
plt.show()
标签:plot,f2,axes,f1,python,练习,plt,y1 From: https://www.cnblogs.com/yunbianshangdadun/p/17404388.html