numpy.meshgrid()理解
本文的目的是记录meshgrid()的理解过程:
step1. 通过一个示例引入创建网格点矩阵;
step2. 基于步骤1,说明meshgrid()的作用;
step3. 详细解读meshgrid()的官网定义;
说明:step1和2 的数据都是基于笛卡尔坐标系的矩阵,目的是为了方便讨论。
step1. 通过一个示例引入创建网格点矩阵;
示例1,创建一个2行3列的网格点矩阵。
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
############################
#File Name: meshgrid1.py
#Brief:
#Author: frank
#Mail: frank0903@aliyun.com
#Created Time:2018-06-14 21:33:14
############################
import numpy as np
import matplotlib.pyplot as plt X = 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()理解的更多相关文章
- 对numpy.meshgrid()理解
一句话解释numpy.meshgrid()——生成网格点坐标矩阵.关键词:网格点,坐标矩阵 网格点是什么?坐标矩阵又是什么鬼?看个图就明白了: 图中,每个交叉点都是网格点,描述这些网格点的坐标的矩阵, ...
- numpy.meshgrid()
numpy提供的numpy.meshgrid()函数可以让我们快速生成坐标矩阵X,Y 语法:X,Y = numpy.meshgrid(x, y)输入:x,y,就是网格点的横纵坐标列向量(非矩阵)输出: ...
- numpy meshgrid 和 mgrid 的两个简单实例和解析
numpy.meshgrid 和 numpy.mgrid 用于返回包含坐标向量的坐标矩阵. 当坐标矩阵为二维时, 可用于在图像变形时构建网格. 实例一 from __future__ import p ...
- numpy.meshgrid的理解以及3D曲面图绘制(梯度下降法实现过程)
相关概念: 1.x向量和y向量 import numpy as np import matplotlib.pyplot as plt x = np.array([[0,1,2,3], [0,0,0,0 ...
- numpy meshgrid函数
1.meshgrid函数用两个坐标轴上的点在平面上画格. 用法: [X,Y]=meshgrid(x,y) [X,Y]=meshgrid(x)与[X,Y]=meshgrid(x,x)是等同的 [X, ...
- NumPy之:理解广播
目录 简介 基础广播 广播规则 简介 广播描述的是NumPy如何计算不同形状的数组之间的运算.如果是较大的矩阵和较小的矩阵进行运算的话,较小的矩阵就会被广播,从而保证运算的正确进行. 本文将会以具体的 ...
- numpy深入理解剖析
http://www.scipy-lectures.org/advanced/advanced_numpy/index.html
- 《利用Python进行数据分析·第2版》第四章 Numpy基础:数组和矢量计算
<利用Python进行数据分析·第2版>第四章 Numpy基础:数组和矢量计算 numpy高效处理大数组的数据原因: numpy是在一个连续的内存块中存储数据,独立于其他python内置对 ...
- Numpy应用100问
对于从事机器学习的人,python+numpy+scipy+matplotlib是重要的基础:它们基本与matlab相同,而其中最重要的当属numpy:因此,这里列出100个关于numpy函数的问题, ...
随机推荐
- WinCE6.0 2012年补丁下载地址
Windows CE6.0 2012年补丁包WinCEPB60-121231-Product-Update-Rollup-Armv4I.msi下载地址:http://www.microsoft.com ...
- bash: hadoop:command not found
这种情况应该是hadoop的bin环境变量没有配置好 打开 gedit ~/.bashrc 修改 export HADOOP_HOME=/home/hadoop1/softwares/hadoop- ...
- 可以ping通虚拟机但不能telnet 9000端口
突然发现eclipse不能连上虚拟机了,报错是本机连接不上9000的端口. 觉得有点奇怪,就在命令行里试图ping通虚拟机,成功:但尝试这telnet 9000端口的时候,却报错连接不上. 上网查了这 ...
- 一般源码安装添加的GD库 是不支持 jpeg 格式的图片的
一般源码安装添加的GD库 是不支持 jpeg 格式的图片的,只支持如下格式 GD Support enabled GD Version bundled (2.0.34 compatible) GIF ...
- 云计算之路-阿里云上:启用Windows虚拟内存引发的CPU 100%故障
今天上午11:35~11:40左右,由于负载均衡中的两台云服务器CPU占用突然飚至100%,造成网站5分钟左右不能正常访问,请大家带来了麻烦,请谅解! (上图中红色曲线表示CPU占用) 经过分析,我们 ...
- Gstreamer学习
Gstreamer学习笔记----Gstreamer架构设计思想 http://blog.csdn.net/tx3344/article/details/7497434 Gstreamer到底是个啥? ...
- python 学习定时任务apscheduler模块
最近在解决定时任务问题找到了apscheduler模块,贴一段代码 from apscheduler.schedulers.blocking import BlockingSchedulerimpor ...
- python——python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- TP框架中模板赋值
TP框架中模板赋值 $this->assign('name',$value); $this->name = $value; // 两种写法是等效的
- CSS优先级的详细解说
一.什么是CSS优先级? 所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序. 二.CSS优先级规则 既然样式有优先级,那么就会有一个规则来约定这个优先级,而这个“规则”就是本次所需要讲的重 ...