Exercise 11.1: Plotting a function
Plot the function f(x)=sin2(x−2)e−x2 f ( x ) = s i n 2 ( x − 2 ) e − x 2 over the interval [0, 2]. Add proper axis labels, a title, etc.
代码:
import numpy as np
import matplotlib.pyplot as plt
import math
x = np.linspace(0, 2, 1000)
y = [math.sin(i-2)**2 * math.exp(-i**2) for i in x]
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')
plt.title('y = $sin^2(x-2){e^{-x^2}}$')
plt.show()
运行效果: