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解决字典中的值是列表问题的方法
Mar 04 Python
Python ORM框架SQLAlchemy学习笔记之安装和简单查询实例
Jun 10 Python
Python打造出适合自己的定制化Eclipse IDE
Mar 02 Python
python+selenium识别验证码并登录的示例代码
Dec 21 Python
Python实现绘制双柱状图并显示数值功能示例
Jun 23 Python
对python 数据处理中的LabelEncoder 和 OneHotEncoder详解
Jul 11 Python
Python中如何使用if语句处理列表实例代码
Feb 24 Python
提升Python程序性能的7个习惯
Apr 14 Python
django框架自定义模板标签(template tag)操作示例
Jun 24 Python
Python any()函数的使用方法
Oct 28 Python
Django框架HttpRequest对象用法实例分析
Nov 01 Python
Python 基于jwt实现认证机制流程解析
Jun 22 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
延长phpmyadmin登录时间的方法
2011/02/06 PHP
thinkphp实现面包屑导航(当前位置)例子分享
2014/05/10 PHP
joomla组件开发入门教程
2016/05/04 PHP
PHP对象的浅复制与深复制的实例详解
2017/10/26 PHP
laravel框架模型、视图与控制器简单操作示例
2019/10/10 PHP
关于laravel 子查询 & join的使用
2019/10/16 PHP
PHP防止sql注入小技巧之sql预处理原理与实现方法分析
2019/12/13 PHP
克隆javascript对象的三个方法小结
2011/01/12 Javascript
JavaScript中的property和attribute介绍
2011/12/26 Javascript
防止浏览器记住用户名及密码的简单实用方法
2013/04/22 Javascript
js实现滑动触屏事件监听的方法
2015/05/05 Javascript
jquery实现点击弹出可放大居中及关闭的对话框(附demo源码下载)
2016/05/10 Javascript
JavaScript中ES6字符串扩展方法
2016/08/26 Javascript
vue.js实现价格格式化的方法
2017/05/23 Javascript
VUE2实现事件驱动弹窗示例
2017/10/21 Javascript
在AngularJs中设置请求头信息(headers)的方法及不同方法的比较
2018/09/04 Javascript
jQuery.validate.js表单验证插件的使用代码详解
2018/10/22 jQuery
简单了解node npm cnpm的具体使用方法
2019/02/27 Javascript
vue实现跨域的方法分析
2019/05/21 Javascript
[01:56]《DOTA2》中文配音CG
2013/04/22 DOTA
详解appium+python 启动一个app步骤
2017/12/20 Python
python将处理好的图像保存到指定目录下的方法
2019/01/10 Python
对python中的try、except、finally 执行顺序详解
2019/02/18 Python
python设计微型小说网站(基于Django+Bootstrap框架)
2019/07/08 Python
Python实现一个数组除以一个数的例子
2019/07/20 Python
pycharm 关掉syntax检查操作
2020/06/09 Python
html5绘制时钟动画
2014/12/15 HTML / CSS
佳能法国商店:Canon法国
2019/02/14 全球购物
Interrail法国:乘火车探索欧洲,最受欢迎的欧洲铁路通票
2019/08/27 全球购物
海南地接欢迎词
2014/01/14 职场文书
租车协议书
2015/01/27 职场文书
政协常委会议主持词
2015/07/03 职场文书
婚前协议书怎么写,才具有法律效力呢 ?
2019/06/28 职场文书
十二月早安励志心语大全
2019/12/03 职场文书
Python字符串对齐方法使用(ljust()、rjust()和center())
2021/04/26 Python
MySQL七种JOIN类型小结
2021/10/24 MySQL