Exercise 11.1: Plotting a function
分析:
要绘制这样的函数图像,用点来描绘,选择给定区间较密集的点(用numpy.arange实现其x坐标在该区域的等距选取),y用给定的表达式给出,使用plot绘制即可。
相关说明:
- pylab将pyplot与numpy合并为一个命名空间。这对于交互式工作很方便,但是对于编程来说,建议将名称空间分开。
例:画 y = sin x 图像(我由此找到了解题的方法)
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.show()
2 . arrange()函数
函数说明:arange([start,] stop[, step,], dtype=None)根据start与stop指定的范围以及step设定的步长,生成一个 ndarray。 dtype : dtype
The type of the output array. If dtype
is not given, infer the data
type from the other input arguments.
ow、high、size三个参数。默认high是None,如果只有low,那范围就是[0,low)。如果有high,范围就是[low,high)。
一些例子:
np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
代码实现
x = np.arange(0,