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小技巧之批量抓取美女图片
Jun 06 Python
Python基于list的append和pop方法实现堆栈与队列功能示例
Jul 24 Python
Python字符串、整数、和浮点型数相互转换实例
Aug 04 Python
Python3.6中Twisted模块安装的问题与解决
Apr 15 Python
NumPy 基本切片和索引的具体使用方法
Apr 24 Python
Django分页功能的实现代码详解
Jul 29 Python
利用jupyter网页版本进行python函数查询方式
Apr 14 Python
Python 存取npy格式数据实例
Jul 01 Python
Python数据可视化实现多种图例代码详解
Jul 14 Python
python爬虫要用到的库总结
Jul 28 Python
Python 中random 库的详细使用
Jun 03 Python
总结三种用 Python 作为小程序后端的方式
May 02 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你的验证码安全码?
2007/01/02 PHP
PHP比较运算符的详细介绍
2015/09/29 PHP
php抽象方法和抽象类实例分析
2016/12/07 PHP
php 常用的系统函数
2017/02/07 PHP
身份证号码前六位所代表的省,市,区, 以及地区编码下载
2007/04/12 Javascript
javascript+iframe 实现无刷新载入整页的代码
2010/03/17 Javascript
使用node.js 制作网站前台后台
2014/11/13 Javascript
jQuery Ajax()方法使用指南
2014/11/19 Javascript
javascript面向对象之this关键词用法分析
2015/01/13 Javascript
Jquery搜索父元素操作方法
2015/02/10 Javascript
jQuery判断数组是否包含了指定的元素
2015/03/10 Javascript
javascript Slip.js实现整屏滑动的手机网页
2015/11/25 Javascript
javascript弹出窗口中增加确定取消按钮
2016/06/24 Javascript
JS实现输入框提示文字点击时消失效果
2016/07/19 Javascript
微信小程序之获取当前位置经纬度以及地图显示详解
2017/05/09 Javascript
如何理解Vue的作用域插槽的实现原理
2017/08/19 Javascript
JavaScript基于遍历操作实现对象深拷贝功能示例
2019/03/05 Javascript
详解javascript对数组和json数组的操作
2019/04/15 Javascript
vant 中van-list的用法说明
2020/11/11 Javascript
Django实现表单验证
2018/09/08 Python
使用Python实现从各个子文件夹中复制指定文件的方法
2018/10/25 Python
浅谈Python脚本开头及导包注释自动添加方法
2018/10/27 Python
python 判断linux进程,并杀死进程的实现方法
2019/07/01 Python
keras训练曲线,混淆矩阵,CNN层输出可视化实例
2020/06/15 Python
城市观光通行证:The Sightseeing Pass
2018/04/28 全球购物
高清安全摄像头系统:Lorex Technology
2018/07/20 全球购物
波兰在线儿童和婴儿用品零售商:pinkorblue
2019/06/29 全球购物
执行总经理岗位职责
2014/02/03 职场文书
大学社团计划书
2014/05/01 职场文书
大学生个人先进事迹材料范文
2014/05/03 职场文书
高三霸气励志标语
2014/06/24 职场文书
基层党员四风问题自我剖析材料
2014/09/29 职场文书
2014年采购部工作总结
2014/11/20 职场文书
酒店仓管员岗位职责
2015/04/01 职场文书
物业前台接待岗位职责
2015/04/03 职场文书
党员干部学习三严三实心得体会
2016/01/05 职场文书