文章目录
一、Numpy
NumPy 的主要对象是齐次多维数组。它是一个元素表(通常是元素是数字),其中所有元素类型都相同,元素以正整数元组索引。在 NumPy 维度(dimension)被称为轴(axis)。
ps. 有几个轴就是几维数组,符合平时生活中有 x, y 两个坐标轴就是二维空间,再加上 z 轴就是三维空间的概念
例如三维空间空一个点的坐标 [1, 2, 1] 有一个轴。这个轴有 3 个元素,即该轴的长度是 3。下面代码区中的数组有两个轴。第一个轴长度是 2,第二个长度是 3.
[[ 1., 0., 0.],
[ 0., 1., 2.]]
Numpy 的数组类称做 ndarry,别名是 array。注意 numpy.array 和 Python 标准库的类 array.array 不同,标准库的类只处理一维数组(one-dimensional arrays)。
1. 重要属性
-
ndarray.ndim
the number of axes (dimensions) of the array.
-
ndarray.shape
数组的维度(the dimensions of the array)。 以一个整型元组的方式表示数组中每个维度的大小。比如对一个有 n 行 m 列的矩阵来说,其 shape 属性为 (n, m)。The length of the shape tuple is therefore the number of axes, ndim.
-
ndarray.size
数组元素总数。相当于 shape 中每个元素的乘积。
-
ndarray.dtype
一个用来描述数组中元素类型的对象。我们可以使用 Python 标准类型来创建指定该对象,NumPy 也提供了自己的类型,如 numpy.int32, numpy.int16, and numpy.float641 等
-
ndarray.itemsize
数组中每个元素的字节大小。 For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
二、Matpltlib
参考:菜鸟教程
Matplotlib是python的绘图库,经常与Numpy一起使用,停工了有效的Matlab开源替代方案,也可以和图形工具包一起使用,比如PyQt和wxPython。
plt.title("图标题")
plt.xlabel("x轴标识")
plt.ylabel("y轴标识")
创建x和y的值
x = np.arange(1,11)
y = 2 * x + 5
1.Plot()
直线绘制使用matplotlib中的pyplot子模块的plot()函数绘制。
plt.plot(x,y)
plt.show()
要显示点而不是线
plt.plot(x,y,"ob")
plt.show()
2.Subplot()
允许在同一图中绘制不同的东西。
import numpy as np
import matplotlib.pyplot as plt
# 计算正弦和余弦曲线上的点的 x 和 y 坐标
x = np