11,手动绘制散点图的背景颜色

# 导包
import numpy as np
import matplotlib.pyplot as plt

#
x = np.linspace(1,3,num=100)
y = np.linspace(6,8,num=100)

xx,yy = np.meshgrid(x,y)
display(xx,yy)


xx.shape
xy = np.c_[xx.reshape(10000,),yy.reshape(10000,)]

plt.scatter(xy[:,0],xy[:,1])
plt.scatter([1,2,3],[6,7,8])

  

numpy.linspace()等差数列函数

numpy.linspace(start, stop[, num=50[, endpoint=True[, retstep=False[, dtype=None]]]]])

返回在指定范围内的均匀间隔的数字(组成的数组),也即返回一个等差数列

start - 起始点,

stop - 结束点

num - 元素个数,默认为50,

endpoint - 是否包含stop数值,默认为True,包含stop值;若为False,则不包含stop值

retstep - 返回值形式,默认为False,返回等差数列组,若为True,则返回结果(array([`samples`, `step`])),

dtype - 返回结果的数据类型,默认无,若无,则参考输入数据类型。

import numpy as np

a = np.linspace(1,10,5,endpoint= True)
print(a) # [ 1.    3.25  5.5   7.75 10.  ]
b = np.linspace(1,10,5,endpoint= False)
print(b) #[1.  2.8 4.6 6.4 8.2]
c = np.linspace(1,10,5,retstep = False)
print(c) # [ 1.    3.25  5.5   7.75 10.  ]
d = np.linspace(1,10,5,retstep = True)
print(d) # (array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ]), 2.25)

np.meshgrid()

np.meshgrid从坐标向量返回坐标矩阵。

x = np.arange(-2,2)
y = np.arange(0,3)#生成一位数组,其实也就是向量

x
Out[31]: array([-2, -1,  0,  1])

y
Out[32]: array([0, 1, 2])

z,s = np.meshgrid(x,y)#将两个一维数组变为二维矩阵

z
Out[36]: 
array([[-2, -1,  0,  1],
       [-2, -1,  0,  1],
       [-2, -1,  0,  1]])

s
Out[37]: 
array([[0, 0, 0, 0],
       [1, 1, 1, 1],
       [2, 2, 2, 2]])

也就是说,它讲 x 变成了矩阵 z 的行向量,y 变成了矩阵 s 的列向量。

反过来,也是一样的:

z,s = np.meshgrid(y,x)

z
Out[40]: 
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])

s
Out[41]: 
array([[-2, -2, -2],
       [-1, -1, -1],
       [ 0,  0,  0],
       [ 1,  1,  1]])

  以上面这个例子来说,z 和 s 就构成了一个坐标矩阵,实际上也就是一个网格,不知道你没有注意到,z 和 s 的维数是一样的,是一个4 × 4的网格矩阵,也就是坐标矩阵。

numpy中np.c_和np.r_

[1 2 3 4 5 6]

[[1 4]
 [2 5]
 [3 6]]

[[1 4 1]
 [2 5 2]
 [3 6 3]]

  在numpy中,一个列表虽然是横着表示的,但它是列向量。

原文地址:https://www.cnblogs.com/feifeifeisir/p/10511766.html