Exercise 1: Plotting a function
题意
题解
numpy.linspace 生成一段区间内均匀分布的点。用这个函数生成横坐标。
numpy 提供了很多数学函数,这里能用到的有 numpy.sin, numpy.power, numpy.exp, numpy.negative。
matplotlib.pyplot 提供了绘制图像的函数 plot,也提供了设置横坐标名、纵坐标名、标题的函数 xlabel, ylabel, title。
from numpy import linspace, power, sin, negative, exp
from matplotlib import pyplot
x = linspace(0, 2, 100)
y = power(sin(x - 2), 2) * exp(negative(power(x, 2)))
pyplot.plot(x, y)
pyplot.ylabel('y')
pyplot.xlabel('x')
pyplot.title('y = $[sin^2(x-2)]e^{-x^2}$')
pyplot.show()