Numpy中的meshgrid()函数
官方解释:
np.meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors. 从坐标向量中返回坐标矩阵
不够直观
直观的例子
二维坐标系中,X轴可以取三个值 1,2,3, Y轴可以取三个值 7,8, 请问可以获得多少个点的坐标?
显而易见是 6 个:
(1, 7) (2, 7) (3, 7)
(1, 8) (2, 8) (3, 8)
np.meshgrid()
就是干这个的!
#coding:utf-8
import numpy as np
# 坐标向量
a = np.array([1,2,3])
# 坐标向量
b = np.array([7,8])
# 从坐标向量中返回坐标矩阵
# 返回list,有两个元素,第一个元素是X轴的取值,第二个元素是Y轴的取值
res = np.meshgrid(a,b)
#返回结果: [array([ [1,2,3] [1,2,3] ]), array([ [7,7,7] [8,8,8] ])]
同理还可以生成更高维度的坐标矩阵
本文的目的是记录 meshgrid()
的理解过程:
step1. 通过一个示例引入创建网格点矩阵;
step2. 基于步骤1,说明 meshgrid()
的作用;
step3. 详细解读 meshgrid()
的官网定义;
说明:step1 和 2 的数据都是基于笛卡尔坐标系的矩阵,目的是为了方便讨论。
step1. 通过一个示例引入创建网格点矩阵;
示例1,创建一个 2 行 3 列的网格点矩阵。
import numpy as np
import matplotlib.pyplot as pltX = np.array([[0, 0.5, 1],[0, 0.5, 1]])
print("X的维度:{},shape:{}".format(X.ndim, X.shape))
Y = np.array([[0, 0, 0],[1, 1, 1]])
print("Y的维度:{},shape:{}".format(Y.ndim, Y.shape))plt.plot(X, Y, 'o--')
plt.grid(True)
plt.show()
X矩阵是: [[0. 0.5 1. ], [0. 0.5 1. ]]
Y矩阵是: [[0 0 0], [1 1 1]]
step2. meshgrid()
的作用;
当要描绘的 矩阵网格点的数据量小的时候,可以用上述方法构造网格点坐标数据;
但是如果是一个 (256, 100) 的整数矩阵网格,要怎样构造数据呢?
方法1 : 将 x 轴上的 100 个整数点组成的行向量,重复 256 次,构成 shape(256,100)
的 X 矩阵;将 y 轴上的 256 个整数点组成列向量, 重复 100 次构成 shape(256,100)
的 Y 矩阵
显然方法1 的数据构造过程很繁琐, 也不方便调用, 那么有没有更好的办法呢?
of course!!! 那么 meshgrid()
就显示出它的作用了
使用 meshgrid
方法,你只需要构造一个表示 x
轴上的坐标的向量和一个表示 y
轴上的坐标的向量; 然后作为参数给到 meshgrid()
, 该函数就会返回相应维度的两个矩阵;
例如,你想构造一个 2 行 3 列的矩阵网格点, 那么 x
生成一个 shape(3,)
的向量, y
生成一个 shape(2,)
的向量, 将 x
, y
传入 meshgrid()
, 最后返回的 X , Y 矩阵的 shape(2,3)
示例2,使用 meshgrid()
生成 step1 中的网格点矩阵
x = np.array([0, 0.5, 1])
y = np.array([0,1])xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()
示例3,生成一个20行30列的网格点矩阵
x = np.linspace(0,500,30)
print("x的维度:{},shape:{}".format(x.ndim, x.shape))
print(x)
y = np.linspace(0,500,20)
print("y的维度:{},shape:{}".format(y.ndim, y.shape))
print(y)xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))plt.plot(xv, yv, '.')
plt.grid(True)
plt.show()
step3. 详细解读 meshgrid()
的官网定义;
numpy.meshgrid(*xi, **kwargs)
Return coordinate matrices from coordinate vectors.
根据输入的坐标向量生成对应的坐标矩阵
Parameters:
x1, x2,…, xn : array_like
1-D arrays representing the coordinates of a grid.
indexing : {‘xy’, ‘ij’}, optional
Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.
sparse : bool, optional
If True a sparse grid is returned in order to conserve memory. Default is False.
copy : bool, optional
If False, a view into the original arrays are returned in order to conserve memory.
Default is True. Please note that sparse=False, copy=False will likely return non-contiguous arrays.
Furthermore, more than one element of a broadcast array may refer to a single memory location.
If you need to write to the arrays, make copies first.
Returns:
X1, X2,…, XN : ndarray
For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) ,
return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’
or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’
with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.
针对 indexing
参数的说明:
indexing
只是影响 meshgrid()
函数返回的矩阵的表示形式,但并不影响坐标点
x = np.array([0, 0.5, 1])
y = np.array([0,1])xv,yv = np.meshgrid(x, y)
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()
x = np.array([0, 0.5, 1])
y = np.array([0,1])xv,yv = np.meshgrid(x, y,indexing='ij')
print("xv的维度:{},shape:{}".format(xv.ndim, xv.shape))
print("yv的维度:{},shape:{}".format(yv.ndim, yv.shape))
print(xv)
print(yv)plt.plot(xv, yv, 'o--')
plt.grid(True)
plt.show()
Numpy中的meshgrid()函数相关推荐
- python grid函数_详解numpy中的meshgrid函数用法
numpy中的meshgrid函数的使用 numpy官方文档meshgrid函数帮助文档https://docs.scipy.org/doc/numpy/reference/generated/num ...
- numpy中的meshgrid函数
numpy官方文档meshgrid函数帮助文档https://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html mesh ...
- Numpy中np.mashgri() 函数介绍及2种应用场景
@[toc](Numpy中np.mashgri() 函数介绍及2种应用场景 文章目录:) 近期在好几个地方都看到meshgrid的使用,虽然之前也注意到meshgrid的用法. 但总觉得印象不深刻,不 ...
- python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组、使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank)
python使用numpy中的flatten函数将2D numpy数组拉平为1Dnumpy数组.使用np.linalg.matrix_rank函数计算2D numpy数组的秩(rank) 目录
- Python使用numpy中的hstack函数水平堆叠(horizontally stack)数组实战
Python使用numpy中的hstack函数水平堆叠(horizontally stack)数组实战 目录 Python使用numpy中的hstack函数水平堆叠(horizontally stac ...
- python中tile的用法_Python:numpy中的tile函数
在学习机器学习实教程时,实现KNN算法的代码中用到了numpy的tile函数,因此对该函数进行了一番学习: tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复 ...
- Numpy中使用astype函数将字符串格式数据转换为数值数据类型
Numpy中使用astype函数将字符串格式数据转换为数值数据类型 目录 Numpy中使用astype函数将字符串格式数据转换为数值数据类型 numpy是什么?numpy和list有哪些区别? Num ...
- Numpy中使用astype函数转换numpy数组数据类型
Numpy中使用astype函数转换numpy数组数据类型 目录 Numpy中使用astype函数转换numpy数组数据类型 numpy是什么?numpy和list有哪些区别? Numpy中使用ast ...
- Python语言Numpy包之Meshgrid 函数
1 Meshgrid 函数的基本用法 在 Numpy 的官方文章里,meshgrid 函数的英文描述也显得文绉绉的,理解起来有些难度.可以这么理解, meshgrid 函数用两个坐标轴上的点在平面上画 ...
最新文章
- Oracle VM VirtualBox下各种视图切换
- 小微商 获取平台证书 报错
- linux如何编译php扩展,linux环境下编译php扩展
- n皇后问题java版
- 项目中有出现过缓存击穿,简单说说怎么回事?
- 结对项目 浪曦计时器
- ctf xss利用_Csrf+Xss组合拳
- 计算机唱歌按键学猫叫6,电脑键盘按键的功能介绍(最全的键盘各键及组合键功能说明)...
- C++实现多级时间轮定时器
- 长沙理工大学第十二届ACM大赛【9/12】
- iphone更改照片分辨率?手机怎样修改图片分辨率?
- 二级C语言操作例题(十六)
- 鸿蒙 3.0 来了!新版本就是强啊!!
- WPF入门教程(一)---基础
- 七夕快到了,用python给女朋友画张素描吧
- mysql中having的意思_正确理解MySQL中的where和having的区别
- CygWin、MinGw和Msys的区别
- Android性能优化面试题集锦,实战篇
- EAS融资租赁系统(财务业务一体化)
- python3图片进行base64编码与解码
热门文章
- HTML5 网站大观:应用图片大背景的优秀 HTML5 网站作品
- ***:***之路的必备技能
- JS如何设置打开页面后将光标定位在指定的输入框?
- 极客新闻——04、WiFi万能钥匙万玉权:管理应该是“自下而上”
- 修改模拟器的IMEI号
- 大数据分析,在中国,找个身高1米7年入20万的老公,到底有多难?
- 从MongoDB迁移到ES后,我们减少了80%的服务器
- 程序员吐槽:非常后悔3年前选择加入互联网行业,因为短期的高工资断送了自己长期的职业生涯发展...
- 如何从0写一个服务网关?
- 怎么用leangoo做需求管理?(用户故事地图)