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 相关文章推荐
恢复百度云盘本地误删的文件脚本(简单方法)
Oct 21 Python
Python读取MRI并显示为灰度图像实例代码
Jan 03 Python
pandas去重复行并分类汇总的实现方法
Jan 29 Python
python实现列表的排序方法分享
Jul 01 Python
Django Admin中增加导出Excel功能过程解析
Sep 04 Python
numpy库reshape用法详解
Apr 19 Python
Python中zip函数如何使用
Jun 04 Python
Docker如何部署Python项目的实现详解
Oct 26 Python
Python3自带工具2to3.py 转换 Python2.x 代码到Python3的操作
Mar 03 Python
Pytorch数据读取之Dataset和DataLoader知识总结
May 23 Python
pandas中DataFrame数据合并连接(merge、join、concat)
May 30 Python
python中Pyqt5使用Qlabel标签播放视频
Apr 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
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
2014/06/23 PHP
取得窗口大小 兼容所有浏览器的js代码
2011/08/09 Javascript
jQuery实现首页顶部可伸缩广告特效代码
2015/04/15 Javascript
JavaScript仿支付宝密码输入框
2015/12/29 Javascript
在JavaScript中使用JSON数据
2016/02/15 Javascript
javascript iframe跨域详解
2016/10/26 Javascript
Bootstarp 基础教程之表单部分实例代码
2017/02/03 Javascript
详解angular2封装material2对话框组件
2017/03/03 Javascript
jQuery中clone()函数实现表单中增加和减少输入项
2017/05/13 jQuery
jQuery+ajax实现局部刷新的两种方法
2017/06/08 jQuery
详解vue中使用express+fetch获取本地json文件
2017/10/10 Javascript
IE9 elementUI文件上传的问题解决
2018/10/17 Javascript
js实现转动骰子模型
2019/10/24 Javascript
js实现简单的打印表格
2020/01/15 Javascript
angular *Ngif else用法详解
2020/12/15 Javascript
Python批量按比例缩小图片脚本分享
2015/05/21 Python
Python通过matplotlib画双层饼图及环形图简单示例
2017/12/15 Python
python删除过期log文件操作实例解析
2018/01/31 Python
Python输出各行命令详解
2018/02/01 Python
python自制包并用pip免提交到pypi仅安装到本机【推荐】
2019/06/03 Python
基于Python爬取51cto博客页面信息过程解析
2020/08/25 Python
详解Python GUI编程之PyQt5入门到实战
2020/12/10 Python
Canvas与Image互相转换示例代码
2013/08/09 HTML / CSS
爱尔兰最大的体育零售商:Life Style Sports
2019/06/12 全球购物
WebSphere 应用服务器都支持哪些认证
2013/12/26 面试题
机械工程系毕业生求职信
2013/09/27 职场文书
求职简历自荐信
2013/10/20 职场文书
考试作弊检讨书大全
2014/02/18 职场文书
停车位租赁协议书
2014/09/24 职场文书
建筑专业毕业生求职信
2014/09/30 职场文书
2014年消防工作总结
2014/11/21 职场文书
民事和解协议书格式
2014/11/29 职场文书
个人学习总结范文
2015/02/15 职场文书
2016春季小学开学寄语
2015/12/03 职场文书
加薪申请书应该这样写!
2019/07/04 职场文书
SpringBoot 集成短信和邮件 以阿里云短信服务为例
2022/04/22 Java/Android