numpy.meshgrid()理解(小结)


Posted in Python onAugust 01, 2019

本文的目的是记录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()

numpy.meshgrid()理解(小结)

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()

numpy.meshgrid()理解(小结)

示例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()

numpy.meshgrid()理解(小结)

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()

numpy.meshgrid()理解(小结)

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 相关文章推荐
Python中使用hashlib模块处理算法的教程
Apr 28 Python
利用python写个下载teahour音频的小脚本
May 08 Python
Python简单实现自动删除目录下空文件夹的方法
Aug 29 Python
Python 实现淘宝秒杀的示例代码
Jan 02 Python
python实现冒泡排序算法的两种方法
Mar 10 Python
python中map的基本用法示例
Sep 10 Python
浅谈Python中函数的定义及其调用方法
Jul 19 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
Jan 10 Python
Python3内置函数chr和ord实现进制转换
Jun 05 Python
PyCharm2020.1.2社区版安装,配置及使用教程详解(Windows)
Aug 07 Python
python实现简单的学生管理系统
Feb 22 Python
Python+tkinter实现高清图片保存
Mar 13 Python
Python-接口开发入门解析
Aug 01 #Python
Python列表(list)所有元素的同一操作解析
Aug 01 #Python
详解numpy.meshgrid()方法使用
Aug 01 #Python
解决安装python3.7.4报错Can''t connect to HTTPS URL because the SSL module is not available
Jul 31 #Python
numpy中的meshgrid函数的使用
Jul 31 #Python
pandas的排序和排名的具体使用
Jul 31 #Python
pandas如何处理缺失值
Jul 31 #Python
You might like
《星际争霸》各版本雷兽特点图文解析 雷兽不同形态一览
2020/03/02 星际争霸
PHP中通过HTTP_USER_AGENT判断是否为手机移动终端的函数代码
2013/02/14 PHP
php购物车实现方法
2015/01/03 PHP
支付宝接口开发集成支付环境小结
2015/03/17 PHP
PHP查找数值数组中不重复最大和最小的10个数的方法
2015/04/20 PHP
Symfony2安装的方法(2种方法)
2016/02/04 PHP
javascrip客户端验证文件大小及文件类型并重置上传
2011/01/12 Javascript
jquery实现带缩略图的全屏图片画廊效果实例
2015/06/25 Javascript
小议JavaScript中Generator和Iterator的使用
2015/07/29 Javascript
jquery对dom节点的操作【推荐】
2016/04/15 Javascript
JS出现失效的情况总结
2017/01/20 Javascript
Angular2 Service实现简单音乐播放器服务
2017/02/24 Javascript
JS动态添加的div点击跳转到另一页面实现代码
2017/09/30 Javascript
JS中touchstart事件与click事件冲突的解决方法
2018/03/12 Javascript
Vuex 单状态库与多模块状态库详解
2018/12/11 Javascript
在NPM发布自己造的轮子的方法步骤
2019/03/09 Javascript
简单谈谈offsetleft、offsetTop和offsetParent
2020/12/04 Javascript
python回调函数的使用方法
2014/01/23 Python
python实现简单socket通信的方法
2016/04/19 Python
python实现在函数中修改变量值的方法
2019/07/16 Python
使用OpenCV-python3实现滑动条更新图像的Canny边缘检测功能
2019/12/12 Python
详解python 支持向量机(SVM)算法
2020/09/18 Python
paramiko使用tail实时获取服务器的日志输出详解
2020/12/06 Python
凯普林包包西班牙官网:Kipling西班牙
2019/04/12 全球购物
3个CCIE对一个工程师的面试题
2012/05/06 面试题
法人委托书
2014/07/31 职场文书
贪污受贿检讨书范文
2014/11/19 职场文书
领导参观欢迎词
2015/01/26 职场文书
房屋认购协议书
2015/01/29 职场文书
个性与发展自我评价
2015/03/06 职场文书
警告通知
2015/04/25 职场文书
检讨书范文大全
2015/05/07 职场文书
毕业生政审意见范文
2015/06/04 职场文书
Nginx反向代理多个服务器的实现方法
2021/03/31 Servers
详解ZABBIX监控ESXI主机的问题
2022/06/21 Servers
python中validators库的使用方法详解
2022/09/23 Python