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读写Excel文件方法介绍
Nov 22 Python
win7上python2.7连接mysql数据库的方法
Jan 14 Python
解决PyCharm同目录下导入模块会报错的问题
Oct 13 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
Django 静态文件配置过程详解
Jul 23 Python
python内存监控工具memory_profiler和guppy的用法详解
Jul 29 Python
Python 调用 Outlook 发送邮件过程解析
Aug 08 Python
Python 一行代码能实现丧心病狂的功能
Jan 18 Python
使用python的turtle函数绘制一个滑稽表情
Feb 28 Python
Python使用sqlite3模块内置数据库
May 07 Python
python 用pandas实现数据透视表功能
Dec 21 Python
python通过函数名调用函数的几种方法总结
Jun 07 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
PHP下escape解码函数的实现方法
2010/08/08 PHP
PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
2011/08/11 PHP
Linux Apache PHP Oracle 安装配置(具体操作步骤)
2013/06/17 PHP
php导入excel文件到mysql数据库的方法
2015/01/14 PHP
基于php实现的php代码加密解密类完整实例
2016/10/12 PHP
PHP 实现base64编码文件上传出现问题详解
2020/09/01 PHP
phpcmsv9.0任意文件上传漏洞解析
2020/10/20 PHP
让IE6支持min-width和max-width的方法
2010/06/25 Javascript
UI Events 用户界面事件
2012/06/27 Javascript
asp.net刷新本页面的六种方法总结
2014/01/07 Javascript
使用JavaScript的ActiveXObject对象检测应用程序是否安装的方法
2014/04/15 Javascript
js实现按钮加背景图片常用方法
2014/11/01 Javascript
深入浅出理解javaScript原型链
2015/05/09 Javascript
JS实现快速比较两个字符串中包含有相同数字的方法
2017/09/11 Javascript
iframe与主框架跨域相互访问实现方法
2017/09/14 Javascript
详解vue-cli3多页应用改造
2019/06/04 Javascript
js实现跟随鼠标移动的小球
2019/08/26 Javascript
CentOS 8.2服务器上安装最新版Node.js的方法
2020/12/16 Javascript
windows下wxPython开发环境安装与配置方法
2014/06/28 Python
利用Psyco提升Python运行速度
2014/12/24 Python
Python栈类实例分析
2015/06/15 Python
python循环定时中断执行某一段程序的实例
2019/06/29 Python
pip安装提示Twisted错误问题(Python3.6.4安装Twisted错误)
2020/05/09 Python
用python实现学生管理系统
2020/07/24 Python
CSS3弹性布局内容对齐(justify-content)属性使用详解
2017/07/31 HTML / CSS
AmazeUI 等分网格的实现示例
2020/08/25 HTML / CSS
新加坡最佳婴儿用品店:Mamahood.com.sg
2018/08/26 全球购物
活动总结怎么写啊
2014/05/07 职场文书
公司领导班子对照材料
2014/08/18 职场文书
工人先锋号事迹材料
2014/12/24 职场文书
保证书格式
2015/01/16 职场文书
投诉信回复范文
2015/07/03 职场文书
导游词之吉林吉塔
2019/11/11 职场文书
mybatis使用oracle进行添加数据的方法
2021/04/27 Oracle
详解Vue的sync修饰符
2021/05/15 Vue.js
手把手教你用SpringBoot将文件打包成zip存放或导出
2021/06/11 Java/Android