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对多属性的重复数据去重实例
Apr 18 Python
对Python中的@classmethod用法详解
Apr 21 Python
浅谈Python接口对json串的处理方法
Dec 19 Python
python环境路径配置以及命令行运行脚本
Apr 02 Python
python画双y轴图像的示例代码
Jul 07 Python
Python列表切片常用操作实例解析
Dec 16 Python
python实现井字棋小游戏
Mar 04 Python
浅谈Django前端后端值传递问题
Jul 15 Python
浅谈对python中if、elif、else的误解
Aug 20 Python
Pycharm编辑器功能之代码折叠效果的实现代码
Oct 15 Python
Python 制作自动化翻译工具
Apr 25 Python
Python基础之tkinter图形化界面学习
Apr 29 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
解决GD中文乱码问题
2007/02/14 PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
2016/05/19 PHP
php版微信开发之接收消息,自动判断及回复相应消息的方法
2016/09/23 PHP
Zend Framework入门教程之Zend_View组件用法示例
2016/12/09 PHP
php+mysql+jquery实现日历签到功能
2017/02/27 PHP
PHP 代码简洁之道(小结)
2019/10/16 PHP
jQuery AnythingSlider滑动效果插件
2010/02/07 Javascript
基于jquery的弹出提示框始终处于窗口的居中位置(类似于alert弹出框的效果)
2011/09/28 Javascript
js中的异常处理try...catch使用介绍
2013/09/21 Javascript
利用js实现禁止复制文本信息
2015/06/03 Javascript
Backbone中View之间传值的学习心得
2016/08/09 Javascript
深入理解Nodejs Global 模块
2017/06/03 NodeJs
详解vue-cli + webpack 多页面实例配置优化方法
2017/07/13 Javascript
JS解析url查询参数的简单代码
2017/08/06 Javascript
JavaScript查看代码运行效率console.time()与console.timeEnd()用法
2019/01/18 Javascript
JavaScript中的"=、==、==="区别讲解
2019/01/22 Javascript
微信小程序 WXML节点信息查询详解
2019/07/29 Javascript
webpack打包html里面img后src为“[object Module]”问题
2019/12/22 Javascript
如何在Vue中使localStorage具有响应式(思想实验)
2020/07/14 Javascript
vue项目中播放rtmp视频文件流的方法
2020/09/17 Javascript
[04:04]DOTA2亚洲邀请赛比赛场馆&酒店全攻略
2017/03/23 DOTA
python统计一个文本中重复行数的方法
2014/11/19 Python
Python中的fileinput模块的简单实用示例
2015/07/09 Python
在Python中pandas.DataFrame重置索引名称的实例
2018/11/06 Python
Python分析彩票记录并预测中奖号码过程详解
2019/07/09 Python
Python实现汇率转换操作
2020/05/03 Python
纯CSS实现的大小渐变、渐远效果
2014/04/15 HTML / CSS
移动端HTML5实现文件上传功能【附代码】
2016/03/25 HTML / CSS
英国最大的纸工艺品商店:CraftStash
2018/12/01 全球购物
工程部主管岗位职责
2013/11/17 职场文书
会计电算化专业自荐信
2014/03/15 职场文书
学校教师安全责任书
2014/07/23 职场文书
新闻稿格式范文
2015/07/18 职场文书
提档介绍信范文
2015/10/22 职场文书
有关保护环境的宣传标语100条
2019/08/07 职场文书
mysql数据库隔离级别详解
2022/06/16 MySQL